in Search
Welcome to Neopoleon - Sign in | Join | Help
Navigation: Home | Forums | Galleries

"Paper bag" code

[Note: Steve Maine has a wonderful post on "Jedi coders" that focuses on the ability of great coders to do great conceptual work - I consider this post to be a compliment to Steve's, but covering the more superficial aspects of coding. A great coder, in my opinion, is great from top to bottom - being able to work out an elegant solution to a problem isn't the same as being able to code elegantly, but I think both are important qualities of a great coder.]

I was driven out of the apartment early this morning by a malfunctioning furnace. The temperature within the walls dropped to approximately one eighth of (my body weight + my IQ) last night (figure that one out, you bastards), and I thought I'd set out for someplace warmer like the walk-in beer cooler at the convenience store down the street.

Got a little sidetracked, forgot about the beer cooler (hey - I am a bit hungover here), and found myself heading for the downtown Stumptown Coffee (free WiFi). On the way, I was watching the way people were behaving in traffic.

They were, if you must know, behaving like pigs. They were cutting each other off, not using their signals, driving too slowly, driving too fast, switching lanes where it wasn't legal, running lights, rolling through stop signs, and just generally making a mockery of the rules and regulations outlined in the Oregon DMV manual.

This got me thinking about coders.

At the last Portland Nerd Meeting, there was a lot of talk on the subject of "code aesthetics." We didn't call it that at the time because nobody had managed to be as pretentious as I was just now, but that's really what we were discussing.

We talked about casing, commenting, variable naming, and laziness.

I don't know about you, but when I drive, I generally try to do my best to follow the rules. I signal when I'm supposed to, let people in when it makes sense to do so (when I won't hold up 20 cars behind me to let one person pull onto the road), let pedestrians cross at crosswalks, stop at the lights, and so on. I might be totally off my rocker, but it seems to me that traffic jams, road rage, and other scourges of the traffic system have a lot to do with the general populace's inability to follow a few simple rules. I do my part to try and make life on the road easy, even if I'm having to work harder, and even when most of the people around me aren't making the same effort.

Not to sound like I'm patting myself on the back or anything (I have plenty of other posts in which I've done that), but I understand that the road doesn't belong to me, that I have to share it with other people, and that I have the choice to make their lives easier rather than harder (and harder on myself in the process). I don't get into my car, tear down the street, honk at people who have right of way, flip people off who signal to get into my lane, or anything of the sort.

So why should it be any different while coding?

I've encountered so much "paper bag" code in the past few years that it makes me want to cry. "Paper bag" code, by the way, is code that's so ugly that I want to put a paper bag over its head.

Here are some examples of "paper bag" code - this stuff is the equivalent of cutting people off on the road, running over pedestrians, and not pulling to the side when emergency response vehicles need to get through (a lot of these were brought to mind by Jason Olson during a very intense, very entertaining conversation):

Code that doesn't follow generally accepted and practiced style guidelines

This is a biggie for me. It might seem like it's a petty concern, but imagine that you're coming into an existing C# project and you see something like this:

    SomeVariableName.SomeMethodName();

I don't know about you, but my brain immediately causes me to believe that I'm looking at a static method call. When I'm coding in C#, I don't equate Pascal casing with instances of a type. Per the style guide, this is what I expect:

    someVariableName.SomeMethodName();

This makes sense to me, and probably would to the majority of C# coders out there, as well.

The danger of ignoring a rule like this is that you're going to confuse the hell out of the developer who's inheriting your project. I recently worked with some code like this, and five minutes into looking at it made the assumption, based on all the Pascal casing, that someone had put together about 500 libraries with nothing but static method calls in them. That's not cool.

On another note, during my recent move, one of the first things I threw out was the book "C# Design Patterns." Many of the examples were obviously just Java that had been "ported" to C#. This wouldn't have bothered me so much, but the author didn't even take the time to properly case anything, going so far as to leave methods camel cased as they were in the original Java examples.

Screw that.

Comments that clutter

How many times have you seen the following:

    // gets the user's first name from an instance of the user class:
    name = user.FirstName;

