Skip to Content

More on coding standards

Posted on 4 mins read

After I wrote a teaser on coding standard, I found this article saying that Style is symmetry and structure! I’ve allways felt that Sun’s standard was hurting my poor brain but I couldn’t say why. Now, thanks to this article, I know why. Symmetry !

When one has to impose coding standards, good reasons are mandatory. Rules need to be set in purpose. A proper rule can improve readability and code sharing, or ease debugging, or leave less room to bugs.

Symmetry is the number one rule to improve readability. You can also add whitespaces to your code because they help your brain to understand the structure the code.


is less structured than :

result = ((methodCall()*2) + (1-methodCall()) ;

Of course a modern IDE will had color to this.

<b><span style="color:#7F0055;">boolean</span></b> result = ((<span style="color:#7F0055;">methodCall</span>()*2) + (1-<span style="color:#7F0055;">methodCall</span>()) ;

To ease debugging, a very simple rule taught to every beginner : don’t put two statements on the same line :

argv++; argc--;

When stepping into code, you really want to know which part of the line you are debugging.

It’s been some times that I write if test like this :

if (3.14 == Pi) {...}

instead of :

if (Pi == 3.14) {...}

to avoid :

if (Pi = 3.14) {...}

which is always true (in C/C++). With a modern IDE, it is however not very useful anymore since the IDE can detect things like those.

I like to give prefixes to parameters :

<b><span style="color:#7F0055;">public class</span></b> Test()
	<b><span style="color:#7F0055;">private final int</span></b> info ;

	<b><span style="color:#7F0055;">public</span></b> Test (<b><span style="color:#7F0055;">final int</span></b> anInfo)
		info = anInfo ;

It is also a good idea to make the parameter final to avoid changing the value of the parameter instead of the class variable.

comments powered by Disqus