Good Coding: My Top 5 Pet Peeves
I thought I’d get warmed up here with the five coding practices I find most aggravating.
1. Non-existant Indentation
If you’ve ever encountered this demon, you know how difficult it is to figure out what’s going on without reformatting the whole thing.
Emacs makes this easy to fix with the M-Alt-\ command. Just hilight the offending section, use that command, and say hello to indentation.
2. Abandon all whitespace, ye who enter here
Some coders seem to be afraid of the spacebar. It won’t bite, you. Seriously, folks.
I find statements like this incredibly annoying and often difficult to read:
if($num_donuts>0||$num_cheetos>0){print"We've got snacks!\n";}
Compare that to:
if ( $num_donuts > 0 || $num_cheetos > 0 ) {
print "We've got snacks!\n";
}
3. Single-letter variables
If you are naming your variables with a single letter, and they are anything but a counter in a loop, and I end up having to maintain your code, I will curse you.
4. Nonsensical or non-existant comments
Comment your code. Please.
But make it useful. Comments like # I don't know why this works, but it does help no one.
When you’ve figured out a particularly tricky bit of code, take 5 seconds and explain it in a comment so that the next person (and that might be you 6 months from now) doesn’t have to spend an hour deciphering it all over again.
5. Embedding commands inside commands.
While technically feasible, this is a nightmare for maintenance coders.
my $result = join(',', get_animal_ids( $type, split(',', $list_of_cities) ) );
Better to do something like this:
my @cities = split(',', $list_of_cities);
my @animal_ids = get_animal_ids( $type, \@cities);
my $result = join(',', @animal_ids);
I could go on, but let’s just stick to 5 examples.
What bad coding practices drive you nuts? Share your peeves in the comments!
This relates to your #3: Lazy variable naming.
In my experience, this is a systemic problem. There’s a mass movement of programmers out there incapable of naming their variables properly. Drives me sodding barmy. I guess I shouldn’t be surprised: it can be really HARD coming up with that perfect name and people are naturally lazy. But taking the time to properly sum up a variable’s purpose can make the difference between mediocre code and kick-ass code.
Every once in a while I play video games with a friend of mine. He’s always joked that the hardest part of playing any game is coming up with a witty character name. But he’s PATIENT and what he comes up is usually worth the wait. I wish he’d program more.
[/rant over]
— Ben Mar 14, 01:30 AM #re: #3, I occasionally use short variables (one to three characters) outside of counter increments, but only for really temporary stuff (and even then, usually only inside of a function). I realize that will probably drive those that come after me absolutely mad, but there’s where #4 comes in :)
In fact, proper commenting will negate the ill effects of almost any convoluted code. That is, except when they don’t #&@*ing indent. Grr…
— Colin Mar 20, 11:11 AM #
