[OpenLayers-Dev] coding style best practices

Paul Spencer pspencer at dmsolutions.ca
Mon Oct 1 16:28:15 EDT 2007


I am not a particularly fastidious coder but I do strongly believe  
that folks should be making their code readable and, as much as  
possible, avoid common sources of bugs.  Also, for those folks  
looking to make contributions, it is going to be much easier for the  
core maintainers to bring your code into trunk if it already fits the  
coding style of OpenLayers.

I am sure Chris will have more to say on this, but my particular pet  
peeves are:

1. Not using { } around code associated with if statements.  For  
instance, it is legal in javascript to write the following:

if (foo)
     return true

Lets say you need to do one more thing in this case, increment foo:

if (foo)
     foo++
     return true

This will no longer work as expected (its not python!).  I highly  
recommend using brace brackets around all conditionals.

2. Not using whitespace to make code readable:

if(foo<2||foo>5)return foo+12
else if(foo>7)return foo-1
else return foo

should be:

if (foo < 2 || foo > 5) {
     return foo + 12;
} else if (foo > 7) {
     return foo - 1;
} else {
     return foo;
}

or some variant of this.  The code compressor can make it unreadable  
later.  Right now, I need to be able to understand the code when  
reviewing it!

Cheers

Paul

+-----------------------------------------------------------------+
|Paul Spencer                          pspencer at dmsolutions.ca    |
+-----------------------------------------------------------------+
|Chief Technology Officer                                         |
|DM Solutions Group Inc                http://www.dmsolutions.ca/ |
+-----------------------------------------------------------------+








More information about the Dev mailing list