A Couple of Tips for Effective Code Formatting
By Argishti Yeghiazaryan
To ensure continues development, we had a goal of perfecting the code formatting of our team. Below you can see the steps that can help you achieve a clean and accurate code formatting.
The objective of formatting
It’s essential for the programmer to follow code formatting. The code the programmer writes to solve any problem can be adjusted with a different solution at some point, which can be implemented easier if the code is readable and formatted. Let’s look at a few code formatting rules below.
1. Vertical formatting
Let’s understand how big the files containing our code should be? How much should the minimum and the maximum number of lines be in each source file? Research has shown that it is possible to design quite serious programs and systems with the maximum amount of 500 lines in the source file, and on average it’s usually 200 lines. Small source files are easier to understand than the large ones. That’s why having some sort of limitations with the vertical formatting is very important.
2. The similarities between the source file and a newspaper article
Source file and the code it contains should mimic a newspaper article with its structure. What construction does the news piece have and keep in mind what information we get from it? The first paragraph gives us a general understanding of what the story is about. We proceed with the piece and get into other details. The further we go down with the lines in the article the more information we gather. A similar feeling has to accompany the programmer as they read the code in the source file. In particular, the naming of the file has to be simple and informative about the contents of it. In the top lines of the file high-level concepts and algorithms should be placed and on low-level lines — functions, etc.
3. Vertical distance
Let’s look at two examples of formatting on the same code
Image 1: Without concept separation, code formatting looks like this
Image 2: Code formatting with concept separation through a vertical distance
It’s clear that the code in the second image is more legible and the empty spaces used help our eye easily differentiate the concepts better than in the first image.
Declaration of variables
The declaration of variables has to be done as close to their use as possible. If the variable we declared is used only in the space of the implementation space of one method, then the declaration of the variable needs to be done in the local scope.
Declaration of class variables
The declaration of class variables has to be done in the top part of the class. Following this rule, you can ease the search of the declaration of variables in the code and you won’t have to go through every line of the Class.
Dependent Functions
When one function has a call for another function, then the function that’s being called has to be in the bottom part of the calling function. (Vertical Ordering)
Horizontal Formatting
How long does the length of our line have to be??
The optimal length for the line is a maximum of 120 words. Of course, we can’t definitely insist that overlapping the mentioned length is wrong. Nevertheless, the length of the line has to be such that the programmer doesn’t have to scroll horizontally to read it.
Indentation
There are a few rules to leaving some space at the beginning of the code, the ones used the most by the programmers are the following:
1. The class declaration doesn’t have space in the beginning
2. Class methods have one level to the right indentation
3. The contents of class degree themselves are written with one level to the right indentation
4. If the method has blocks, the contents of the block also have to have one level to the right indentation.
As we see, there are a few important points that when followed will result in clean and comfortable code formatting. With vertical formatting, the programmer needs to stick to a maximum of 500 lines and with vertical — 200 lines. We need to pay attention to space, variable and class declarations and codependent functions. So follow these rules and ease the further adjustments of codes and your collaboration with other programmers.
Keep in touch with HelpSystems Armenia on Facebook, Instagram or LinkedIn.