Code Smell – badly structured source code
The term code smell goes back to the book “Refactoring – Improving the Design of Existing Code”¹ by Martin Fowler and a metaphor used in this book by Kent Beck, the inventor of Extreme Programming. With “If it stinks, change it” Beck quoted his grandmother talking about changing babies.
Code smells refer to functioning but poorly structured source code. They declare constructs in programming that suggest refactoring. Figuratively speaking, it’ s code that smells bad.
The big problem with code smells – alternatively called simply smells or bad smells – lies in the developers’ understanding of the code: if the structure of a software is difficult to understand, errors easily creep in during adaptations or extensions. In addition, the maintainability of software becomes more difficult. Basically, two categories of code smells can be distinguished: class-internal and cross-class smells.
Class-Internal Code Smells
The following constructs often exist for Code Smells within a class:
- Code duplicates: A code duplicate is source code that is used in identical or similar form several times within a software application.
Long methods: the longer a method is, the more error-prone it is. In addition, long methods make reuse more difficult. Spaghetti code is also a popular term.
- Extensive parameter lists: they make it difficult to understand method calls because attributes of objects are passed as parameter lists instead of passing the object to the method.
- Many changes to a class: if classes become too extensive, a class often has to be adapted in several places for a change.
- Temporary fields: fields of a class describe the states of its objects, where an object uses a field only under certain circumstances or temporarily, making the code difficult to understand.
- Repetitive switch statements: they are used at multiple code locations, although it would be better to extract them as a separate method.
- Comments: commentary is good in itself, but it can also provide clues to bad code which is made more understandable by the comment.
Cross-Class Code Smells
In addition to class internal code, there are often cross-class code smells such as large classes, alternative classes (which do the same but use different interfaces), lazy classes, message chains, data clusters, parallel inheritance hierarchies, or subclasses that do not require inherited methods and data.
Basically, there are also other Code Smells that also make the code difficult to read and understand, such as meaningless, too short or too long names, redundant code, the forced use of design patterns, complex branching and deep nesting.
Impulse to discuss:
There are opinions that consider the term code-smell to be demeaning, condescending and smug. How do you see it?
 For a complete overview of code smells, take a look at the work of Martin Fowler.
Here you will find additional information from our Smartpedia section: