ZSI / Kvalita kódu

Kvalita kódu

Základy softvérového inžinierstva

Sergej Chodarev (sergejx.net)

Bad Code Quality

You will not make the deadline by making the mess. Indeed, the mess will slow you down instantly, and will force you to miss the deadline. The only way to make the deadline—the only way to go fast—is to keep the code as clean as possible at all times.

– Robert C. Martin: Clean Code

Clean code

Every system is built from a domain-specific language designed by the programmers to describe that system. Functions are the verbs of that language, and classes are the nouns. ‹…› The art of programming is, and has always been, the art of language design.

Master programmers think of systems as stories to be told rather than programs to be written. They use the facilities of their chosen programming language to construct a much richer and more expressive language that can be used to tell that story.

– Robert C. Martin: Clean Code

Comments

Formatting

The Newspaper Metaphor

We would like a source file to be like a newspaper article. The name should be simple but explanatory. The name, by itself, should be sufficient to tell us whether we are in the right module or not. The topmost parts of the source file should provide the high-level concepts and algorithms. Detail should increase as we move downward, until at the end we find the lowest level functions and details in the source file.

– Robert C. Martin: Clean Code

Code Smells

A code smell is a surface indication that usually corresponds to a deeper problem in the system.

  1. something that's quick to spot
  2. don't always indicate a problem

Comments

  1. Inappropriate information
  2. Obsolete comment
  3. Redundant comment
    i++; /* increment i */
    
  4. Poorly written comment
  5. Commented-out code

Functions

  1. Too many arguments
  2. Output arguments
  3. Flag arguments
  4. Dead function

Duplication

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

Not all code duplication is knowledge duplication

def validate_age(value):
    validate_type(valie, :integer)
    validate_min_integer(value, 0)

def validate_quantity(value):
    validate_type(valie, :integer)
    validate_min_integer(value, 0)

Too Much Information

Obscured Intent

int m_otCalc() {
    return iThsWkd * iThsRte +
        (int) round(0.5 * iThsRte * max(0, iThsWkd - 400));
}
int straight_pay() {
    return get_tenths_worked() * get_tenth_rate();
}
int overtime_pay() {
    int overtime_tenths = max(0, get_tenths_worked() - 400);
    int overtime_pay = overtime_bonus(overtime_tenths);
    return straight_pay() + overtime_pay;
}
int overtime_bonus(int overtime_tenths) {
    double bonus = 0.5 * get_tenth_rate() * overtime_tenths;
    return (int) round(bonus);
}

Magic Numbers

Names

Refactoring

Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.

Tests

Refactoring

  1. Ensure you have good tests
  2. Take small steps

Don’t Program by Coincidence

Rely only on reliable things. Beware of accidental complexity, and don’t confuse a happy coincidence with a purposeful plan.

The Pragmatic Programmer