in reply to Confessional: why I wrote bad Perl code.
Code was meant for personal use?
At some point someone else will find out about it, start using it, maybe even modify it, and then you'll be expected to maintain it. I write handy throw-away scripts all the time, but the problem is that I never throw them away, because they're handy. And I share them with people who know just enough Perl to be dangerous. Unfortunately, when they extend my handy scripts for themselves, they do it in the same sloppy style I used in the first place, because I thought it was just a throw-away. And since I'm the resident Perl programmer, I get to fix those scripts when they break.
Code was only a temporary solution or a patch?
As people use the code and want it to do more and more things, it will grow, one feature at a time, until it is an unrecognizable blob of spaghetti that gives you a headache just to think about. Again, you'll be expected to maintain it. I've spent the last couple weeks undoing many of the mistakes I originally made with a small program that was only supposed to do one simple thing once upon a time. I used a few global variables and a few sloppy subroutines with side effects in package main. Well, you can guess what the code came to look like. Dozens of globals, dozens of sloppy subroutines with side effects, all in package main. Total spaghetti. And it had come to be used for all sorts of things I had never envisioned. Ugh.
Too lazy to think any better?
I've come to see that writing sloppy code is false laziness. I may save myself a few seconds and some design effort in the short term, but eventually I will pay ten-fold in undoing entrenched sloppiness when the program grows. If I were truly lazy, I would never write sloppy code in the first place.
I think you've hit the nail on the head in identifying some reasons we write sloppy code. Of those, it seems that they can all be prevented with a little effort except for Tight deadlines, which are generally controlled by someone other than the programmer.
I am also tempted by Cleverness. It's fun to write one clever statement that could instead be written as three straightforward, easily maintainable statements. But later on, when I try to modify clever code I've written, I've often thought, "What the heck?" and been confused. I've done that enough times to realize that readable code is better than clever code, if you ever have to modify it. Thanks for bringing up this painful (but important) subject.
|
|---|