Clean Code | Chapter(5) | Formatting
Code formatting is important. Code formatting is about communication, and communication is the professional developer’s first order of business.
Vertical Formatting
1. Vertical Openness Between Concepts:
Should be there are a blank lines that separate the package declaration, the import(s), and each of the functions. As the following:
2. Vertical Density:
The lines of code that are more related to each other should appear close to each other. For example: the following two snippet, as you see the second snippet is much easier to read than the first.
3. Vertical Distance:
- Variable Declarations: variables should be declared as close to their usage as possible. Because our functions are very short, local variables should appear at the top of each function. like the following:
For loops control variables should usually be declared within the loop statement.
In rare cases a variable might be declared at the top of a block or just before a loop in the long function.
- Instance Variables: should be declared in one well-known place (at the top of the class). Because they are used by many of the methods within the class. And because everybody should know where to go to see the declarations.
- Dependent Functions: If one function calls another, they should be vertically close, and the caller should be above the callee. For example:
Listing 5-5 WikiPageResponder.java
- Conceptual Affinity: this means certain parts of code want to be near other parts, sometimes because a group of functions perform a similar operation or one function calling another. For example:
These functions have a strong conceptual affinity because they share a common naming scheme and perform variations of the same basic task. So, they would want to be close together.
4. Vertical Ordering:
This means a function that is called should be below a function that does the calling. We want this because it creates a nice flow down the source code module from high level to low level. For example: Scroll above to see Listing 5-5 WikiPageResponder.java.
Horizontal Formatting
How wide should a line be? The best answer for that is "you should never have to scroll to the right". "I personally set my limit at 120 characters per line"..Uncle bob said.
1. Horizontal Openness and Density
We didn't put horizontal space for things that are more related, and we put horizontal space for things are less related (are separate). For example:
Look at the above circles and read the following points:
- The writer didn’t put horizontal spaces between the function name and the opening parenthesis. This is because the function and its arguments are closely related (more related).
- Surround the assignment operators with horizontal spaces (left side and the right side) to clarify them. This because they are separate (less related).
- Separated arguments within the function call parenthesis to clarify the comma and show that the arguments are separate.
Another use for horizontal space is to clarify the precedence of operators.
Notice how nicely the equations read,
- The factors have no horizontal space between them because they are high precedence.
- The terms are separated by horizontal space because '+' and '-' are lower precedence.
2. Horizontal Alignment
The horizontal alignment is not useful and it leads my eye away from the true purpose, like the following example:
So, don't make alignment, and make your code like this:
3. Indentation
The indentation is the left space that show the scopes of classes within the file, the methods within the classes, the blocks within the methods, and recursively to the blocks within the blocks. Each level of this hierarchy is a scope.
To make this hierarchy of scopes visible, we indent (length of left space) the lines of source code in proportion to their position in the hierarchy. Follow the following indentation rules:
- Statements at the level of the file, such as class declarations, are not indented (no left space)
- Methods within a class are indented one level to the right of the class.
- Implementations of those methods are indented one level to the right of the method declaration.
- Block implementations are implemented one level to the right of their containing block, and so on.
Team Rules
A team of developers should agree upon a single formatting style, and then every member of that team should use that style. We want the software to have a consistent style.
Uncle Bob’s Formatting Rules
This is an example of formatting the code by Uncle Bob (Robert C. Martin):
Thanks for reading, waiting your feedback🙂
Software Engineer | Flutter Developer@MainBrains & Android Native
5moجامد