Iris Classon
Iris Classon - In Love with Code

‘Stupid’ Question 15: What does ’Code-smell’ mean, and where does the term come from?

[To celebrate my first year of programming I will ask a ‘stupid’ questions daily on my blog for a year, to make sure I learn at least 365 new things during my second year as a developer]

Time for some saturday fun, coding on the balcony

I’m sitting on the balcony right now, after some serious sleeping (14 h) with a cup of coffee in my hand and questions on my mind. Last night I stayed up late on Twitter, and we had a bit of a talk/debate about the last question, ‘What is a tuple and what can I use it for’? And so we got into the code smell talk and I realized that I haven’t really thought too much about what we mean by that term and decided it would be a perfect question for today. And I will spend a whole week dedicated to this term, and go through 6 more questions that are related- but, let’s answer this one first.

Code smell is a part code (large or small) that might indicate that something is wrong, that there is a deeper underlying problem. By this I mean that the code itself isn’t necessarily breaking anything, but that it shares common traits with code written that way as a quick fix or result of an underlying problem.

When I was working on a project with some serious issues I used to call it duct-tapping. You’ve smashed your car up, but instead of fixing it you’ll just get the duct-tape out and fix the other bits and pieces. Of course this will only work so long, you’ll need more and more tape as more cracks appear- until the whole thing breaks down on you. You would have been better of fixing the underlying problem, be that you’re driving skills, the car or something else.

Obviously there is an underlying problem here…

And why ‘smell’? The ‘smell’ part is indicating that you can smell something, and you should follow that smell to locate the source of the smell - and fix that. It’s like covering up the smell of sweat with a perfume,- you should probably take a shower instead. For me it feels more like watching something that I think is ugly, and it is the same with design (design is interesting, but it is complicated and I prefer to ask the pro’s about advice as it is more than just making something pretty). Only this week I’ve called my dear friend Kim Brockie for UI design advice and said (while sharing the screen) – what’s wrong here? I don’t like it but I don’t know why. And it’s the same with code, I’ll just get a gut feeling that something is wrong, and I’ll usually grab one of my coworkers and ask – what is wrong with this picture and what can I do? Having a second nose, or more, around can be really helpfull to analyze smells :D

The term itself was according to Martin Fowler coined by Kent Beck while helping out on M.Fowler’s Refactoring book (Bad smells in code).

Comments

Leave a comment below, or by email.
Patrik Löwendahl
8/4/2012 2:08:10 PM
Some more ideas from Michael Feathers on the relationship between smells and testability @NDC2010: 

http://blog.lowendahl.net/?p=306 
Ladislau Radu Nagy
1/21/2013 10:36:59 AM
I can define code-smell something like this: Source code that is too long, too complicated, too ugly, too cryptic, spaghetti like, jack of all trades, madness, the algorithm is too dumb or complicated, there must be a simple way to do this.
Have you seen comments like this: "Magic. Don't touch it!", "Increment the counter if you couldn't fix it!", "When I've made this only God and I know what I was doing, now only God knows."
In my experience C and C++ code is the most smelly, people just don't realize they are making life much harder. Surely I've seen many C# code that was a maintenance nightmare.
My favorite quotes about code smell and maintenance: "Code with your future self in mind." and "Code as if the guy who will maintain the code is a serial killer."
There is and interesting article from Jeff Atwood about this topic: http://www.codinghorror.com/blog/2006/05/code-smells.html 


Last modified on 2012-08-04

comments powered by Disqus