Skip to content
Taynara Carvalho edited this page Sep 22, 2017 · 1 revision

Style Sheet C and C++

1 - Naming Conventions
2 - Constants
3 - Boolean
4 - Global vs. Local Variables
5 - Array
6 - Pointers and reference
7 - Coding​ ​ language
8 - Strings
9 - Line Length
10 - Identation
11 - Control​ ​ Structure:​ ​ IF
12 - Control​ ​ structure:​ ​ Switch
13 - Repetition ​ structure:​ ​ While
14 - Repetition ​ structure:​ ​ For
15 - Comments
16 - Header Style
17 - Exception handling
18 - Writing Doc Comments

1 - Naming Conventions:

  • Domain abbreviations in method names, variables, constrains, classes and attributes should be avoided. Do not use names and letters without a well-defined, easily understood meaning. Always use meaningful names.

😃 Good example:

    tower ();

😩 Bad Example:

    twr();
  • For declaration:
    • Class name
    • Name Of Enums
    • Name Interfaces

Snake case: Writes compound words or phrases in which the elements are separated with one underscore character (_) and no spaces, with each element's initial letter usually uppercased within the compound and the first letter upper and the first letter after underscore also — as in "Hello_Word". If have more word, the same pattern above will applied.

😃 Good example:

    class Country_Region
    {
        enum Distance
        {
            London_Manchester,
            London_Cardiff,
            London_Liverpool
        };
    }

😩 Bad Example:

    class Country_region
        enum distance
        {
            London_manchester,
            London_cardiff,
            London_liverpool
       };
  • For declaration:
    • attributes
    • variables
    • methods

Snake case: Writes compound words or phrases in which the elements are separated with one underscore character (_) and no spaces, with each element's initial letter usually lowercased within the compound and the first letter after underscore also lower case—as in "foo_bar". If have more word, the same pattern above will applied.

😃 Good example:

    int do_stuff ( int some_number ) 
    {
        return some_number * 2;
    }

😩 Bad Example:

    int doStuff(int someNumber){
        return someNumber*2;
    }
  • Initialize all variables when declaring

😃 Good example:

    int velocity_car = 0;  

😩 Bad Example:

    int velocity_car;

2 - Constants

  • Constants are written in all capital letters. Underscores may be used to separate words. For example, MAX_SIZE, E, PI;

😃 Good example:

    MAX_SIZE, E, PI;

😩 Bad Example:

    max_Size, e, Pi;
  • Use all uppercase letters, with underscores, for #ifndef/#define/#endif. For example:

😃 Good example:

    #ifndef TIMEFILE_H, #define TIMEFILE_H;

😩 Bad Example:

    #ifndef timeFile_h, #define timeFile_h;

3 - Boolean

  • Boolean variables should be named so that they state the affirmative action.

😃 Good example:

    bool valid;  //Use this affirmative form.

😩 Bad Example:

    bool notValid; //Avoid this

4 - Global vs. Local Variables

  • Declare variables to be local to the function(s) in which they are used.
  • Place named constants as local as possible.
  • Do not use global variables to get around the need to pass parameters.

5 - Array

  • Array with one dimension, or with more than one, use a single space only for separate the equal sign, to separate the brackets and also to separate values inside on the same array.

😃 Good example:

    int test_score [ ] = { 74, 87, 91 };
    char grades [ 5 ];
    char matrix [ 32 ] [ 16 ];

😩 Bad Example:

    int test_score[ ]={ 74,87,91 };
    char grades[ 5 ] ;
    char matrix[ 32 ][ 16 ] ;

6 - Pointers and reference

  • C++ pointers and references should have their reference symbol next to the name rather than to the type.

😃 Good example:

    float *pointer_name;
    int  &reference_name; 

😩 Bad Example:

    float* pointer_name;
    int& reference_name; 

7 - Coding​ ​ language

  • The​ ​ coding​ ​ language​ ​ for​ ​ programming​ ​ techniques​ ​ applying​ ​ is​ ​ English.

