10 most voted C++ best practices

Every  project has its own style guide: a set of conventions about how to write code for that project. Some managers choose a basic coding rules, others prefer very advanced ones and for many projects no coding rules are specified, and each developer uses his style.

The coding style has a big impact on the source code readability, investing some hours to train developers, and doing periodically a code review is always good to make the code easy to maintain and evolve.

Many resources exist talking about the better coding rules to adopt. However  even if thousands of practices are recommended,  the developers focus only on some of them.

In case of C++ here’s the list of the 10 most voted best practices in our C++ best practices repository:

1- Always compile at the highest warning level possibleA lot of bugs can be avoided by paying attention to compiler diagnostics

2- Code that is not used (commented out) shall be deleted

3-If you can exit a function early, you shouldEarly exits out of a function, specially through guard clauses at the top of a function are preferred since they simplify the logic further down in the function.

4- Avoid methods with too many local variablesMethods where NbVariables > 8 are hard to understand and maintain. Methods where NbVariables > 15 are extremely complex and must be refactored

5- Avoid types with too many fieldsTypes where fields count > 20   might be hard to understand and maintain but there might be cases where it is relevant to have a high number of fields.

6- Avoid types with too many methodsTypes where methods count > 20 might be hard to  understand and maintain but there might be cases where it is relevant to have a high number of methods.

7- Avoid too big types

8- Avoid too complex methods

9- Avoid methods with too many parameters

10- Avoid too big methods

As we can remark the most voted rules are the most basic and generic ones, every developer could just use the common sens to respect them, and these rules may be applied to any other language.

Leave a Reply

Your email address will not be published. Required fields are marked *