Table of Contents
The top 7 standards
Although these rules coding can be counted in the dozens and depend on each company, I have selected here seven rules that should, in my opinion, be used everywhere and for any IT project.
File organization and project tree
From the beginning of a project, it is essential to think carefully and define a clean tree structure. That is an organization of the files adapted to the project, the language, and the frameworks used.
Typically, when you start in development, you often create all the files in the same directory. However, it is essential to separate files that have different uses.
Similarly, these files should be named correctly. Often, this rule depends on the companies and the conventions defined in them. But, one of the principles that we find is to call these files of the same name that we name what is described.
For example, for an Automobile class, we will name the file Automobile. Extension.
For a Network Service, the file will be called a Network Service extension.
It is a simple but essential rule because it allows you to understand the usefulness of a file just by looking at its name.
The naming of classes, variables, functions, etc.
If we have to pay attention to how we name these files, it is even more important to know how we call what is in them: classes, functions, variables, etc.
Again, if these rules will change from one company to another, or even from one team to another, certain conventions remain more or less invariable.
Already, the name of these elements must make sense. Avoid calling an object containing a user’s data myObject. User (if it is an instance) or user data would make more sense.
To go into a little more detail, we often find these rules in the world of coding:
For a class, we use camelCase starting with a capital letter: User ;
For a function, we use camelCase beginning with a lowercase: connecter ;
Same for a variable: connected user ;
A constant should be written only in capital letters: USER_ID.
There are other rules, but overall, these are the most important. To summarize them:
Examples of coding best practices for item declarations
Indentation is the process of adding tabs or spaces to its lines of code; this is another essential component of coding rules. This convention, although unanimous, is sometimes a subject of discord between developers.
Some use tabs; other spaces. Some prefer to use two areas; other four.
These rules, therefore, depend on each developer, team, or company. But whatever it is, having a well-indented code is essential to its good reading and maintainability.
Often forgotten by novice developers, comments are of paramount importance. Although they have no added value in code execution – hence their omission – words are essential to correctly understanding a piece of code.
Without falling into excess and commenting on each line of code, it is essential to put comments for each class, function, and block a little complex.
This will be useful for your colleagues looking at your code and for yourself when you want to resume your code after several weeks without having worked on it.
An example of great practice when it comes to comments is the use of Javadoc comments. For example, using this standard to comment on the usefulness of a function is equivalent to adding the following lines:
Example of coding best practices for comments
s we can see, thanks to these comments, we fully understand how the function works. Some IDEs allow you to generate Javadoc comments automatically; for others, there are plugins.
File size, Functions
Here, we will talk more about optimization than actual convention, but the size (in several lines) of a file and a function are also important.
And reading 400 lines of code without losing the thread is not easy! Simply because understanding what this function does requires reading it entirely.
How can we reduce them? As for the functions, it is good, when they become too imposing, to separate them into several parts. You will easily find redundancies or pieces of code running ancillary functions suitable for export in new methods.
Some developers even say that a function should not exceed, in height, the size of the screen.
When it comes to files, it’s even simpler. If a file has 40 or 50 functions, some of them must be outsourced. It is easy to find commonalities between specific methods, which will allow the creation of functional groups that can be exported to new controllers, departments, etc.
Parentheses, semicolons, etc.
The last best practice we’re going to discuss here is using parentheses, semicolons, line breaks, etc.
This rule, or set of rules, is often entirely dependent on a company, particularly a CTO.
For example, it uses space before and after a parenthesis and skips the line at the end of an if condition. This is arbitrary.
If you work in a company that is not defined, it would be good to talk to your CTO or project manager to create these rules and thus standardize the source code of the same project.
How to ensure their implementation?
We have seen some essential rules for a beautiful, clean and maintainable code. However, how can we be sure that they are respected? How can I be sure that every developer on the same team is using the same conventions?
There are a few different ways to ensure this.
Via a code analysis tool
It exists, whether integrated or installed through plugins, reading, and code analysis tools. For example, an agency of this kind well known to web developers is ESLint.
Under Visual Studio Code, ESLint is a configurable plugin, which allows you to define conventions, rules, coding.
If you do not respect these rules, warnings will appear in your editor, or even worse, it will be impossible for you to commit your changes without correcting them… It may be a bit violent, but it is how some CTOs have found to be sure that the conventions are respected.
Fortunately, through this type of tool, you can automatically format your code with each backup, for example.
More classic but almost as effective, compliance with coding conventions can also be verified via code reviews, especially by more experienced developers, older in the company, or directly with the CTO.
Whether done together or through pull requests, these code reviews help developers understand how their code can be improved and how they do not respect established conventions.
Not always pleasant for the novice developer, these reviews are nevertheless crucial in a development team.
As we have just seen, the use of coding standards is essential. Through compliance with various rules, it allows keeping a clean source code easy to read and optimize. Whether you work alone or in a team, this is essential to maintain a project over the long term.
Also Read: Popular Programming Languages of 2021