Yeah. Thanks for the comment, pal. I needed it, 'cause I'm TOTALLY FREAKING STUPID.

If you need comments like this, and if someone is actually paying you to code, then please drop me an email so that I can find out where you work and come in to perform what I call a Citizen's Firing. I realize that I lack the authority to officially relieve you of your duty, but that's a minor detail we'll worry about later.

Pointless laziness

There is some laziness that's OK.

Take this for example:

    for (int i = 0; i < 10; i++)
    {
        ...
    }

Is anybody going to have a problem with the variable name "i" here?

Possibly, but we're talking about a very short lived variable here, and I think that the uber-crappy variable name "i" is perfectly justified in this case.

However, take this JavaScript code that I saw recently:

    var t = this;
    var v = navigator.appVersion.toLowerCase(), u = navigator.userAgent.toLowerCase(), n = navigator.appName;
    var d = document;
    t.ua = new Object();
    t.ua.mac = (v.indexOf("mac")!=-1);
    t.ua.win = (v.indexOf("win")!=-1);
    t.ua.nn = (n=="Netscape");
    ...

I mean, c'mon - How much clarity did we trade for saving a couple of keystrokes here? Is it really that hard to type "this"? Or to name a variable "version" instead of "v"?

Crap, I say! That's some raunchy code. Check out the last line before moving on. Notice the triple-whammy of an insult with all the poorly named variables. This stuff makes my stomach turn.

Whoever did this, wherever you are, I hereby perform a Citizen's Firing on your lazy ass.

I would never pass something like this on to another coder.

Where'd the vowels go?

Jason Olson brought up another great one during dinner.

Let's say that you have an variable called "factoryWall" (it's just an example, my friends).

For the sake of clarity, it's easy enough to type "factoryWall" for each call, right?

That's what I think, too, but evidentially some people would rather write something like "fctrWll" to save a couple of keystrokes. Never mind the instantaneous obfuscation of code that takes place upon doing this - milliseconds were saved by turning a blind eye to readability, maintainability, and a few other words that end in "ability."

I realize that some of you are ex-mainframe coders, but this is the 21st century, and times have changed. Looking at some mainframe code, I've determined that there must have been a great shortage of vowel keys back in the early days of coding, but they're in perfect abundance now. Use them! I don't know where all the new vowel keys came from, don't know who had to die to bring them to us, how many vowel key trees had to be chopped down to accommodate our desires, but don't let these sacrifices seem to have been made in vain.

That's just the beginning

These few examples just barely begin to cover all the ways in which you can, figuratively speaking, cut off your fellow coders. I'm sure there will be a few other pet peeves added in the comments list.

I just feel that if you really care about what you do, and if you really understand that other people will have to work with your code at some point, it's a fine thing to put in the effort to help make your fellow coders' lives suck a little less.

Published Friday, December 19, 2003 1:04 AM by Rory

Filed Under:

Comments

 

Patrick Cauldwell said:

I totally agree on the point about inane comments. I try to follow the guideline set down in Martin Fowler's "Refactoring". If you feel you need to write a comment, refactor instead because your code should be more obvious.
December 19, 2003 1:39 AM
 

Jason Olson said:

I couldn't agree with the concept of Refactoring more. Another pet peeve that I will lay out there is commenting a 400-line section of code by surrounding it with an "If 0 Then" statement. Man, that one really fried my goat's milk, let me tell you!!!!
December 19, 2003 1:45 AM
 

Patrick Cauldwell said:

That's definitely annoying, although I tend to see while(false) rather than If 0 Then. Probably just linguistic differences :)
December 19, 2003 1:53 AM
 

Justin Rudd said:

Hmmm...guess I'm guilty of the if 0 stuff. Not with real code, but with a C++ compiler I used to do #if 0 if I was trying out new implementations and I didn't want to lose the old one.

Sometimes I forgot and left it in...

As for your

SomeVariableName.SomeMethod()

What happens if you are in a class method and SomeVariableName is really a property? Does it still bug you? Or do you use the variable and not the property inside of methods?
December 19, 2003 2:42 AM
 

hurcane said:

