What is a Code Smell, what types exist and what is its effect?
Badly structured source code
A Code Smell refers to functioning but poorly structured source code. The term 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, who talked about changing babies. Code-Smell declares constructs in programming that suggest refactoring. In the figurative sense, it is about badly scented code.
The big problem with Code Smells – alternatively also simply called Smells or Bad Smells – lies in the developers’ understanding of code. If the structure of a software is difficult to understand, errors can easily creep in during adaptations or extensions. In addition, the maintainability of software is made more difficult. There are two basic categories of Code Smells: internal and cross-class Smells.
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.
For a complete overview of code smells, take a look at Martin Fowler’s work. You can find more information here »