Coding standards

markleeds wrote on Monday, June 27, 2005:

Here’s a small step towards improving code legibility:

1) Formatting of code blocks

I prefer starting a new { on it’s own line and at the same indentation level as the statement above that starts the block.

if (1)
{
    if (2)
    {
        print "2";
    }
}

The popular style of putting the starting { at the end of the statement on the same line, while favored by many, I feel obscures code legibility.  Some here may disagree with this.

2) Indentation

Most sources recommend against using tabs for indentation.  I am guilty of using them, but I have to agree that for several reasons, spaces are better.  People disagree on the ideal number of spaces for each indent level, but I think that 4 provides maximum legibility.

We should briefly debate these items and come to an agreement.  Various styles are to be found within OpenEMR, even in the same file.

markleeds wrote on Monday, June 27, 2005:

forget my example, sourceforge removed my leading spaces!

drbowen wrote on Monday, June 27, 2005:

We assume:

if (1)
{
4spaces if (2)
4spaces {
4spaces print "2";
4spaces }
}

markleeds wrote on Monday, June 27, 2005:

close:

if (1) 

4spaces if (2) 
4spaces { 
8spaces print "2"; 
4spaces } 
}

sunsetsystems wrote on Monday, June 27, 2005:

I don’t feel very strongly about it, but I often seem to write code with many levels of indentation, and normally use 2 spaces, or even just one!  Usually I use single-space indents in JavaScript code, as the size of a JavaScript module has a significant impact on performance.

– Rod <rod at sunsetsystems dot com>

sunsetsystems wrote on Monday, June 27, 2005:

By the way HTML should be indented consistently also.  For example:

<table>
_<tr>
__<td>

(imagine spaces where the underscores are)

– Rod <rod at sunsetsystems dot com>

andres_paglayan wrote on Monday, June 27, 2005:

yep, and adding \n for line breaks in the echos is also great to debug html output with ‘view page source’