wiki:CodingStandards

Coding Standards

Hacking by yourself is fun and straightforward. Team hacking on the other hand needs a bit of ceremony. Having a strict coding standard makes the code a lot cleaner and easier to read (after you get used to it).

NOTE: We enforce our coding standards. Don't be surprised if we rollback poorly-formatted changes.

Don't use things like print_r and echo for debugging, use the logging system instead.

Basic formatting rules

Here are a few basic rules for coding (more to come):

  • Only use '\n' for end-line in source code. Make sure you configure your Windows editor to use UNIX-like end-line! (for Windows we recommend gvim or EditPlus)
  • Indent with 4 spaces. Don't use tabs.
  • Always write {} for blocks, even for one-line ifs. This rule can be relaxed if there are a lot of if(condition) continue; or similar.
  • Opening { brace goes on the same line as the statement. The closing brace has the same level of indentation as the statement. This goes for EVERYTHING, including functions, methods and classes.
  • Code lines should be shorter than 80 characters. You can break this for html code.

Proposals:

  • Else is on the same line as the close brace: "} else {".

Naming conventions

  • PHP functions, variables: lowercase with underscore separators: do_something, do_something_else.
  • PHP constants: uppercase with underscore: SOME_CONSTANT, DB_USER.
  • PHP classes: first letter of each word capitalized, no separators: MyTextile?, SomeClass?.
  • URLs: Romanian, lowercase with dash separators: documentatie/conventii-formatare-probleme

These are proposals:

  • CSS classes/id: lowercase with dash separators: some-id, exponential-pager.
  • SQL: lowercase with underscores. Prefix with table names to simplify joins.

Other

  • Try to document every function/method. Keep it short, a few lines at most. We don't use any automatic documentation generation, so don't use a strict format.
  • Try to keep the code simple, no fancy tricks. If you need fancy tricks, DOCUMENT THEM .
  • PHP include/require/etc should use full paths: require_once(IA_ROOT . 'common/db/textblock.php');
  • Only use <?php ?> tags (no short tags)

Proposals:

  • Functions should be short. Anything above 50 lines is a bug.