8 - Strings

  • Strings must​ ​ be​ ​ written​ ​ between​ ​ double​ ​ quotes​ ​ ”.

😃 Good example:

    ask_name = "Whats is your name? ";

9 - Line Length

  • Avoid lines longer than 120 characters. When a statement won't fill in a single line, it may be necessary to break it.

10 - Identation

  • When using braces, open on the below line to which it was called and close vertically aligned with the name to which called it. It include functions, class, control​ ​ structure and repetition among others.
  • Indent with 2 spaces — pick one and stick with it. Be consistent. Do not indent with the TAB key — your indents will generally be way too wide.
  • The​ ​ next​ ​ line​ ​ after​ ​ the​ ​ class​ ​ signature or file​ ​ have​ ​ to​ ​ be​ ​ a ​ ​ blank​ ​ line.
  • All files finish with blank lines.

😃 Good example:

    void state_moving_crouch ()
    {
        this -> player -> max_speed *= 2;
        this -> player -> speed = 20;
    }

😩 Bad Example:

    void state_moving_crouch(){
    this->player->maxSpeed *= 2;
    this->player->speed = 20;}

11 - Control​ ​ Structure:​ ​ IF

  • There​ ​ must​ ​ have​ ​ a ​ ​ blank​ ​ space​ ​ immediately​ ​ after​ ​ the​ ​ keyword​ ​ if.
  • There​ ​ must​ ​ have​ ​ a ​ ​ blank​ ​ space​ ​ between​ ​ the​ ​ parentheses​ ​ and​ ​ the​ ​ if condition.
  • ​The​ ​ comparison​ ​ operators​ ​ must​ ​ have​ ​ a ​ ​ blank​ ​ space​ ​ immediately​ ​ to​ ​ the​ ​ left and​ ​ the​ ​ right.​ ​If​ ​ line​ ​ exceed​ ​ 80​ ​ characters,​ ​ it​ ​ must​ ​ be​ ​ broken​ ​ into​ ​ multiple​ ​ lines​ ​ and​ ​ the condition​ ​ must​ ​ be​ ​ aligned​ ​ with​ ​ the​ ​ previous​ ​ one.
  • Braces must open in the below line the conditional structure, aligned vertically. Closing with the same aligned.
  • ​The​ ​ body​ ​ must​ ​ be​ ​ written​ ​ between​ ​ braces, skipping 1 line,​ ​ even​ ​ when​ ​ it​ ​ is​ ​ a ​ ​ single​ ​ line. ​ 'Else​ ​ if'​ ​ or​ ​ 'else'​ ​ must​ ​ be​ ​ initiated​ ​ on​ ​ the​ ​ same​ ​ line​ ​ of​ ​ the​ ​ closing​ ​ braces.
  • After​ ​ all​ ​ 'if'​ ​ or​ ​ 'else​ ​ if'​ ​ structure,​ ​ an​ ​ 'else'​ ​ structure​ ​ is​ ​ always​ ​ needed.
  • Avoid nested if statements — they can become very hard to understand and debug.

😃 Good example:

    if​ ​( ​person_age​​ < ​18​ &&​ ​//​ ​There​ ​ is​ ​ more​ ​ than​ ​ 80​ ​ characters​ ​ here
        person_vip ==​ NULL )​ ​
    {
        cout ​<< "Not​ ​ authorized​ ​ to​ ​ drive!"​ ;
    }​ else​ ​
      {
            //​ ​ . ​ ​ . ​ ​ .
      }
    }

    if​ ( week_day​ ​==​ ​1 ) 
    ​{
        cout​ <<​  "Sunday"​ ;
    }​ elseif​ ​( week_day​​ ==​ ​2​ )
      {
        cout​ << ​ "Monday"​;
      }

😩 Bad Example:

    if(cond1) cout << 1;
    else if (cond2) cout << 2;
    else cout << 3;