Here's a pet peeve of mine, and it comes from the primary project I've been working on.

Class: SalesOrderLine
Some properties:
SalesOrderNumber
SalesOrderLineNumber

Those two properties happen to be the composite key to the class. Why not properties of "Order" and "Line"? I'm not so lazy to use single letter variables (except as loop counters), but has this not gone to the extreme?
December 19, 2003 2:52 AM
 

Rory said:

Justin -

"What happens if you are in a class method and SomeVariableName is really a property?"

No biggie - If you see a bit of this, then you can figure it out.

What I saw was Pascal cased instances strewn all over the place, which is very different.
December 19, 2003 2:55 AM
 

Rory said:

hurcane -


"Why not properties of 'Order' and 'Line'? ...[H]as this not gone to the extreme?"

That's one of my peeves, too.

I was guilty of it, though, before realizing it. I remember that, somewhere in the past, I had a class like this:

Class: Customer
Prop: CustomerFirstName
Prop: CustomerLastName
etc...

I remember realizing my stupid mistake, and that's when I learned not to do this.

That was back in the VB6 days, though...

But, yeah. It gets me, too.
December 19, 2003 2:58 AM
 

milbertus said:

Amen! Rory, I totally agree with you. I can't tell you how much crappy code I've seen at work that does all the things that you mentioned, and so many more. Maybe if you stop by Michigan in your tour that's coming up, you could stop by and perform some Citizen's Firings for me? ;)

This is my favorite snippet:
// Get the Apc Project
//
m_pApc->GetApcProject(&pApcProject);

[sarcasm]Pure brilliance.[/sarcasm]
December 19, 2003 12:42 AM
 

Andy said:

Couldn't agree with you more. I hate seeing variables like vm_nn that one was for a Vector Matrix of the order N x N. I think legacy embeded code written in C and just about everything I've ever seen in Fortran is the worst for this.

Where it gets hard for me is this: 80% or better of what I do is computational geometry written in C++. When you get a function of the order of:
x=s*x’*cos(?)-s*y’*sin(?)+k
y=s*y’*cos(?)+s*x’*sin(?)+h

and you need to turn it into code what do you name those variables so that those who come after you know that you were transforming coordinates from one coordinate system to another?

I do the best I can with the naming convention but there is a time and a place for good commenting skills too. I find with programs written for things like databases I have much more descriptive names for variables and less commenting but with functions that are purely computational I do far more commenting.

I hate being handed legacy projects with no comments and single letter variables. It's just not cool.
December 19, 2003 3:25 AM
 

Andy said:

oops sorry the question marks should have been theta symbols but they didn't come through.
December 19, 2003 3:28 AM
 

Steve Maine said:

Agreed, agreed, agreed.

One thing I hate is when code that's supposed to return an array or a collection returns null when that collection is empty -- the propert thing to do is return an empty collection or zero-length array, so callers can foreach over the return value without doing an explicit null check.

I'm all for long variable names. I think Intellisense auto-complete makes the extra clarity worth it. The one thing I hate, though, is when people have really long variable names -- complete with spelling errors (ie, WebServiceMessageReciever). When I actually spell it right, my code breaks. Grr.
December 19, 2003 3:40 AM
 

Scott Hanselman said:

In this example:
var t = this;
var v = navigator.appVersion.toLowerCase(), u = navigator.userAgent.toLowerCase(), n = navigator.appName;
var d = document;
t.ua = new Object();
t.ua.mac = (v.indexOf("mac")!=-1);
t.ua.win = (v.indexOf("win")!=-1);
t.ua.nn = (n=="Netscape");

versus:
var v = navigator.appVersion.toLowerCase(), u = navigator.userAgent.toLowerCase(), n = navigator.appName;
var d = document;
this.ua = new Object();
this.ua.mac = (v.indexOf("mac")!=-1);
this.ua.win = (v.indexOf("win")!=-1);
this.ua.nn = (n=="Netscape");

The FIRST one (the "shorter one") takes 1 byte more than the SECOND. ;)
December 19, 2003 7:00 AM
 

Rory said:

