The power of lazy programming

These 13 tools and techniques prove that, when it comes to coding, laziness is a virtue

Page 2 of 2

Code reuse

Only suckers start from scratch. In fact, today I took out some code I wrote over the summer, changed five lines, and started it running again. Woo hoo. It was sitting there in a code repository waiting for a chance to live again.

Smart developers reuse code as often as they can. That was one of the main goals of the open source movement. It wasn’t freedom; it was laziness. If we reuse our code, we save a gazillion hours of work.

Flexible code

Before I immerse myself into programming, I like to take a nap -- not a full trip to dreamland, but a gentle excursion that gives me the time to imagine everything the code might do. It looks like I’m sleeping, but I’m planning how to make the code as flexible as possible.

This laziness always pays off. Good programmers usually try to build flexibility into their code in case they need to change parameters or details in the future. Naysayers will complain that adding flexibility may make the code longer. They take some shortcuts that are often short-sighted. Adding flexibility by building in more parameters and splitting up procedures doesn’t cost that much time. Yes, it adds lines and expands the size of the file sizes, but those extra key clicks always pay off later when someone wants to change the code.

The key is to understand the code and architecture well enough to know when to add the extra flexibility. When this becomes instinct, you’ll have a way to be lazy and efficient at the same time. And usually a nap helps.

Documentation saves questions

While it is a well-known fact that writing is hard work and writers are never appreciated enough for their ability, programmers write out of laziness. They create documentation about the code so that others won’t bother them. They don’t want to field questions from the great sea of fools, so they write up good man pages explaining what the switches do. Sure, it takes a bit more work to write clearly and crisply, but it more than makes up for it in the long run when people leave you alone.

Sparse comments

Of course, when it comes to commenting, often less is more. Yes, you’re supposed to mix in a few words from your favorite human language to make it easier to understand the computer language. In fact, contracts often include requirements that withhold payment if there are no comments.

But being lazy about comments does force you to produce clean, simple code based on straightforward logic. Sure, if you’re doing something weird or hard to grasp, leave a note explaining your burst of genius. But should you really be doing something weird and hard to grasp? The best solution is to spend a little nap time pondering a very clean architecture. Then you won’t need to write many comments or spend hours updating them.

As with the constants mentioned above, longer variable names mix documentation into the code. This documentation follows the variable everywhere it’s used, unlike comments, which are merely attached to the declaration.

Automated testing

When you’re creating amazing code with only a few keystrokes, who has time to double and triple check to make sure it works? And how can anyone validate whether the new code isn’t breaking the old code? Enter the unit test -- the lazy programmer’s godsend for automatically making sure the code still works.

Some people say the greatest thing Sun did when it created Java was build millions of unit tests. Oh, the software is wonderful, but the unit tests make sure it keeps working in a predictable and unsurprising manner.

Automated metatools

It’s not only automated housekeeping like garbage collection, and it’s not automated devops tools like Puppet and Chef alone. There are now great metatools that merge all of the housekeeping chores into one big system. The logo for Hudson and Jenkins is a goofy cartoon of a butler, a perfect symbol for a system designed to organize all of the scut work around code -- while you kick back with a cold brew.

Once you check in the code, these automated metatool systems handle all the other tasks, like running unit tests, backing up the code, and even deploying it. It’s like the ultimate assistant for a lazy person -- and that’s why these so-called continuous integration tools are the ultimate assistants for programmers.

Compilers

Many of the basic tools we take for granted started out as inventions of the lazy. If it weren’t for compiler creators, we would still be stringing together machine op-codes and trying to remember whether that intermediate value was stashed in register two or three. Heck, if it weren’t for the inventors of mass storage tools like paper tape or magnetic tape, we would be flicking in these values through those front panels, the ones with flashing lights and switches. They look so cool in movies but were a pain to use.

Pretty much all of computing has been a quest to create a new set of tools to make our lives easier. Almost every tool or app began out of frustration. It’s been a long journey from the beginning of the internet to Snapchat, but every step along the way has been a short exercise in laziness. In a few years we probably won’t even have to use a keyboard to program.

Related articles

| 1 2 Page 2