wiki:Coding Style Guidelines

Version 1 (modified by sam@webkit.org, 18 years ago) ( diff )

First wack at transfering Style Guildlines to the Wiki

Indenting

  • Use spaces to indent. Tabs should not appear in code files (with the exception of files that require tabs, e.g. Makefiles). We have a Subversion pre-commit script that enforces this rule for most source files, preventing commits of files that don't follow this rule.
  • The indent size is 4 spaces.
  • Code editors should be configured to expand tabs that you type to 4 spaces.

Braces

  • Function definitions — open and close braces should be on lines by themselves. Do not put the open brace on the same line as the function signature.

Right:

void foo()
{
    // do stuff
}

Wrong:

void foo() {
    // do stuff
}
  • Other braces, including for, while, do, switch statements and class definitions — the open brace should go on the same line as the as the control structure.

Right:

for (int i = 0; i < 10; i++) {
    // do stuff
}

Wrong:

for (int i = 0; i < 10; i++)
{
    // do stuff
}
  • If/else statements — as above, but if there is an else clause, the close brace should go on the same line as the else. Also, one-line if or else clauses should not get braces.

Right:

if (timeToGetCoffee) {
    buyCoffee(&coffee);
    chugIt(coffee);
} else if (timeToGoHome)
    // comment on else case
    outtaHere = true;

Wrong:

if (timeToGetCoffee)
{
    buyCoffee(&coffee);
    chugIt(coffee);
// comment on else case
} else if (timeToGoHome)
{
    outtaHere = true;
}
  
if (timeToGetCoffee) {
}
else
  
// comment on else case
  
if (timeToGoHome)
    outtaHere = true;
Note: See TracWiki for help on using the wiki.