paul's code formatting

This is for C-like languages including Java, JavaScript, Perl, PHP, Ruby, etc.

  1. Use a programmer's editor that does automatic formatting, e.g., emacs.

  2. Keep all lines less than 78 characters long. (If you absolutely need to have a long line, consider putting a blank line above and below it, to make it easier to read.)

  3. Use two spaces for indentation level. (Wider indents would make more lines wrap.)

  4. Make all inline comments start in the same column when possible. I.e., in emacs try M-; to run indent-for-comment.

  5. Use a 65-dash comment to separate sections, e.g., for Perl:
    # -----------------------------------------------------------------
    # subroutines:
  6. Most subroutines should be less than about 50 lines of code.

  7. Usually put a space before any open parens, and after every comma and semi-colon, and on either side of built-in operators like the = sign.

  8. Do not put two statements on one line.

  9. Open brace at end of line like this:
    if condition {
      action();
    }
    

  10. Always follow an if/for/while clause with {} to wrap the actions, even if there is only one statement as the action.

See Also

paulenglish.com - articles - startups - nonprofits - press 05-Apr-2010