Friday, 20 July 2012

Code Readability

Or why I use ternary operations but never single line if statements.

This is a massive issue on collaborative projects. And since virtually every single piece of code you use will need to be modified / understood by someone else in the future, in my opinion everything you write should emphasise the readability rather than functionality.

Of course there will always be projects where speed is important, and you want to use the data structure that is fastest / most efficient.

As a professional software engineer (even if it is "just" websites) I am constantly working with other people's code. And it frustrates the hell out of me that people write code that is just so hard to understand.

A ternary operation is fairly trivial for any professional to understand. Even there though, the use of a variable name that explains where it comes from would be so handy!

One thing that annoys me though is single line if statements. Most IDE's allow you to format everything properly according to your conventions (we use Zend), and if everyone follows those it makes it much easier to follow the code when you have to fix a bug. But scanning code by eye for a line (I'm lazy like that) is made so much harder with single line if statements. Especially when you're checking for a value like: $_SESSION['carhire.search.dropoff_location'] .

The number of studies that have been done on the width of columns for readability is astonishing. Made your code more readable by using the following format always:

if (statement) {
    then do this
}

And comment your bloody code or I will curse you forever.

No comments:

Post a Comment