Code Smell

A code smell is a hint that something has gone wrong somewhere in your code. Use the smell to track down the problem. KentBeck (with inspiration from the nose of MassimoArnoldi) seems to have coined the phrase in the "OnceAndOnlyOnce" page, where he also said that code "wants to be simple". Bad Smells in Code was an essay by KentBeck and MartinFowler, published as Chapter 3 of RefactoringImprovingTheDesignOfExistingCode.

Highly experienced and knowledgeable developers have a "feel" for good design. Having reached a state of "UnconsciousCompetence," where they routinely practice good design without thinking about it too much, they find that they can look at a design or the code and immediately get a "feel" for its quality, without getting bogged down in extensive "logically detailed arguments".

Note that a CodeSmell is a hint that something might be wrong, not a certainty. A perfectly good idiom may be considered a CodeSmell because it's often misused, or because there's a simpler alternative that works in most cases. Calling something a CodeSmell is not an attack; it's simply a sign that a closer look is warranted. so CodeSmell is more instinctive that intuitive? -- S

[Copied from GotoConsideredHarmful:]

There are two major approaches to programming: [is there a page that covers this? there should be]

...And therefore a CodeSmell is a hint of possible bad practice to a pragmatist, but a sure sign of bad practice to a purist.

If I recall correctly, this is part of the reason why (was it KentBeck or MartinFowler) chose this name for the term. They wanted to emphasize the pragmatic view of CodeSmells by the connotation of the word smell. If something smells, it definitely needs to be checked out, but it may not actually need fixing or might have to just be tolerated.

See also


Here are some Code Smells that tell you something might be wrong.

Too much code, time to take something off the stove before it boils over:

Not enough code, better put the half-baked code back in the oven a while:

Not actually the code:

Problems with the way the code is changing:

Other code problems:

From BadSmellsInCode?: The list of canonical smells found in RefactoringImprovingTheDesignOfExistingCode can be found at: http://emw.inf.tu-dresden.de/de/pdai/Forschung/refactoring/refactoring_html/node26.html (we're slowly making sure they're all here.)

Some Principles for judging whether code smells bad or good are defined in PrinciplesOfObjectOrientedDesign.

See Also:


[This list needs refactoring to above:] CodeSmells referenced in:


Discussion:

What ever happened to "Listen to what Smalltalk is telling you?"

Same thing, isn't it? Just a different analogy: perhaps some of us smell out code problems; some see them; and some ListenToTheCode.

. . . . . .

I have recently completed a Sensory Orientation Survey of 11,352 randomly-selected software developers, determining their primary sensory mode. 10,901 were visually-oriented, 430 hearing-oriented, 20 were touch-oriented, and one was smell-oriented. Some other interesting statistics were also gleaned, but I'm not sure how they are related. There were 386 visually-impaired developers, 19 vision-and-hearing-impaired developers, and one developer named Spot.

I think I interviewed Spot today.

I think my major problem with the terminology is that it complicates critiques. It means one thing for one developer to say to another, "your code doesn't sound right," or, "your code sounds off-key," and quite a different thing to say, "your code smells." I wish the earlier auditory metaphor had stuck... -- RussellGold

Most people are visual ("I see that"). Fewer are auditory ("I hear you"). Fewer still are tactile ("I feel that"). So few use smell as their primary sense that speaking of smells seems a bit alien to almost everyone. Perhaps that makes it a good choice.

You see this, you hear that. But you go "what is that smell ?"

It must be Coffee Which brings up the point that all smells are not BadThings. Smells tend to permeate the air and are not necessarily easy to track down. (Except in the kitchen). It seems that smell has been used in a negative sense. Could this term be used to describe a GoodThing? Can someone create code that smells good?

Smell is such a primitive (neurologically speaking) sense that people often respond to smells without being consciously aware of them, with their hindbrain acting on it before the cerebrum learns about it (if it ever does). This goes for smells good and bad. Not to say the same thing doesn't happen with other senses, but that "something's going on here" impression that comes with a consciously unperceived whiff of something may possibly be not too dissimilar to the same "something's going on here" impression that comes with "smelly code".


In English, "it smells" ~= "it smells bad".


Code is too abstract to map well to visual metaphor, and not enough people have the ear for auditory metaphor, but [almost] everyone has a nose and can smell garbage or roses. -- Pete Hardie

Also, smells cause a greater visceral reaction, reaching deeper into the greymatter than sight or sound. Both sight and sound are so overloaded that we have evolved the ability to tune out a lot of noise and clutter. Its easier to provoke a response with a smell, assuming the scent is communicated. -- EricScheid


Ah, but when you smell a problem, you only know that you have to fix something. You still have to look to find exactly what needs fixing.

The smell is only a symptom.


When I eat a bowl of plain rice while smelling my neighbor's broiled duck, I eat a feast fit for a king. -- Chinese saying (paraphrased)


CodeSmell can creep up on you. Look at EmpiricalPatternDiscovery as a development tool to guide you in the process of SlowRefactoring? as complement to doing what we call RefactorMercilessly. -- NiclasOlofsson


While chatting with CharlesMedcoff over drinks, we came up with the idea of ProcessSmell?'s. He gave the example of a company that has more QA staff than developers. Maybe this should be called MassiveQualityAssuranceRequired, or maybe something shorter. Hmmm...

-- EricRunquist


Code sounds: http://www.newscientist.com/news/news.jsp?id=ns99992757


In English, "it smells" ~= "it smells bad".

Yes, but 'smell' is not neutral enough for my liking. I suppose CodeSmell is more neutral than CodeStench or CodePerfume?.

-- DavidVincent

Codour? (or Codor, for those that think "US English" is a language) ;o) -- Nick Grimshaw

CodeSuspicionButNotRavingParanoia?? :-)


A bit of empirical evidence for the code smell concept - see "Using Redundancies to Find Error" at http://www.stanford.edu/~engler/p401-xie.pdf.

In it, the authors use code scanners to look for redundant operations (assignment to self, duplicated conditions statements). They find that not only are redundant operations sometimes "hard errors" themselves, but also that the presence of redundant operations in a source file is a good predictor of other hard errors in the same file.


code smells in C, C++ (EditHint: do we need a separate page for this?)

TailCallOptimization has some example code. One function has a ("automatic") local variable, and it passes that variable to another function. Then it mentions "On the other hand, bar() may do something like tuck the pointer away for later use, so using an automatic variable this way may not be safe anyhow."

I agree - this may not be safe; it's a code smell. I can't decide if it's the

The problem: After that function tucks the pointer away for later use and returns, after the calling function returns, then the (stack) memory that pointer points to is no longer valid. It probably gets overwritten with other values. Then if that pointer ever gets used, it is pointing to irrelevant invalid data.

Java goes to great lengths to make sure this problem never happens, by making it impossible to pass a pointer to a local (stack) variable - the only pointers that can be passed are "references" to objects created on the heap.

Please look at Java Servlets and the re-entrancy problems due to avoidance of stack based storage. I don't want to start yet another language battle, I just want to highlight that memory management issues are a complex problem that rears its head in different ways in different languages.


'code fragrance' - when a piece of coding is particularly elegant in design and efficiency. (code smells good)
See also: WhatIsaSmell

CategoryCodeSmell CategoryJargon


EditText of this page (last edited March 3, 2006)
FindPage by browsing or searching

This page mirrored in WikiPagesAboutRefactoring as of April 29, 2006