If you’re reading this, either your curiosity got the best of you, or you honestly think I have something you might be interested in reading about. Whatever managed to bring you here, let me preach to you for just a moment.

For most of my “coding” life, I have subscribed to the idea that conforming to a standard is a good practice. It began with an unhealthy obsession with W3C Validation and making a point to ensure that each static file I created validated, with the mindset that if my markup validated, my work would display exactly as intended across all browsers. We know now that that was a naive notion.

Nevertheless, I became an XHTML-strict evangelist, validating sites everywhere I went, and chastizing those whom were found to be “buggy”. But in reality, It wasn’t about ensuring cross-browser compatibility, it was about sticking to a standard, and I saw a need where others didn’t. Or maybe they just didn’t care to look.

But let’s not belabor the topic. What I’m really after is codestyle in general. It doesn’t matter language you write in, whether it’s Java, or PHP, or even plain HTML. Codestyle is important, and if you don’t understand why, you need to do some research and learn about it.

Compare these two (primitive, for the sake of brevity) snippets:

<div class="some-class-here"><p>Some text goes here</p>
<p>Some text goes here</p>
<p>Some more text goes here</p></div>
<div class="some-class-here">
    <p>Some text goes here</p>
    <p>Some text goes here</p>
    <p>Some more text goes here</p>
</div>

Someone who isn’t concerned with codestyle would say “What’s wrong? They both look fine to me.” And syntactically, he or she would be correct. But I would venture to say that this line of thinking sows a seed for disastrous code in the future. Am I being excessive? Why make such a big deal about tabs and spaces?

Another example, just for kicks:

$array=["an element"=>24,"another"=>false,"and_a_third"=>"stringy"];
$array = [
    "an element" => 24, 
    "another" => false, 
    "and_a_third" => "stringy"
];

Again, primitive, but it illustrates a point. A commonplace argument might be, “Why take five lines when I can do it in one?”  Sure, I’m all for saving space where it makes sense to. But more and more, I am taking the path of readability over brevity. It burns me up to see code written with wanton disregard for codestyle.

“But you’re making a blanket statement that doesn’t apply to all languages!”

How so?

“JavaScript should be compacted to detract whitespace, because it makes the file smaller, which means it loads faster.”

And you probably need to consider upgrading your 56.6k modem, buddy. In all seriousness, I agree. Javascript can be obfuscated, and minified down to a single line of thousands of characters. And the only reason we do that is so that it makes it easier for browsers to swallow. But are you using Gulp or Grunt to do that for you? If you are (and you should be), then you should want your source code (not your build code) as easy to read as possible. Add whitespace to break things apart. Space out your operators and line-break arrays. Comment where it makes sense.

Why am I being fussy? Let’s use an example. Say you maintain a codebase that is hundreds of thousands of lines, and you have been the sole developer for a year or so on this one project. It’s quite possible that things have changed through the lifespan of the application, but your methods are consistent. You follow a standard. Now introduce two new developers to the mix, who start committing code that doesn’t follow the same standard. Suddenly you have visibly different code, and you’re having to go back and make adjustments. “Get new devs” you might say. Not the best choice.

Maybe it’s my fault. Maybe I’m territorial and obsessive to a fault. But just maybe it’s worthwhile to care about the details? Do yourselves, your coworkers, and your colleagues a favor, and pay attention to the tabs and spaces.


Extra Credit

Want to find out more about codestyle? Check out these super-awesome links:

  • PHP-FIG – Some might ask why I choose PSR2 over WordPress PHP codestyle standards, and to those, I offer only blank stares
  • WordPress JavasCript Standard – This one makes a bit more sense, but of course there are several out there
  • W3 XHTML 1 – I include this only to point out syntactical basics. But the thing about tabs and spaces should not be lost on the HTML folks

At the end of the day, it truly doesn’t matter what codestyle standard you choose to adhere to. What does matter, however, is your absolutely imperative decision to do so. Chaos is only cool in mathematics.