Clean Code | Chapter(5) | Formatting

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:

No alt text provided for this image

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.

No alt text provided for this image
No alt text provided for this image

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:
No alt text provided for this image

For loops control variables should usually be declared within the loop statement.

No alt text provided for this image

In rare cases a variable might be declared at the top of a block or just before a loop in the long function.

No alt text provided for this image
  • 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

No alt text provided for this image
  • 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:
No alt text provided for this image

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:

No alt text provided for this image

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.

No alt text provided for this image

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:

No alt text provided for this image

So, don't make alignment, and make your code like this:

No alt text provided for this image

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.
No alt text provided for this image
No alt text provided for this image

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):

No alt text provided for this image


Thanks for reading, waiting your feedback🙂


Ahmed Elsharhawy

Software Engineer | Flutter Developer@MainBrains & Android Native

5mo

جامد

To view or add a comment, sign in

More articles by Mahmoud Ibrahim

  • Clean Code | Chapter(6) | Objects and Data Structures

    Clean Code | Chapter(6) | Objects and Data Structures

    Data Abstraction: Abstraction means hiding implementation. Hiding implementation isn't means making the variables…

    7 Comments
  • Clean Code | Chapter(4) | Comments

    Clean Code | Chapter(4) | Comments

    This chapter talks about some rules should be followed when writing the comments, and some cases which writing a…

    2 Comments
  • Clean Code | Chapter(3) | Functions

    Clean Code | Chapter(3) | Functions

    The following rules is about the mechanics of writing functions well. If you follow these rules, your functions will be…

    5 Comments
  • Clean Code | Chapter(2) | Meaningful Names

    Clean Code | Chapter(2) | Meaningful Names

    The following 15 rules for creating a good names for any thing in your software: variables, functions, arguments…

    18 Comments

Insights from the community

Others also viewed

Explore topics