How to write sexy code

General Development

How to write sexy code, like a rockstar would


Date : 2009-04-11
Most of us probably either imagine ourselves as, or aspire to become Rock Star programmers. It takes a lot of work to get that kind of reputation but is definitely rewarding. Before going on we will have to define what a Rock Star programmer is. We're not talking about someone who has a great programming job, that's easy to get. Instead a Rock Star programmer is someone who always writes sexy code. Sometimes we refer to sexy code as “Elegant” but sexy can actually be a little less than elegant. For instance, using a dynamic script to serve custom CSS to different browsers is not elegant but it is down right sexy. The elegant solution can stand with it's head tall, it's above accusation. The elegant CSS solution would be to have 1 static CSS file that works on every browser known to man, and doesn't need changed when the next version of each browser comes out. Elegant is nice, but elegant solutions will not every make you reach Rock Star status.

Let's compare 2 examples of code in PHP:

Elegant:
Code:
for ($x=65;$x<=90;$x++)
{
    echo '<a href="/list.php?C=' . chr($x) . '">' . chr($x) . '</a>';
}

The above code works perfectly, it's type-safe, it's above reproach, it's Elegant. Oh, it makes a list of alphabetic links from A-Z that pass the selected character in on the C querystring variable.

Now, lets look at sexy code:
Code:
for ($x='A';$x != 'AA';$x++)
{
    echo '<a href="/list.php?C=' . $x . '">' . $x . '</a>';
}

Mmm... nice, you can actually increment a character in PHP? And, of course everyone knows that “AA” comes right after “Z” alphabetically. In PHP you can use single quotes in sexy code as there is a slight chance that it will run a little faster that way. On the other hand double quotes could look cooler.
Code:
for ($x='A';$x != 'AA';$x++)
{
    echo “<a href="/list.php?C=$x">$x</a>”;
}

Everyone knows that embedded values are cool, and escaped characters are cool as long as you're using a backslash. In ASP using 2 double quotes to make a single double quote is definitely a turn-off.

You know what would be really sexy? Check this out:
Code:
for ($x="A";$x <= "Z";$x++)
{
    echo '<a href="/list.php?C=' . $x . '">' . $x . '</a>';
}

Yep, that's HOT, but, unfortunately, while the developers of PHP allow us to use a character like an integer for some reason this code loops until “YZ”. Not Sexy Rasmus! (Tested on PHP 5.2.3 on IIS)

What is sexy code then? Sexy code is similar to elegant code in several ways. Both are fast, both are light, both will never produce an ugly error code. Where sexy and elegant depart from each other is that elegant code is going to be standards compliant whereas sexy code is allowed (sometimes encouraged) to take advantage of caveats of languages and platforms.

Sexy code, above all, has to look great. That means using great code indentation and perfectly crafted comment blocks. Take a look at the following examples:

First a very utilitarian C# default variable setup block.
Code:
    int _results = 20;
    int _countIncludes = 0;
    int _countExcludes = 0;
    int _totalPassengers = 0;
    int _legsCount = 0;
    string _name = “unnamed”;


Compare that to this smoking hot C# default variable setup block.
Code:
    int    _results          =  20;
    int    _countIncludes    =   0;
    int    _countExcludes    =   0;
    int    _totalPassengers  =   0;
    int    _legsCount        =   0;
    string _name             =  "unnamed”;

Nice, huh? I'm sorry VB.NET wont allow sexy formatting within VS as it's always tweaking with the formatting, but C# lets you go crazy. Of course, everyone knows C#.NET is much sexier than VB.NET. There are plenty of options when it comes to sexy code formatting, the basic test is “If one variable were to change would you have to fix the whole code block?” if so, that's probably sexy code formatting.

So, how do you write sexy code? The basic thing you need to remember is that someone will always end up looking at your code, it has to look great. When it comes to your methods and patterns you always want to go with fast and light, but you need to add style to your code. Make it where, when other programmers see your code, they go “Oh, nice”. Sometimes this means that your method is not clearly understood from the start. They have to read your comments then it all becomes clear. This is no excuse for writing bad code, it certainly can't be bad, but it can be difficult to undestand.

For .NET programmers a definite hint that your code is sexy is that you've used the XML documentation system so that your code descriptions and parameter extents show in VS code tips. Being a freelance developer I like delivering DLLs with all the code commented and documented this way so that when a staff programmer asks some question about how to use the methods and what the allowed values of parameters are, thinking he can get an extra day of sitting playing Wold of Warcraft instead of working, I can respond, “Just check the docs, it's all explained”.

Once again, the goal is to make other programmers say “Oh”, or “Ah”

So, if you really want to write sexy code the main thing to think about is what other programmers will think when they see your code. It might sound like that is a very superficial approach to programming, but honestly it helps to write better code when we realize that someone else will look at it. If we think our code will be hidden from prying eyes forever we may allow ourselves to get away with bad coding practices. We might let ourselves write something that will run slow or take up a lot of resources because it's easier to write. Writing with the idea of someone looking at, even critiquing our code will keep us in line, even when nobody is looking.

Comments :

No comments yet
  • Search For Articles