Identifying Code Smells - Falmouth-Games-Academy/comp350-research-journal GitHub Wiki

Identifying Code Smells

What is Code Smell?

Code Smell is described by Martin Fowler as a surface indication that usually corresponds to a deeper problem in the system. The term was first coined by Kent Beck in Martin Fowler's book "Refactoring: improving the design of existing code" 1(https://www.amazon.co.uk/Refactoring-Improving-Design-Existing-Technology/dp/0201485672) 2(https://martinfowler.com/bliki/CodeSmell.html).

For more information on this, see Code Smell.

Why are they important to identify?

Code smells are usually not bugs and do not prevent the program from running. Instead, they indicate a weakness that might slow down development or increase failures in the future. This why it is extremely important to identify code smells early in your projects and fix them because the bigger your project is going to get, the more work you will have to do in the future. Fixing code smells is about optimising your development. The more code smells your code contains, the more time will be wasted working around these structural issues.

Types of Code Smells 3(https://sourcemaking.com/refactoring/smells)

See Refactoring for solutions to some of these problems with your code.

Bloaters

Any code, methods, and classes that have increased in size to the point of being hard to work with.

  • Long Method - A shorter method is easier to read, understand and troubleshoot. Refactor long methods into smaller ones if possible 4(https://blog.codinghorror.com/code-smells/).
  • Large Class - Smaller classes are easier to read, troubleshoot and understand. Does the class contain too many responsibilities?
  • Primitive Obsession - Don't use primitive data type variables as a substitute for a class. If the data type is mildly complex write a class for it.
  • Long Parameter List - The more parameters a methods ha, the more complex it is.
  • Data Clumps

Object-Orientation Abusers

Smells that do not fit Object-Oriented programming principles.

  • Switch Statements
  • Temporary Field
  • Refused Bequest
  • Alternative Classes with Different Interfaces

Change Preventers

If you have to make one change to your code it should not require you to change it in other places also.

  • Divergent Change
  • Shotgun Surgery
  • Parallel Inheritance Hierarchies

Dispensables

Anything that is pointless and unneeded. With it removed the code should be cleaner and easier to understand.

  • Comments
  • Duplicate Code
  • Lazy Class
  • Data Class
  • Dead Code
  • Speculative Generality

Couplers

All the smells in this group contribute to excessive coupling between classes or show what happens if the coupling is replaced by excessive delegation.

  • Feature Envy
  • Inappropriate Intimacy
  • Message Chains
  • Middle Man
  • Incomplete Library Class

References

[1] Fowler, Martin. Refactoring: improving the design of existing code. Addison-Wesley Professional, 2018.
[2] CodeSmell https://martinfowler.com/bliki/CodeSmell.html
[3] Code Smells https://sourcemaking.com/refactoring/smells
[4] https://blog.codinghorror.com/code-smells/
[5] https://github.com/Falmouth-Games-Academy/comp350-research-journal/wiki/Code-Smell