Jump to content

User:Xvedejas/Style

From Wikipedia, the free encyclopedia

Braces and Indentation

[edit]

All indentation should be four-spaces (soft tabs). It's basically a variation of the "Horstmann style" according to wikipedia. Braces should be lined up either on rows or columns for functions and control statements.

while (x == y) 
{   something();
    somethingelse();
    //...
    if (x < 0)
    {   printf("Negative");
        negative(x);
    } else 
    {   printf("Positive");
        positive(x);
    }
}
finalthing();

Omitting braces is okay, but only if there is exactly one line within.

// Okay:

if (a)
    func();

// Bad:

if (a)
    if (b)
        func();

Parens do not need to be lined up. The first paren of a function should always be directly after the function name.

very_long_function_name_needs_indentation(
    argument_with_long_name_1,
    argument_with_long_name_2,
    argument_with_long_name_3
);

There should be a space between a keyword and paren.

while (condition) ...;

The "while" in do/while or "else" in if/elseif/else should be directly after the close brace.

do
{   ...
    ...
} while (condition);
if (condition)
{   ...
    ...
} else if (condition)
{   ...
    ...
} else
{   ...
    ...
}

More complex switch/case statements should use extra braces for scoping. "break"s should come after the closing brace. Always include a "break" after the default statement (save issues when refactoring)

switch (var)
{  case 0:
   {   ...
       ...
   } break;
   case 1:
   {   ...
       ...
   } break;
   default:
   {   ...
       ...
   } break;
}

Definitions

[edit]

Don't define (but you can declare) variables all on one line.

// Okay:

int a, b, c;

// Bad:

int a = 1, b = 2, c = 3;

// Okay:

int a = 1,
    b = 2,
    c = 3;