Develop sensible coding habits on the path to true Laziness

| 7 Comments

Whenever I walk in my front door, I walk straight to the kitchen, open the first drawer, put in my keys and wallet and cell phone, and close the drawer. Always, without fail, no matter how bad I might have to take a leak. It is no less convenient for me to do this than to drop my stuff any old place. It is a habit that I have maintained for close to 30 years now. And I never ever ever have to wonder where I put my keys, or my phone, or my wallet. Ever. Having to do so would be extremely inconvenient. In fact, it would be EXTRA brain cycles on my part to put them where they do not belong.

When the phone rings, I know where it is. I don't run around wondering "Where's my phone?" trying to triangulate position. It's always there in the kitchen drawer. Always.

I also always put on my seat belt in the car. I never wonder if it's "necessary". I just do. Thinking about if it is "necessary" is also inconvenient. It takes brain cycles that I don't need to spend. I also know that the one time I need my seat belt and I don't have it on, I'm screwed.

Trying to decide to save three seconds by not buckling my seat belt is false laziness. And so is worrying about when you can leave out warnings and strict from your Perl programs.

Yesterday in IRC, someone was lamenting that Perl 6 effectively has warnings and strict on by default. All variables must declared before use. This person was one of those programmers who tried for the premature optimization of saving some typing. He forgot that typing is the least of our concerns when programming. He forgot that programmer thinking time costs many orders of magnitude more than programmer typing time, and that the time spent debugging can dwarf the amount of time spent creating code.

He also forgot that he's a human.

Checks like warnings and strict are there because we are fallible humans. We type $totl instead of $total, and we want Perl to tell us that. We try to dereference scalars and since we're no longer using Perl 4, we want Perl to tell us about that, too. We want Perl, which is a program and doesn't get sloppy and forget things, to catch us when we are humans and get sloppy and forget things.

Buy and read The Pragmatic Programmer and Code Complete and develop a solid set of programming habits from them. Sensible habits that are followed even when you might be able to get away without them are the true Laziness.

7 Comments

Why isn't strict and warnings on by default in Perl in the first place? For those rare times when you don't want them you can use no warnings and no strict.

Why isn't strict and warnings on by default in Perl in the first place?

Forcing strict and warnings somewhere in the development chain of Perl 5 releases would have broken too much. Perl 6 gives a fresh starting point.

It's not Perl but it also saved my life a couple of time:

When you start typing a sql delete statement, start with the where condition, then go back to complete your from clause:

1) DELETE WHERE id = 93299284928
2) DELETE FROM t WHERE id = 93299284928;

Because I'm human, I once forgot the where clause and went through a world of pain. One can also argue we should enclose everything with a transaction..

One liners need neither warnings nor strict, in most cases..

Think of "perl -lne'/xx/ and print'" if you will

People give me a hard time for initializing all the variables, even though there's nothing in Perl that says Perl does it for you. This use to be a problem in mod_perl, but since there's nothing to say initialization takes place, this can change without warning.

Private Variables via my() in perlsub promises initialization of variables.

Nice article, Andy. I'll try that keys / wallet / phone tip!

Leave a comment

Job hunting for programmers


Land the Tech Job You Love, Andy Lester's guide to job hunting for programmers and other technical professionals, is available in PDF, ePub and .mobi formats, all DRM-free, as well as good old-fashioned paper.