Skip to content
This repository has been archived by the owner on Aug 18, 2018. It is now read-only.

Code Guidelines

jbarnette edited this page Sep 12, 2010 · 10 revisions

Guidelines aren’t laws. What’s important is consistency. Anybody’s welcome to change these, just get consensus and make sure all the existing code matches your new guideline.

Two spaces per indent.


// bad
if (something)
       do_something();

// good
if (something)
  do_something();

Put braces on the next line.


// bad
if (something) {
  do_something();
}

// good
if (something)
{
  do_something();
}

Don’t insert extra spaces in parentheses.


// bad
if ( (foo && bar ) || baz )
  do_something();

// good
if ((foo && bar) || baz)
  do_something();

Use consistent pointer notation.


// bad
Type * variable;
Type *variable;

// good
Type* variable;

Use vertical whitespace liberally.


// bad
bool do_something() {
  if (flag_p()) do_something_else();
  switch(type_of_thingy()){
  case FOO: handle_foo(); break;
  case BAR: handle_bar(); return false;
  default: handle_default();
  }
  if (flag_p()) do_something_else_again();
  return true;
}

// good
bool do_something()
{
  if (flag_p())
    do_something_else();

  switch (type_of_thingy())
  {
    case FOO:
      handle_foo();
      break;

    case BAR:
      handle_bar();
      return false;

    default:
      handle_default();
  }

  if (flag_p())
    do_something_else_again();

  return true;
}