CO886: Software Engineering
Code quality






Tomas Petricek

email: t.petricek@kent.ac.uk
twitter: @tomaspetricek
office: S129A

Software engineering today

Mathematician's answer to software engineering

Use mathematical methods to prove system correctness

People don't do it, yet, software mostly works.

Detailed upfront models

We know where
we want to get

Useful if predictability matters more than cost and delivery time

Software engineering today

In contrast to upfront model

We know that we continue
moving in the right direction

Tools and practices to ensure that

  • We can learn from past mistakes
  • We're tracking known issues
  • Software keeps working when we make changes
  • It is easy to improve and contribute

Software engineering today

Some important aspects that we will cover

Source control and collaborative development

Testing and build automation

Continuous integration and reproducibility

How open-source software development works

Code quality, naming and "good code"

Code quality

Software Maintenance

You spend more time reading and modifying code than writing it.

Investing into readability when writing pays off

eXtreme Programming (2000s)

Keeping code simple

  • Write self-documenting code
  • Reduce need for comments
  • You Ain't Gonna Need It (YAGNI)

Shared understanding in a team

  • Common coding standards
  • Pair programming
  • Use of code reviews

Code for readability

Always code as if the person maintaining your code will be a violent psychopath who knows where you live.

The person maintaining it will likely be future you.

What is good quality code?

Hacker culture

  • Quality is learned by experience
  • Apprenticeships if coding was craft
  • Good examples, but no written rules

Coding standards

  • Explicit rules that one can follow
  • There are often exceptions
  • Good starting point, but not ultimate answer

Do programming languages matter?

[It] appear[s] that strong typing is modestly better than weak typing, and among functional languages, static typing is also somewhat better than dynamic typing.

Do programming languages matter?

Attempt to reproduce the study mostly failed

"I believe they do in my heart of hearts, but it's kind of an impossible experiment to run."

Code quality

Aspects of code quality

  • Variable and function names
  • Structure of code logic
  • Organization of functionality

Processes for keeping quality

  • Improving quality through refactoring
  • Ensuring code quality using tools
  • Ensuring code quality using social processes

Naming

Good naming is easy today!

Computers supports
lower case letters!

Long names fit in
computer memory

Auto-complete helps
you avoid typos

Naming of variables (1/3)

What is the following function doing?

1: 
2: 
3: 
function conv(x) {
  return x * 9 / 5 + 32;
}

Converting temperature from Celsius to Fahrenheit!

1: 
2: 
3: 
function celsiusToFahrenheit(temperature) {
  return temperature * 9 / 5 + 32;
}

Naming of variables (2/3)

Was that descriptive enough?

1: 
2: 
3: 
4: 
function convertTemperatureFromCelsiusToFahrenheit
    (temperatureInCelsius) {
  return temperatureInCelsius * 9 / 5 + 32;
}

Can we improve this by using abbreviations?

1: 
2: 
3: 
function convTempFromCelsToFahr(tempInCels) {
  return tempInCels * 9 / 5 + 32;
}

Naming of variables (3/3)

Are all variable names in this example descriptive?

1: 
2: 
3: 
var result = 0;
for(var i = 0; i < data.length; i++) result += data[i];
return result;

This is too much. Sensible exceptions allowed!

1: 
2: 
3: 
4: 
var resultNumber = 0;
for(var dataIndex = 0; dataIndex < data.length; dataIndex++)
  resultNumber += data[dataIndex];
return resultNumber;

Naming of variables

Descriptive names of reasonable length

Name should add semantic information

No need to say it's number, string, etc.

Longer names are good, but don't overdo it!

Follow common conventions where applicable

The more local, the less it matters

Naming of variables, functions and classes

Combining words in names

  • PascalCase
  • camelCase
  • snake_case

Using spaces in a name

  • Would makes code nightmare to parse
  • Some language allow escaping e.g. ``funny name!``

Naming of classes and methods in Java

Is the following naming consistent or not?

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
public class Program {
   public Program(Boolean adminMode) {
     // Some actual logic here
   }
   public static void main(String []args) {
      Program prog = new Program(false);
   }
}

Pascal case Program, camelCase adminMode, main

Naming classes and other issues

Coding standards and exceptions

Java standards use PascalCase for class names

camelCase is used for variables and method names

You should generally follow coding standards

Exceptions such as numerical code: P or alpha

Whitespace

How does compiler understand structure

Indentation or
curly brackets?

Compiler may ignore whitespace, but human brain does not!

Consistent code indentation (1/2)

What is the right spacing and line breaks?

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
if (isAvailable( product ))
  {
    if (!isHidden(product)) {
    return getProductPage(product);
    } else
        return null;
} else
return null;

Compiler ignores whitespace, but humans do not!

Consistent code indentation (2/2)

Follow style of a project or a language; be consistent!

Indent nested code by the same number of spaces or tabs

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
if (isAvailable(product)) {
    if (!isHidden(product)) {
        return getProductPage(product);
    } else {
        return null;
    }
} else {
    return null;
}

Summary

Code quality

Software engineering today
Process that ensures things keep improving
Supported by tools and good practices

What is code quality and why it matters
More time is spent reading code than writing it
Extreme programming - simplicity & shared understanding

Specific rules for keeping high code quality
Naming variables, upper/lower case
Follow consistent indentation rules

CO886: Code quality

What you should remember from this lecture

  • Role of code quality in modern SWE
  • Rules for good naming and lower/upper case
  • Practice making your code look nice!


Tomas Petricek
t.petricek@kent.ac.uk | @tomaspetricek

References

Selected chapters from books

Papers and articles