Sunday, November 9, 2014

The Sound of Code Smells

Caveat: A friend told me that this is a bit of a rambling post so proceed if you must but be prepared to bear with my blabbering if you have nothing better to do. I will work on refactoring this for clarity and post an update when I can get around to it. Thanks.

If a piece of code smells and there are no developers around, does it make a sound?

That's an interesting twist to the tree in the forest thing, isn't it? We may forever ponder the question about that poor tree in the forest--one company even advertises its opinion about it--but my answer to the code smell question is a definite and resounding "Yes!"

What sound does a code smell make?

At worst, I would imagine that it sounds something like whatever inspired Edvard Munch to produce his arguably most famous piece of work, The Scream. The eternal juveniles and jaded cynics out there will probably say "Plop!" or "Schploosh!" or "Really bad code smells are silent but deadly." If you've read Uncle Bob's "Clean Code" book, you might suggest that the sound of a code smell is "Dude, WTF?!" As crude as some of these retorts may be, you have to admit there's a distinct ring of truth to them.

Seriously though, you'd think that a very apt metaphor like "code smell" should be enough to motivate developers to take another look at their code and clean it up, right? So why would you want to add to a perfectly good metaphor? In this day and age when everyone is supposed to be familiar with—and presumably doing!—refactoring and unit testing, shouldn't this "smell" thing make sense to developers already?

Well, Virginia, I hate to break it to you like this but apparently, the "code smell" metaphor is still not good enough to help a large number (I shudder to think that "a majority" applies here) of developers in the real world start writing better code.

While I'm sure it would be easy for your average agile developer to think as far back as a day ago to remember when he last got a whiff of bad code, tracked it down, and cleaned it up, the average Joe Schmoe developer just doesn't have that kind of olfactory ability. At best, their code smell genes are still lying dormant somewhere inside them, just waiting for that moment of XP epiphany to finally break out in full glory so these developers can reach their true potential as lean, mean, code smell busting machines, the way nature intended all good developers to be.

Ok, it isn't that glorious either. Not usually. For many, "breaking through" those walls of ignorance that imprisoned us in rotting, smelly code doesn't conjure up images of emerging from a pit of darkness into glorious sunshine, birds singing, and gentle breezes blowing over endless seas of undulating green grass.

Getting code smells is really more like XP puberty, right? It's like those episodes of adolescent "break outs" that meant painful hours of intense scrutiny, self-loathing, and daily scrubbing in front of a mirror. For the unfortunate few who don't get it quite right and are left permanently scarred, code smells are just a fact of life that you learn to live with and things like Agile, TDD, and refactoring are nothing more than bottles of snake oil that unscrupulous traveling salesmen hawk to the many eager but desperate, unsuspecting, and gullible developers out there.

For those who do get code smells, TDD, and refactoring, "breaking out" is often followed by a few of those post-escape Andy Dufresne moments when our initial jubilation of making it out of the sewer is replaced by cold fear, confusion, and self-doubt. And certainly there are those poor old souls like Brooks Hatlen whose senses become overwhelmed--everybody is such a big damn hurry!--that they just simply give up in despair, longing for those simpler times before they were given so much freedom and a new lease on life. Some people just can't handle the truth. There has to be something that can be done to help out guys like that.

Who among us, apart from the very fortunate and brilliant few, have never said to themselves after they first started truly grokking and experiencing code smells, things like "I can't believe I wrote that sh*t!" or "Was I even sober when I wrote that cr*p?!" or just simply, "Dude, WTF?!" and then follow it up with the inevitable "Now what?" and "Maybe I was better off back in Shawshank and not having to deal with this."

Can any of us claim that we can always point a finger at the source of a code smell and decisively and incisively take care of it lickety-split? Heck, even gurus like Kent Beck admit to getting stuck sometimes. What hope is there then for us mere mortal developers who aspire to keep our code as clean as humanly possible? Ok, that's a bit too dramatic, I admit, but if you've been there, you know what I'm talking about.

Anyway, lately I've been asking people on JavaRanch to read their code out loud. Here's one reply where I do that (look for my reply that has a bunch of sample unit test code). This is actually what motivated me to write all this but there are quite a few other threads where I've asked the OP to do the same thing: read the code out loud.

If you try it and read some smelly code out loud, you might see what I mean by "the sound of code smells". Or rather, you might hear what I mean.

For example, the sound of redundancy is often a stammer, as with the Booking thing in that JavaRanch thread (Listing 1).

Listing 1. The sound that Redundancy makes

    public void addBooking(Booking booking) {
        bookings.add(booking);
    }


Comments that smell of redundancy sound very maddenesque (Listing 2).

Listing 2. The sound that Redundant Comments make

    public void addBooking(Booking booking) {
        // adds a booking to bookings
        bookings.add(booking);
    }


The sound of poorly chosen names can be heard in this thread and this thread and there are quite a few others where you may hear different kinds of sounds.

(Sorry if the links to JavaRanch are off-putting to you. I'm not trying to click-bait or spam-link you with these -- it's just that I have yet to ask the bosses at JavaRanch if I can take code that was posted there and put them up here as examples. I'll update this later if I get a nod from them, which I'm hoping I will).

I'm sure there are many other kinds of code smell "sounds" out there that I've heard but, just like with "smell", it's difficult to describe "sound", especially to someone else who has never heard the same sound before. To someone who doesn't know any better, "The sound of a poorly chosen name" is very Zen-like. I tweeted the other day about how difficult it would be to train a dog named "Stay". The sound of a poorly named method inspired that tweet. Here's my reply on JavaRanch about it.

If you think about it, adding the sense of hearing to the already good metaphor of "code smell" makes a lot of, well, sense. We're already saying things like "the code tells a story" and "the code is telling you what it wants to do" and "that code just screams out to be refactored" and "that variable is asking to be renamed," right? These all suggest that we need to listen more carefully to our code. What better way to get developers to listen than to get them to read their code OUT LOUD?

The point of all this, and thank you for listening to me blabber away for this long, is that I think it would help those developers, the ones who don't quite know how to recognize a code smell yet, if we older and more experienced developers who do would tell them more often to read their code out loud and listen to the sounds that their code is making.

Even for us old paroled jail birds, who sometimes get stuck and are experiencing that "maybe I was better off back in Shawshank" feeling, it might help us to read our code out loud once in a while. Something seems to happen when we actually hear what the code is doing and sometimes that's all it takes to get unstuck.

Maybe for us developers, the best way to develop our metaphorical sense of code smell is by exercising our literal sense of hearing.

So, to all the young developers and all the old fans of Casablanca, "Here's to hearing from you, kid."

(Update, Tuesday 11-Nov-2014)

I was looking around for other references to code sounds and found one on Ward's Wiki on, of all places, the page on Code Smells. Russell Gold mentions the auditory metaphor in a comment about how saying code "smells" has a different connotation than saying that it is "off-key" or "doesn't sound right." While his point is not quite the same as the one I'm trying to make here, I still think it's interesting that others have felt that it's at least worth considering other sensory metaphors besides smell.
Post a Comment