Slandering other people's code - Code Smell !


There is nothing worse than someone standing over your shoulder and telling you that something is wrong with the code you've written. "That's a 'Code Smell'" they proclaim. "You should fix that" and walking off. You cringe, looking at the code, not sure what they are talking about and at the same time, feeling like you've committed some cardinal sin worthy of a good flogging at the very least.

Code Smell is defined rather well on Wikipedia. So go have a read if you are not familiar with the term.

There are several points made in the page which I think are quite important. The first points is that just because something is a code smell, it doesn't mean it's buggy or non-working code. Usually the code does work, it's just written in a way that isn't great, or indicates a potential deeper problem in the code base. The second point is that most code smell's are usually written by experienced developers. And usually because they are cutting corners to get the work done on time.

The problem I have is that it's too easy for people to just flipantly call something a smell. Often without taking the time to understand whether the problem they are seeing is indeed, a known code smell, something they just don't like or even a real problem. They just label it a "smell" to give it a perceived authority so they don't have to justify their opinion. And their opinion might be completely wrong because sometimes, something that looks wrong may in fact be right. Like design patterns, smells are not gold rules, but hints, guides and clues, not 100% applicable Gold Plated Standards.

I don't think in terms of code smells, and I don't often use the term. Instead I simply see code that makes me nervous. It might feel to complicated for what it does. It might feel like it's likely to create more problems than it solves. Or it might feel like it's re-inventing something I've seen somewhere else. Yes these are known smells, but it's the approach that counts.

I tend to talk to the developer about the code, explain what I'm concerned about, give them a chance to explain it to me and offer my opinions on how I think it might be improved. Yes I could just point at something and say "That's a code smell", but that's a fairly hostile/derogatory approach and not the way I prefer to do things.

So next time you are looking at someone's code and thinking about calling something in it a "Code Smell", think twice, ask yourself - 'Does this person know what they are doing? Could they know something I don't?' and take a second look.

Comments powered by Disqus