Readable Code vs Compact Code

Inspired by a thread on Facebook to which I contributed, a few words concerning readable code vs compact code.

When writing applications, a common scenario involves setting a variable from some external source. What immediately ensues is a check to ensure that our variable has some meaningful value. With most languages, it should look something like this:

variable = foo();

if(variable == null)
  variable = somedefault;

It’s clean and semantically correct, but is it perhaps a bit verbose for such a simple concept? We could be clever and express the assignment/guard in a more compact form:

variable = (variable = foo()) == null ? somedefault : variable;

Behold, it fits on one line now! Note however the significant loss of clarity. From a performance perspective it’s no better than the original attempt. We definitely do not want this kind of code in our application, but perhaps the idea of sneaking the assignment into the if statement can be refined as such:

if ((variable = foo() ) == null);
  variable = somedefault;

Well this is an improvement, relinquishing one line to restore readability. Compacting the logic into one line with the trinary operator was just silly anyway but this code removes that syntax clutter. If the program requires decision logic, you might as well expose it clearly. Nonetheless it’s still not fully satisfying, having to multipurpose the if statement for assignment and conditional logic, even if we have shaved a line off the original code without affecting performance, and only minor loss of readability.

Ultimately it is the language that is at fault here, its lack of syntactic sugar resulting in verbose code, not the developer. If the code must be verbose to clearly express a concept in the language, so be it. Of course, don’t be verbose needlessly! An example of a language that has powerful enough syntax to simplify this assignment & guard coupling – Perl’s logical OR operator allows for a very convenient way of expressing what we are are trying to achieve, and it reads very naturally:

$variable = foo() || 'somedefault';

Coupling standard logical operator short-circuiting, weak-typed value interchangeability, and Perl’s logical operators returning the value of the last evaluated expression within, we can express powerful ideas naturally as shown above.

With PHP the assignment & guard code can be expressed similarly – the logical operators return strict booleans unlike Perl but the language allows expression to stand alone.

$variable = foo() or $variable = 'somedefault';

PHP’s version isn’t quite as clean but still crystal clear and compact without any effect on the performance compared to the original, which is what I’m trying to get at.

What you should take away is that as a developer do not be disdainful of verboseness if your code must be verbose. Of course, keep it as simple as it need be and certainly use the language to your advantage when you can, as we’ve done with PERL and PHP, but never sacrifice readability for so-called compact code – your fellow coders would prefer to understand the semantics of the application, and the computer is going to get a compiled version anyway.