Good point :)
December 19, 2003 2:14 PM
 

John Deal said:

I keep a printout of a screenshot that I picked up off of the net next to my desk that has a dialog box with the text "Write your code like the person who has to maintain it is a psychopathic killer that knows where you live!" I fully believe that everyone should program for the guy that will follow them..
December 19, 2003 3:47 PM
 

Andy said:

One of the all time worst I've ever seen: I was sitting at my desk (I work in a sort of lab type situation with two people surrounded by tons of machines and screens) the other guy who works behind me starts laughing and going OMG you have to see this sh!t! What he's got on his screen is this script he just inherited from our corporate office that operates on an AS400 emulator. It's supposed to update accounts for billing purposes in the system but it's never worked right so they sent it to us to fix. The language looks like VBA-2 it's a sort of old BASIC interpreture. Anyway here is exactly what he saw:

function Check(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q as boolean) as boolean

Dim MadeIt as boolean
MadeIt = false
if a then
if b then
if c then
if d then
if e then
if f then
if g then
if h then
if i then
if j then
if k then
if l then
if m then
if n then
if o then
if p then
if q then
MadeIt = true
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
if not MadeIt then
MsgBox("Dude you are screwed!")
exit function
else
Check = true
end if
end function

That's right 17 nested single character variable if statements and a "Dude you are screwed" message if it failed. In what was supposed to be an enterprise level application for updating billing databases for the entire company. I don't like posting where I work on-line but think "world's largest telecom" and you'll know. Can you believe that cr@p!?! I give him a hard time about it even to this day along the lines of "So any of your brother VB programmers sent you anymore great development examples lately?". He hates it because he writes emmaculate code but works mainly in VB.

As I don't have a lot of contact with VB stuff is the above par for the course with VB programmers or is that worse beyond the norm?
December 19, 2003 5:30 PM
 

Michael Favro said:

John:

"Write your code like the person who has to maintain it is a psychopathic killer that knows where you live!"


TFF! I couldn't agree more! And sometimes that psychopathic killer is yourself, six months or a year down the road, asked to make a small change to some bit of code you wrote. It's hard enough to put yourself back in the frame of mind you were in when you slung that most elegant bit of code ("I know there was a reason why I did things like *that*, now what was it?") without having being confused by the coding standards that you were using (or not using, as the case may be).

Rory, can you perform a "Citizen's Firing" on yourself??? Speaking of which, where were you when I've (desperately) needed you??? I can think of two developers that have really *deserved* a Doc Martin planted firmly upon the cheeks. One is still here in Phoenix where I currently live. Maybe on your tour, if it's not out of your way, you can help me out?

I know! "The Citizen's Firings for Charity Tour"!!! $500 a pop. Donate it to the Midvale School for the Gifted or something.
December 19, 2003 5:52 PM
 

Michael Favro said:

