DEV Community

Mashcuy Lab
Mashcuy Lab

Posted on

Refactoring #2

In this article we are summarizing the most interesting parts of:

  • Martin Fowler - Refactoring (2018) Chapter 3 "Bad Smells in Code"

This chapter helps us to detect bad code practices:

Mysterious name

  • you can't think of a good name for something
  • you are often afraid to rename things
  • solution a good name can save you hours

Duplicated code

  • solution unify code with the same structure

Long function

  • solution long term software is written in short functions

Long parameter list

  • solution depend on objects and not on attributes

Divergent change

  • every time there is a new user type, then I'll have to change these three/four functions
  • solution reduce coupling by joining similar functions together and consider them a new module

Primitive obsession

  • a telephone is more than a collection of characters
  • a url is more than a string
  • solution move those primitive types to it's own meaningful types

Repeated switches

  • if the same switch/case or if/else structure is repeated many times.
  • solution we should consider replace conditional with polymorphism.

Speculative Generality

  • over engineering a feature is very common
  • solution get rid of functions that handles things that aren't required yet

Middle man

  • is common that a class ends up delegating much of it's behavior to other classes
  • solution remove those classes and replace them with the actual functionality

Data class

  • solution instead of having classes with only fields, getters and setters, use interfaces or types

Top comments (2)

Collapse
 
dejanfajfar profile image
Dejan Fajfar

A nice list. Some items are quite hard to achieve and could be more trouble than it is worth. But non the less a worthy goal to strive for

Collapse
 
mashcuy_lab profile image
Mashcuy Lab

Thank you! I view these guidelines as helpful reminders. Nowadays It's easy to get lost in the complexities of architectures, frameworks, APIs, and AI, and overlook the fact that software is ultimately built one small feature at a time. This is where these code smells become crucial. Martin Fowler did an excellent job of highlighting the most essential cases and I highly recommend his book.