Saturday, January 31, 2009

On Laziness and Programmers

As programmers, laziness is presented to us as a ideal form to which we must aspire. I have a t-shirt with Larry Wall's famous quote written out in Perl code, "The three chief virtues of a programmer are: Laziness, Impatience and Hubris." And this is the guy who wrote a hugely popular programming language. Linus Torvalds openly admits to being lazy too, "I'm basically a very lazy person who likes to get credit for things other people actually do." And he wrote an operating system as a hobby. These two computer titans aren't just admitting to being lazy, they're crediting laziness as part of the success. Surely, being a better programmer means learning how to be lazy in a better way.

So why lazy people ruining all my projects? Reflecting on my last 10 years, lazy programmers have contributed the most to making polluted, convoluted messes of our shared code. How many times have you started on an existing project and said, "this should just be rewritten"? Sure there's a million constraints and variables at play on a project, but you can always blame the previous developer at least partly, if not wholly.

My first language was C++. The easiest path to reuse in C++ was multiple inheritance. Need some piece of code shared between objects? Simply extract a superclass and subclass it willy-nilly as needed. This leads to the diamond inheritance anti-pattern of course, and eventually my code was a mess. Laziness wasn't my friend here; I needed foresight to see the long term ramifications of my design decisions.

Later I learned PHP. My pages started off simple, and it was easy to put PHP, HTML, and SQL all within a single file. I kept pages small by extracting and including shared modules. The millions of code samples on the Web made PHP easy, in fact I never even bought a book to learn it. Six months later the project was a tangled mess of includes and untestable mixed-source files. Why? When I started the project I'd never heard of Pear::DB or the Smarty templating engine. I was lazy, I did no research, and I lacked knowledge of any of the standard PHP frameworks at the time. Oops.

Today I mostly program Java. There are more modern languages available, but the community, tool support, and consistency of Java make it a joy to use. Need an ORM framework? Pick one of 4 that have books published on them. Need to extract an interface? Ctrl+Alt+I. Need to work on a 10 year old module? The syntax and idioms haven't changed that much. Need to share code between two objects? The easiest solution of the lazy developer is almost universally bad: make a public static method. The next easiest solution is to move it into a superclass and tweak the inheritance hierarchy or create an adapter. The generally right thing to do is difficult: use composition so the two objects share a dependency on the new one, and use dependency injection to give the objects references. Just because Java makes it hard doesn't mean it's not the right thing to do. It takes discipline not laziness to keep Java code clean.

Still think laziness is a virtue? Being a craftman has nothing to do with being lazy. Anyone can be lazy, and for the most part if leads to crap. Larry Wall and Linus Torvalds aren't talking about run-of-the-mill easiest-way-out laziness. I'd say they're talking about knowing how to put forth just a little effort so that you don't have to see the problem again... they're talking about using foresight, knowledge, and discipline:

Foresight - To anticipate what today's decisions will become tomorrow
Knowledge - To learn, use, and apply the best work of others in the community
Discipline - To use foresight and knowledge to write the best code possible

Foresight, knowledge, and discipline have nothing to do with laziness; rather, they are traits of intelligence. Laziness is not something to which we should aspire. Instead of laziness, how about we aim for intelligence? Can we agree on that? Intelligence works for me if it works for you.

"Intelligence is the ability to avoid doing work, yet getting the work done." -Linus Torvalds

2 comments:

Scott Peacock said...

I wish I could cite the source, but I've heard it said that there are three kinds, or degrees, of laziness.

Lazy: I don't want to do this so I'll hack it, and it will be done as quickly as possible.

Lazier: I don't want to do this so I'll do it right away and get it over and done with.

Laziest: I don't want to do this so I'll write a tool and I'll never have to do it ever again.

Yes, discretion and foresight allow you to be better at being lazy. Those of us who are lazy and incompetent keep creating extra work for ourselves. I would say, however, that laziness and competence are largely independent.

Hamlet D'Arcy said...

I think those categories are about perfect. Thanks!