(I need to get me a cool url - the mailto: thing didn't work - live and learn.)


Andy,

That has to take the cake. No excuse for messages like that. Reminds me of the other developer mentioned above (that needs a C.F. but doesn't live in Phoenix). When he came on my project, it was just me and him. He didn't agree with the project standards that has been in place for three years or so (government project). Which is fine. He can have his opinion. Just get over yourself and follow the friggin' standards. Instead, he would comment out existing (my) code with comments like "old crappy way", and insert his own code with the comment "new better way". The code was, like, 80% complete. And rather than suck it up and conform for the last 80% of the project (remember, government project), he had to go through and change *everything*!!! And it's not like it was "better", just different. It wasn't technically wrong. But it wasn't according to our project's standards.

Of course, that still pales in comparison to the developer that didn't like using Option Explicit because "the code doesn't compile with it. The code runs just fine without it."

"I'm surrounded by idiots."
December 19, 2003 6:09 PM
 

Owain Cleaver said:

Totally with you on most of your points but the js one i'm not so sure about; obfuscation was probably the major reason for it - js "coders" tend to be funny like that, also file size could have been an issue...

Oh and whoever said about about "if 0 then"; in my defense it's only because there's no block commenting in vb (script anyway, i don't do "real" vb).

Regarding comments, am I the only one who loves finding silly comments in inherited code? Hell even my own comments long forgotten, "// messy but i'm hungover and not slept for 2 days. sorry"; surely going to raise a smile on anyone? right?? :-)

/me hopes this doesn't come up in any prospective employers google search
December 19, 2003 7:57 PM
 

Jason Olson said:

"Oh and whoever said about about "if 0 then"; in my defense it's only because there's no block commenting in vb (script anyway, i don't do "real" vb)."

That was me. Yeah, I usually let this one slide a bit when I see it in InterDev due to the lack of a block commenting tool. I still would prefer to apply as many refactorings as needed to the code in order to "Extract Method" that baby!! However, I will admit that in a lot of projects there simply isn't time to do that.

In VS6, or VS.NET? There is a block commenting tool so I usually don't let this slide in those IDEs.
December 19, 2003 9:59 PM
 

Jason Olson said:

And as a quick side-note, I hate InterDev with a passion so intense it rivals the Trailblazers passion to smoke a phatty as much as possible.
December 19, 2003 10:00 PM
 

Davide Inglima said:

Rory does Bileblog. I'm so excited that I am pouring hot grits down my pants.
December 19, 2003 11:44 PM
 

Chris Hanson said:

I totally, 100% agree on the "follow the style guide" comment. Following the style guide can be more than an aesthetic choice, though; it can have real impact on the behavior of your product.

I use Cocoa and Objective-C on Mac OS X. In Cocoa, there's a feature called "Key-Value Coding" that sounds similar to .Net's "properties." Essentially, you can ask an object for the value at the key path "foo.bar" and it will look in the object for a foo property, and in that for a bar property. It'll first look for appropriately-named methods (foo, getFoo, _foo, _getFoo), and then for appropriately-named instance variables (foo, _foo, _isFoo), so you can do some extremely dynamic things with it.

What's the problem? The problem is that I see lots of developers new to Cocoa coming from other platforms giving their instance variables names like "m_pFoo". MFC naming style and Hungarian notation. Which isn't understood by Key-Value Coding. Which means that later on when these developers decide to add AppleScript support, or switch to using the Cocoa bindings layer, or want to sort their data using NSSortDescriptor objects (like table views do automatically), or use my BDControl and BDRuleEngine frameworks, or do anything else which relies on KVC, they'll be hosed. All because they didn't take five minutes to read the documentation on naming conventions and then follow what they read.
December 20, 2003 1:19 AM
 

Jeremy said:

While we're near the subject of poor error messages, check out this fantastic messagebox featured in a late 90's version of the Great Plains accounting package:

FERRARI

The error message basically translated as this: "The software has just corrupted your database so badly that we dare not display an error message that is actually informative, as doing so would cause you to declare open hunting season on our staff. Instead, we display "FERRARI", allowing you to contact tech support with a puzzling question regarding the message box you've seen, at which point we immediately recognize the situation you are in and promptly forward your call to the smartest person in the entire company."

Don't think for a moment that I am kidding.
December 20, 2003 1:35 AM
 

chrootstrap@yahoo.com said:

I love it! That's hilarious!
December 20, 2003 6:45 AM
 

Wayne Allen said:

I had a similar post earlier this month. hmmm.
http://weblogs.asp.net/wallen/archive/2003/12/11/42935.aspx
December 26, 2003 2:51 AM
 

Mark Freedman said:

Back in the late 80s, I had to maintain a dBase III system written by a consultant, who was no longer available because he had a nervous breakdown and was committed. I had to rewrite the entire system because every single variable and module name (over 100 modules) was named after a member of Frank Zappa's family.

I'm serious.

In addition, half the modules contained under 10 lines of code.
April 28, 2004 5:20 PM
 

TrackBack said:

And that's the bottom line cause.....
December 19, 2003 1:43 AM
 

TrackBack said:

Partial Coding Prediction
December 23, 2003 8:07 PM
 

TrackBack said:

Been blogging for a year - Forgotten why I even started...
April 27, 2004 1:14 AM
New Comments to this post are disabled

About Rory

I *own* this site, you loser.