User Tools

Site Tools


principles:single_level_of_abstraction

Single Level of Abstraction (SLA)

Variants and Alternative Names

  • One Level of Abstraction
  • Don't Mix Different Levels of Abstractions

Context

Principle Statement

Each method should be written in terms of a single level of abstraction.

Description

All statements of a method should belong to the same level of abstraction. If there is a statement which belongs to a lower level of abstraction, it should go to a private method which comprises statements on this level. Doing so will result in smaller methods.

Often the body of a loop can be extracted resulting in a separate private method. Loops should ideally contain a single statement (usually a method call). Sometimes this is not achievable without other drawbacks but certainly large loop bodies can be considered a smell.

A further indicator for a missing method is the combination of a blank line, a comment and a block of code. In most of the cases the code block should go to a new private method. This also makes the comment obsolete as the new method carries a name which typically resembles the comment.

Sometimes extracting the method would result in the new method having a large number of parameters. Alternatively the parameters could be converted to fields of the class. But this would often result in bad cohesion. Because of that in such a case extracting a new class is the next step in adhering to the principle.

Rationale

Switching between levels of abstraction makes code harder to read. While reading the code you have to mentally construct the missing abstractions by trying to find groups of statements which belong together (mental grouping).

Strategies

Caveats

See section contrary principles.

Origin

Stated in Clean Code (p. 36). The principle is maybe older, though.

Evidence

  • Accepted: Described in “Clean Code”

Relations to Other Principles

Generalizations

Specializations

Contrary Principles

  • MIMC: Adhering to SLA results in more methods and classes.
  • PSU: The purpose of SLA is to avoid mental grouping. On the other hand just adhering to SLA and neglecting PSU may result in the opposite: The reader of the code has to do mental inlining. Sometimes it can be more readable to allow a small amount of statements on the “wrong” level of abstraction (like having a guarding if statement in a higher level method).

Complementary Principles

  • MIMC: Adhering to SLA results in smaller methods.
  • HC: Adhering to SLA by extracting methods may result in bad cohesion if you don't extract classes if necessary.
  • MP: MP tells how to find suitable abstractions when abstracting methods and classes in order to adhere to SLA.

Principle Collections

Examples

Example1: Loops

A typical example for the application of SLA is a loop iterating over a certain data structure:

public List<ResultDto> buildResult(Set<ResultEntity> resultSet) {
    List<ResultDto> result = new ArrayList<>();
    for (ResultEntity entity : resultSet) {
        ResultDto dto = new ResultDto();
        dto.setShoeSize(entity.getShoeSize());        
        dto.setNumberOfEarthWorms(entity.getNumberOfEarthWorms());
        dto.setAge(computeAge(entity.getBirthday()));
        result.add(dto);
    }
    return result;
}

There are two levels of abstractions in this method. First there is the loop which acts upon the whole result set and second there is the loop body which converts a single entity to a DTO. For the latter there is no syntactical grouping. The reader of the code has to find out that the first four lines of the loop body belong together. The code also doesn't explicitly state that these four lines convert an entity to a DTO. So the following code is better:

public List<ResultDto> buildResult(Set<ResultEntity> resultSet) {
    List<ResultDto> result = new ArrayList<>();
    for (ResultEntity entity : resultSet) {
        result.add(toDto(entity));
    }
    return result;
}
 
private ResultDto toDto(ResultEntity entity) {
    ResultDto dto = new ResultDto();
    dto.setShoeSize(entity.getShoeSize());        
    dto.setNumberOfEarthWorms(entity.getNumberOfEarthWorms());
    dto.setAge(computeAge(entity.getBirthday()));
    return dto;
}

Now there are two smaller methods each of which is written in terms of a single level of abstraction. This is better readable as no mental grouping is necessary. Furthermore the two methods are still separately understandable (PSU) so no mental inlining is necessary and if you don't care about the details of the toDto method, you can just read and understand buildResult without being distracted by unnecessary detail.

Example2: Comment Plus Code Block

Example3: Parameter Checking

Example4: Extracting Classes

Description Status

Further Reading

Discussion

Discuss this wiki article and the principle on the corresponding talk page.

principles/single_level_of_abstraction.txt · Last modified: 2017-01-10 10:15 by christian · Currently locked by: 51.255.65.74