The art of coding

In this article i will talk about coding standards and code hygiene in general. This is a highly underestimated theme even though it's a great way to increasy both code effectiveness and understandability - especially when other people have to deal with our code.

Preface


While making constant evolution of your programming skills you shouldn't be forgetting about how to write your code. You should always assume that other people may have to work with your code and there's hardly a worse way to disgust other coders than by presenting a mess code. Even if you're totally on your own and there's no way other coders will work with your code, you will find considerable benefits by implementing code hygiene in your work. And the ground rule here is that you just can't build great applications on a mess code.

The basics of code hygiene


There are many resources on the internet that provide basic ground rules over some coding standards. Many of them differ, but you can choose which ones fit your needs and of course you will develop your own habits. Sometimes there's no space to do your own decisions here, but that's a different problem :) The key issue is that you use strict rules and that you're applying them precisely. To start being concrete I mean by this that when you're doing 4 chars tab spacing you should do it always and don't mix it with anything else. That includes paren policies, indent policies etc. So the first lesson here is - set an order to your code, whatever the order looks like. By implementing this rule you have successfully achieved that other coders looking at your code will after short time know how to read your code effectively.

Separate, separate, separate


One of the worst things you can face is a "minimalized" code. Demonstration:

for($i=0;$i<100;$i++){$cont.='somethin';}


Ok, this is really an ultimate anti example, but I've seen many such things with very small differences. Now how this should look like:

for ($i = 0;$i < 100;$i++)
{
    $cont .= 'somethin';
}


Very easy changes with a lot more readable code. Signs should be separated with a space, also keywords (not functions). While I'm here - very important thing - curly brackets should be always on a line alone, with nothing else.

Another important thing is indenting. I use tab width 4, which is result of an evolution since 2 chars just can't offer such separation and visual differentiation of code levels. But this is totally your call, when you're completely satisfied with 2 (or 8 :)) I can hardly argue. First of all all these things should help you.

Another great improvement is to separate code blocks (this is too underestimated within say functions ...). Example:

function something($data)
{
    $lol = array();
    $another = array();
    foreach ($data as $key => $item)
    {
        if ($key % 2 == 0)
        {
            $lol[] = $item;
        }
        else
        {
            $another[] = $item;
        }
    }
    return array_merge($lol,$another);
}


Here's a description of what a neutral person (which didn't code this) sees:

function something($data)
{
    lot of stuff
}


And here's how it can look like:

function something($data)
{
    $lol     = array();
    $another = array();
 
    ##
 
    foreach ($data as $key => $item)
    {
        if ($key % 2 == 0)
        {
            $lol[] = $item;
        }
        else
        {
            $another[] = $item;
        }
    }//foreach
 
    ##
 
    return array_merge($lol,$another);
}//something


I covered several things here, so explanation:

1, when you use equal signs - you can dramatically improve readability of a code by making the indents the same. You just see every line perfectly clean as if it was alone.
2, separating code blocks with a comment (doesn't have to say anything). In PHP I like ## since they are more visible than the regular one line comments.
3, very cool and helpful thing is to comment end brackets (or end tags in HTML) - I'll return to this.

So what do I see when I look at something like this ?

function something($data)
{
    i initialize things here
 
    ##
 
    i fill my output variables here
 
    ##
 
    i return stuff here
}//here ends my function


One last thought here. There's absolutely no point in sparing spaces - every parser for every programming language ignores extra spaces. And there's just so much to gain. Getting over excited is of course not good :)

The last thing I would like to cover in this block is making an order in the middle zones (blocks of logically different methods in classes etc). You can use many commenting practises here, it heavily depends on concrete needs. When you have to do custom comments for say documentation generators you have little choice. But when you're not limited by this and you want a clean and very easy way to help organize your code you can use this little helper I use for such situations. Example:

################################
#   Public methods
################################
 
public function aaa() {}
 
public function bbb() {}
 
################################
#   Private methods
################################
 
private function ccc() {}


This is again a PHP example using # comment, which is a great visual separator.

Commenting closures


I will demonstrate quickly what do I mean by this, so here's an example HTML code:

</div>
            </div>
        </div>
    </div>
</div>


And here's an analogical PHP code:

}
            }
        }
    }
}


This is of course a partial example, but such things are often seen. This with a combination of bad indentation policy leads to very poor readability. You can't tell what goes with what and even though modern development environments offer highlighting of relevant starting tags (or brackets) you can't rely on that. You don't know if other people working with your code have such environments. So here's a solution - again very easy and really powerful:

</div><!-- /flash-holder -->
            </div><!-- /logo -->
        </div><!-- /header -->
    </div><!-- /page-top -->
</div><!-- /container -->


}//if ( ...
            }//if ( ...
        }//foreach
    }//if ( ...
}//function name


This way, you always know what belongs where and how things work :)

Demo code


To see an example code covering all the stuff mentioned here you can look at the demo link on the top.

Final words


These are just some of the things you should know. I want to say that I don't claim these examples to be the only right ways. Take these just as tips for you, you can choose some or none of them to be your helpers. My only goal here is to inspire coders to organize their code. Because once you adopt code hygiene as a natural part of your programming life you will achieve much better effectiveness - for yourself in the first place.