coding laziness

Mar 24, 2008 14:38

I know that I'm kind of a code/development snob. I might even be something of a hypocrite, but don't really know it because no one has ever called me out on it. It still irks me when I see some of the practices and "standards" that people use. A lot of it comes down to laziness, which is something I understand, but I also know that a little extra effort up front makes for an easier life later. Not only that, but it can really help if someone else has to dive into your code. You could be selfish and say "so what?", but the opposite is true too. When you have to dive into someone else's code and it is trash, you quickly take their name in vain.

One of my biggest peeves is readability, or lack thereof. There are a lot of ways to make code more readable without taking much effort. I don't know what people have against the spacebar, but it really is your friend, especially surrounding operators ("=", "+", etc.) The Tab key is also pretty nifty when it comes to making things readable. Similarly, use that Shift key! Mixed case is much more readable than all lower case. People complain that it takes too long to type. Bah! I'll argue that if you do it for a little while, you won't even think about it later. Even for the hunt-n-peck typers, you will learn to adapt. You might become "hunt-n-peck-plus-pinky". Additionally, most code editors have auto-completion (a.k.a., "intellisense") built in. Use it.

Another thing that irks me is lack of consistency. This is more prevalent with database queries because SQL is pretty lax. Within a single statement you might get sections that are all lower case, all upper case, and mixed case. You might get 5 comma delimited values in a row without spaces near the comma, then a few with the space after, and even some with the space before the comma.

Oddly enough, there are often cases of mass consistency that bugs me, but I'm talking about the Copy-Paste crowd. If you're going to re-use something over and over, make sure you do it nice once and then copy it. I was recently working in some SQL code that had a block replicated perhaps two dozen times with only a text message being different. It bugged me that all four lines of code had different indention and one of the lines had a bunch of extra spaces on the end of it for no reason.

There is an adage that I've used for well over a decade now: Don't trust your data. In other words, you never know where your data is coming from so be ready for anything. I'm surprised to find people that have been doing this nearly as long as I have that still don't follow that. I still find code that blows up when you give it blanks, but a character in a number field, or stick in an odd apostrophe. There's really no excuse for much of that.

I think the thing that takes the prize (for this rant) are people that duplicate code in multiple places. If you're doing anything even remotely complex or lengthy, make a common function for it. In a current project there are a set of 4 pages that are 90% the same. Each page has its own set of code (over 400 lines), most of which do the exact same thing. Make a freaking function. This stuff is Programming 101! This applies to the previous paragraph too. Write a set of validation functions and pass all your data through them. I've got a little library that I've used and adapted since I started coding professionally.

Related to "Don't trust your data" is "Never say never/always". I'm on a project that takes in 2 numbers (among other things). They are stored as 2 fields in the database in one place but are later combined into 1 (with no way to tie the two sets of records together). "They are always sent together to the client." Well, guess what?! They won't be from now on. Now I've got to come up with some convoluted way to pick the values apart. This also falls into the "It is better to store too much detail" category. You can always filter or aggregate it.

To my defense, even though I think these things, I rarely say anything. We all have our styles and methods. I'm betting there are people that find my code annoying. So I try to make myself available for questions and often offer up things like my code libraries or keyboard shortcuts to make everyone's life simpler. I've had a few takers and they've all said that it has made life easier. Sometimes it is hard to not slip from helpful to pushy freak. (I know I'm the latter, but try to appear as the former.)

I could go on, but I've got work to do... fixing other people's crap.

programming, rant

Previous post Next post
Up