February 15, 2021

Code guidelines and standard

Did you already encounter this issue?
During your work, your boss asks you to add some cool feature to a car app. You are ready to analyze the state of art, you open the source code and… Damned! What happened here? What is this home-made XML parser? Why are we using Unix sockets here?
You continue your investigation and find tab/spaces in the same file. Bad indentation and then, the usage of multiple deprecated standards. No doubt, you are analyzing an old code base not standardized and without coding guidelines.

The issue of code without coding guidelines.

During my career, on old code base, I encounter some strange patterns: Homemade parsers, re implementation of std::string, multiple usage of C style string in no critical parts… In huge code bases, the number of developers is proportional to the risk of multiple tools integration.

The issue of code without coding guidelines that it increases the general complexity:

  • More libraries to verify during upgrading.
  • Questionable code quality.
  • Code harder to read.
  • Losses of knowledge
  • Multiple documentations to read.

However, each teams or individuals want to integrate their own tools. It is the goal of the leaders to control those tools and limit the multiplication of those libraries. I will feature here some simple solution to set standards in your code base.

An easy solution: Communication and coding guidelines usage

It is the obvious one, but also the harder to implement!
The first goal is always to communicate with the team about your objectives.
Before each library integration, you should communicate about your intentions. Debate with your colleagues and select a single tool to support this JSON parsing!

The second step is to adopt coding guidelines. A good code guideline consists to give a set of rules for using a code language in a particular environment. A coding standard is always better than no coding standard. Of course, a good standard should be written by someone with solid knowledge about the concerned language.

Therefore, my recommendation is to use an existing code guideline. You have some great examples in C++:

The usage of those guidelines will save you lot of time in the future:

  • tab/space usages.
  • Unitary test emplacement.
  • Guidelines concerning usage of floats/double/threads.
  • Easy support: The other team members use the same standards!
  • Better code analysis.
  • Consistency in your code.

However, be careful: Some software engineer will reject your coding standards. In majority, those are developers with obsolete skills. They will try to ignore your standards and you should defend the value of code standardization.
The goal here is simple: Have a simple set of rules that help developers to edit any part of the code. Important in big code bases!

Do not hesitate to check this quite interesting article from isocpp about coding guidelines.

Automation and tools.

You defined a coding guideline in your team. Congratulation, you completed the first task!

However, some developers still not respect those standards.
After months, the standards are forgiven. It is important to use automatized tools to support your code standards and force this new policy.
To help the adoption of your guidelines, you should define tools and automatic verification.

The easier to configure is your IDE! There are multiple tools to define code rules and helps for automatic verification.

For instance, you can use the Code Spell Checker in Visual Studio Code.
In eclipse, you have the Static Analysis configuration: StaticAnalysis
For another cases, you can also use some static code analysis tools. Such as CPPCheck.

And here the more important: C++ standards are evolving. Do not hesitate to edit your guidelines with the evolution of C++!

Photo by Vamsi Konduri on Unsplash

About the author 

Axel Fortun

​Developer specialized in Linux environment and embedded systems.
​Knowledge in multiple languages as C/C++, Java, Python​ and AngularJs.
​Working as Software developer since 2014 with a beginning in the car industry​ and then in ​medical systems.