The rules and advices are for everybody. In every environment and situation there will always be suggestions and norms that will keep order and sense in every possible ways.

In high level programming languages is absolutely the same, there are several conditions that help guiding the user while he is developing an application or a program, the only exception is that we don’t call them as “Rules”, instead, we know them as Code Conventions and of course, every programming language has different conventions to follow, they may look alike in some aspects but believe me when I tell you that the differences can also be huge.

In this mastery we are going to check out the Code conventions of C++. Outstanding the most important aspects of this rules so you can improve your skills programming and of course, following the right way to do it.

Microsoft defines “Coding Conventions” as following:

Coding conventions are suggestions that may help you write in a specific programming language.

Coding conventions can include the following:

  • Naming conventions for objects, variables, and procedures
  • Commenting conventions
  • Text formatting and indenting guidelines

The main reason for using a consistent set of coding conventions is to standardize the structure and coding style of a script or set of scripts so that you and others can easily read and understand the code. Using good coding conventions results in precise, readable, and unambiguous source code that is consistent with other language conventions and as intuitive as possible.

We can make a list of the main aspects about the coding conventions, including the most important aspects, such as:

  • General Recommendations.
  • Naming conventions.
  • Files
  • Statements

 

GENERAL RECOMMENDATIONS.

  1. Any violation to the guide is allowed if it enhances readability.

The main goal of the recommendation is to improve readability and thereby the understanding and the maintainability and general quality of the code.

  1. The rules can be violated if there are strong personal objections against them.

The attempt is to make a guideline, not to force a particular coding style onto individuals.

NAMING CONVENTIONS.

  1. Names representing types must be in mixed case starting with upper case.
  1. Variable names must be in mixed case starting with lower case.
  1. Named constants (including enumeration values) must be all uppercase using underscore to separate words.
  1. Names representing methods or functions must be verbs and written in mixed case starting with lower case.
  1. All names should be written in English.
  1. The prefix n should be used for variables representing a number of objects.
  1. Abbreviations in names should be avoided.
  1. Functions (methods returning something) should be named after what they return and procedures (void methods) after what they do.

FILES.

  1. C++ header files should have the extension .h (preferred) or .hpp. Source files can have the extension .c++ (recommended), .C, .cc or .cpp
  1. A class should be declared in a header file and defined in a source file where the name of the files match the name of the class.
  1. All definitions should reside in source files.
  1. File content must be kept within 80 columns.
  1. Special characters like TAB and page break must be avoided.
  1. Header files must contain an include guard.
  1. The incompleteness of split lines must be made obvious.

STATEMENTS.

  1. Types that are local to one file only can be declared inside that file.
  1. Variables should be initialized where they are declared.
  1. C++ pointers and references should have their reference symbol next to the type rather than to the name.
  1. Loop variables should be initialized immediately before the loop.
  1. The form while(true) should be used for infinite loops.
  1. Complex conditional expressions must be avoided. Introduce temporary Boolean variables instead.

Those were some of the most important conventions used for C++ divided by categories. Therefore it is really important to memorize as much as we can so we can improve our programming skills and make our code syntaxes even clearer so everyone with knowledge of the language will be able to understand it without problems.

Here is a page where you will be able to see all the Code Convention of C++ and some really good examples.

CC BY 4.0 C++ Coding Conventions by esaupreciado is licensed under a Creative Commons Attribution 4.0 International License.