C++ coding conventions:

 

Coding conventions are a specific set of “standars” defined for a specific programming language that help users with their coding. Having a “correct” styling is dificult, since many programmers may have many different ways to write one same thing; but guiding your coding through several standard conventions may be really useful for finding bugs, separating blocks of code, sections, etc.

 

Here, for my , from my course at Tec de Mty, Campus Guadalajara, ill show you some basic coding conventions for programming in C++

 

Ill write a few lines of code to show what I mean.

————

//February, 19, 2015.

 

<iostream>

 

using namespace std;

 

int main ()

{

       int x = 1

       cout << “Variable X is equal to: ” << x << endl;

       return 0;

}

————

 

 

Notice, after every statement before the “main” section of my code, I leave a blank space. This, to separate code and to let my eyes “breathe” when coding.

 

Also, the main section needs a couple of “{   }” brackets. Theres a wide discussion, i´ve been told, in whether to write the first bracker right after the second parenthesis of the “main” statement, or to write it below it. Particularilly, I like to write it below. This way, when reading and debugging, I know where my sections start and end.

 

Indentation:

 

I call ‘Code blocks’ the levels of code that englobe a certain function. Code blocks must be indented and separate from the previous block that may englobe the block in question. For example. If working with a loop that contains conditionals, you would indent one space to the right the conditional from the loop, and the loop from the main block, as shown below:

 

Main

{

      Loop

      {

              conditional

               {

                }<— closes conditional

       }<—- closes loop

} <— closes main

 

 

 

Another tip, is adding comments after and/or before code blocks. To help you remember what each section of the code does.

 

If you follow these basic conventions and master the coding syntax, you should be able to fully focus on the engineering of your programs instead of losing time debugging because of a possible mess.

 

 

 

 

CC BY 4.0 C++ coding conventions: by Rodrigo Hernández is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.