12 - Control​ ​ structure:​ ​ Switch

  • Defaults​ ​ and​ ​ cases​ ​ have​ ​ to​ ​ be​ ​ indented like an if statement;
  • ​Statements​ ​ of​ ​ switch​ ​ case​ ​ or​ ​ default​ ​ have​ ​ to​ ​ be​ ​ in​ ​ the​ ​ next​ ​ line​ ​after braces and indented.

😃 Good example:

    switch​ ​ ( day_week​ ​) 
    {
    case​ ​ 1:
        cout​ << ​ "Sunday"​;
        break;
    case​ ​ 2:
        cout <<​ "Monday"​;
        break;
        //​ ​ ...
    default:
        cout << ​ " "​;
        break;
    }

😩 Bad Example:

    switch​(day_week​ ​){
    case​ ​ 1: ​cout​ << ​ "Sunday"​;
    break;
    case​ ​ 2: cout <<​ "Monday"​;
    break;
    //​ ​ ...
    default: cout << ​ " "​;
    break;
    }

13 - Repetition ​ structure:​ ​ While

  • While follows the same if pattern.

😃 Good example:

    while ( count < number_elements ) 
    {
        sum_elements = sum_elements + 1;
    }

😩 Bad Example:

    while (condition) continue ;
  • The format while(true) should be used for infinite loops.

😃 Good example:

    while ( true ) 
    {
        //  ...
    }

😩 Bad Example:

    while (;;)
    {
        //  ...
    }
or 
    while(1)
    {
        //  ...
    }

14 - Repetition ​ structure:​ ​ For

  • The for structure follows the same if pattern.

😃 Good example:

    int i = 0;
    for ( i = 0;  i  <  number_elements;  i++ ) 
    {
        cout << number_elements;
    }

😩 Bad Example:

    for (condition) continue;
  • Only loop control statements must be included in the for() construction.

😃 Good example:

    sum = 0;
    for( i = 0;  i < 100;  i++ ) 
    {
        sum += value [ i ];
    }

😩 Bad Example:

    for(i =  0; sum = 0; i < 100; i++)
    {
        sum +=value[i];
    }

15 - Comments

  • Comments begin with capital letter
  • Use only // in comments to one line

😃 Good example:

    // This is a simple comment.

😩 Bad Example:

    /*This is a simple comment*/	
  • Comments with more than one line should start with / * and close with * / following the structure of the following example:

😃 Good example:

    /* This function calculates the profit
     * of the month in the sales department */

😩 Bad Example:

    // This function calculates the profit
    // of the month in the sales department 

16 - Header Style

  • There should always be the file name
  • There must always be the purpose of this file
  • There must always be the license name
  • There must always be the name of the project
  • There must always be the name of the college.

😃 Good example:

    	  /* Nome do Jogo 
    	   * Universidade de Brasília - FGA
    	   * Técnicas de Programação, 2/2017
    	   * @file name_of_file.cpp
    	   * A description which can be long and explain the complete
    	   * functionality of this module even with indented code examples.
    	   * Class/Function however should not be documented here*/

17 - Exception handling:

The exception handling follows the same pattern already presented here for conditional if and else.
The catch will begin after a simple space in the same line to which the try key closes.

😃 Good example:

          Try 
          {
            new_phrase = phrase.toUpperCase();
          } catch(.....) // CATCH OF THE POSSIBLE EXCEPTION. 
            {
              // Exception handling
              cout << "The initial phrase is null, it was assigned a default value";
              phrase = "empty phrase";
              newPhrase = phrase.toUpperCase();
            }

18 - Writing Doc Comments

A doc comment is written in HTML and must precede a class, field, constructor or method declaration. It is made up of two parts -- a description followed by block tags. In this example, the block tags are @param (methods and constructors only), @return(methods only), and @see(refer to another symbol or resource that may be related to the one being documented).

😃 Good example:

/* 
Updates the position of the dynamic entity
Update is based on what input was recieved, and the players velocity.
@param dt_ : Delta time. Time elapsed between one frame and the other
@return A bool array with the sides the DynamicEntity collided.
@see StateBoss::exit
*/