Coding Style

This is a brief description of the coding style used in Event-Calendar project. Personally, I (Alex) dislike the WordPress coding style, so I use my own – described here.

Names

All names in the global scope should be prefixed with “ec3_” to ensure that we don’t conflict with other code.

Function and variable names should be all lower-case, with words separated by underscore characters. Don’t make excessive use of abbreviations – extra characters cost nothing, and longer names help readers to understand the code.

  ec3_global_function()
  member_function()

Class names should start with a capital letter, and continue in lower-case. New words should be marked by a capital letter. Class names should not contain underscore characters. (CamelCase).

  ec3_MyClass

Name hook functions like this, ec3_TYPE_HOOK(). If you use the same hook more than once, then add a distinguishing name to the end, ec3_TYPE_HOOK_NAME(). For example:

“parse_query” filter: ec3_filter_parse_query()
“init” action: ec3_action_init()
“the_content” filter for big-calendar: ec3_filter_the_content_bigcal()

Indentation

Use two spaces to indent blocks of PHP code. Never ever use tabs for indentation.

Keep lines shorter than 80 characters. There are a few cases where longer lines are acceptable – HTML can become highly indented, so sometimes it’s better to allow it past 80. Gettext strings shouldn’t be broken up, so long ones will have to go past 80.

Use Allman-style braces:

  while(x==y)
  {
    something();
    somethingelse();
  }

Simple, single-line blocks may omit the braces. But then always follow with a blank line.

  if(x<0)
    something();
  else
    somethingelse();
  
  morecode();

If function arguments must be listed over more than one line, then do it like this:

  // Function declaration
  function my_func(
      argument1,  // double-indent
      argument2
    )             // single-indent
  {
    ...
  }

  // Function call
  my_func(
      argument1,  // double-indent
      argument2
    );            // single-indent

Try to keep HTML vertically aligned with the PHP. Indent HTML blocks by one space.

and also...

Don't put unnecessary white-space before open-parentheses.

good: if(true)
ok: if( true )
no: if ( true )

Don't confuse assignments with expressions.

no: if( $x = get_x() ) ...

Final Word

I believe that it's important to keep coding style consistent within a file, but I'm less concerned with what that style actually is. So, while the code is formatted my way, contributors must learn to do likewise. If someone were to reformat the whole thing so that it conforms with the main WordPress code, then I'd consider the patch - even though I prefer my style, I believe that it's even more important to be consistent.

Leave a Reply