Skip to content

Instantly share code, notes, and snippets.

@hobroker
Last active February 19, 2025 11:10
Show Gist options
  • Save hobroker/671ccb14189e8a26b8738fc06419436d to your computer and use it in GitHub Desktop.
Save hobroker/671ccb14189e8a26b8738fc06419436d to your computer and use it in GitHub Desktop.
C++ Code Formatting

1. Indentation and Whitespace

a. Consistent Indentation

  • Choose an indentation size: Common choices are 2 or 4 spaces. Some styles use tabs, but spaces are generally more consistent across editors.
  • Indent each new code block: Inside any set of { }, increase the indentation level by one. When you exit that block, decrease the indentation level.

Example (indenting 4 spaces):

int main() {
    int x = 10;
    if (x > 5) {
        // Inside an if block, so we indent further
        x += 5;
    }
    return 0;
}

b. Strategic Use of Whitespace

  • Around operators: x = 10; (rather than x=10;)
  • After commas in function arguments: function(a, b, c);
  • Between control structures and parentheses (many styles prefer if (condition) instead of if(condition)).

Whitespace usage helps code readability, so it's good to show how it impacts scanning and readability.


2. Braces and Block Placement

There are a few common brace styles. Pick one style and stay consistent. Two popular ones are:

  1. K&R Style (also known as One True Brace Style):

    int main() {
        // code
    }

    The opening brace is on the same line as the statement.

  2. Allman Style (brace on its own line):

    if (x > 5)
    {
        x += 10;
    }
    else
    {
        x--;
    }
    

3. Line Breaking and Wrapping

a. Max Line Width

  • Encourage a limit on line length (say 80 or 100 characters) to avoid horizontal scrolling.
  • When a line of code is too long, break it in a logical place (e.g., after a comma, after an operator, etc.).

b. Breaking Function Calls

  • If a function call's parameter list is long, place each argument on its own line, indented once:

    myFunction(longArgumentName,
           anotherArgument,
           lastArgument);

c. Breaking Conditionals

  • For complex if statements or loops:

    if (someCondition &&
        anotherCondition &&
        yetAnotherCondition)
    {
        // ...
    }

4. Vertical Spacing and Grouping

Use blank lines to separate logical sections of code: group related lines together, then use a blank line before the next section.

Example:

// 1. Variable initialization
int a = 0;
int b = 5;

// 2. Processing
a = b + 10;

// 3. Final Output
std::cout << a << std::endl;

5. Naming Conventions and Consistency

While naming conventions aren't strictly "formatting," they often go hand in hand with clean code. Consistency in naming variables and functions also makes the code more readable. For instance:

  • snake_case or camelCase for variables/functions
  • PascalCase for classes

Example:

// camelCase
int playerHealth = 100;
void updateHealth(int changeAmount);

// snake_case
int player_health = 100;
void update_health(int change_amount);

Format the following code

#include <iostream>
using namespace std;int main(){int x=5;if(x>0){x+=10;for(int i=0;i<x;i++){cout<<i<<" ";}cout<<endl;}return 0;}
  1. Correct Indentation

    • Each new block (like those after if, else, and for) is indented by at least two or four spaces (or one tab, depending on your style guide).
    • Match opening and closing braces { } at the same indentation level.
  2. Add Whitespace for Clarity

    • Add spaces around operators (=, <, ++, etc.) to improve readability.
    • Insert a blank line between logical sections of the code to separate them visually.
  3. Consistent Brace Placement

    • Decide on a style (K&R, Allman, etc.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment