A quick intro to Code Smells

  • They are not bugs
  • They are not technically incorrect
  • They do not block a working program
  • Indicators of weaknesses in design
  • Indicators of slow development
  • Indicators of high risk of introducing new bugs and errors
  • Long Method: Too many lines of code in just one method.
  • Large Class: Always putting in but never taking things out.
  • Primitive Obsession: The objects passing around are too dumb. As everybody understands primitives, everyone can do its own interpretation.
  • Long Parameter List: Use several parameters instead of an object with all the required data.
  • Data Clumps: Pieces of data that always appear together and probably should become an object.
  • Alternative Classes with Different Interfaces: Two classes that look different but perform the exact same operations.
  • Refused Bequest: A class and a super class share a tiny part of logic, but the inheritance as a whole does not make sense at all.
  • Switch Statements: Switch statements (or sequenced if statements) are a signal that polymorphism could be used instead.
  • Temporary Field: An attribute that is only used as a part of a process or algorithm and never again.
  • Divergent Change: When the logic in a single place is so twisted that you have to change unrelated things to perform your changes. Opposite to shotgun surgery.
  • Shotgun Surgery: When the logic is wide distributed and a modification requires small changes in many different places.
  • Parallel Inheritance Hierarchies: Two mirrored hierarchies that need to change always at the same time.
  • Comments: If comments are needed, something is not good. A good naming for the method and its parameters should be enough.
  • Duplicate Code: Two fragments of code that are identical.
  • Data Class: A class that is only a container of data with getters and setters and all the logic is placed in other classes.
  • Dead Code: Anything that is no longer used in the code.
  • Lazy Class: A class that does not get enough attention in a while, maybe has lost their right of being a class on its own.
  • Speculative Generality: Anything that is built “for the future” or “just in case”, in very rare cases we will be right doing predictions of the future.
  • Feature Envy: A method of a class that accesses the data of another object more than their own data.
  • Inappropriate Intimacy: A class that uses private fields or methods of another class.
  • Incomplete Library Class: When a library does not meet all the requirements anymore. Then the use of that library becomes painful. Fix it or throw it.
  • Message Chains: A client requests something to an object, and this requests something to another object, and so on. If the type changes, everybody needs to adapt.
  • Middle Man: When the only job of a class is delegate work or forward messages to other classes. Couldn’t it be avoided?

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Josep Mir

Josep Mir

Software engineer as a profession, frustrated musician as a hobby, casual sportsman and social drinker in my free time. My personal blog: https://mirdevs.com