Code Smell

The 10 most common code smells according to Kent Beck and Martin Fowler are:

  • Duplicated code: Same expression in two methods of the same class, Same expression in two sibling subclasses, Duplicated code in two unrelated classes

  • Long method

  • Large class

  • Long parameter list

  • Divergent change: Occurs when a class is changed for different reasons. Goal is to have a design where each object is changed as a result of only one kind of change.

  • Shotgun surgery: Opposite of divergent change which occurs when every desired change in functionality requires many changes to a many different classes. Problem is that changes like this are scattered in many places so it is easy to miss one.

  • Feature envy: when one method calls methods of other classes more often than methods of the class that it is in.

  • Data clumps: Some data items often appear together in lots of different places.

  • Temporary field

  • Message chains

Below are few more code smells:

Primitive obsession: Primitive data types are basic built-in building blocks of a language. They're usually typed as int, string, or constants. As creating such fields is much easier than making a whole new class, this leads to abuse. Therefore, this makes this smell one of the most common ones.

  • Using primitive data types to represent domain ideas. For example, using an integer to represent an amount of money or a string for a phone number.

  • Using variables or constants for coding information.

  • Using strings as field names in data arrays.

Parallel Inheritance Hierarchies: This occur when an inheritance tree depends on another inheritance tree by composition, and they maintain a special relationship where one subclass of a dependent inheritance must depend on one a particular subclass of another Inheritance.

Speculative generality:

Alternative classes with different interfaces:

Inappropriate intimacy:

Refused bequest: