Couplers I Smell Thee
Think you have a code smell? Well today we are going to go though a list of smells
that are grouped into the context of Couplers. We will go through each of them, and how they each contribute to the making of the
Worse Code Smell Ever
What makes this particular code smell so destructive to your code base is that the ghosts of your past bad design practices start to lay waste to your code, as soon as you start to make any changes. This leads into a reductive feedback loop that leads to developer paralysis when trying to implement any “new” features.
Couplers: who cut the 🧀 ?
Couplers represent a tight coupling between classes or methods.
Couplers make our code fragile and cause a cascade of changes.
Feature Envy
First off the block, this code smell can look like below. Class A
and Class B
are just hanging out, only Class A
seems super
fixated by the methods that are inside of Class B
. Similar to being on a date, but you’re far more interested in the conversation going on at the table next to you, maybe you’re sitting at the wrong table.
The solution ?
Move TableMove the MethodsExtract yourself from the dateExtract the MethodsInappropriate Intimacy
Now apart from really messing up your blog sites key words for S.E.O, Inappropriate Intimacy can lead to super
tight coupling between Classes and lead to you always having to make multiple changes in each of the coupled
classes for every change.
This smell is evident when two or more classes become little too intimate and spend too much time accessing each other’s private methods and behavior.
best to keep your hands off your neigbours
private
methods
The solution ?
move in with your neighbourMove the Methodmove to a new neighbourhoodMake a new Class that better encapsulates theprivate
methodsMessage Chains
Similar to being in a crowded bar with friends and your “Pint of Apple Cider” gets turned into a “Bourbon Shot Rider”, passing Objects loads of chained messages in a noisy and distracting environment can lead to unwanted beverage code consequences.
Take for example:
customer.getAddress.getCountry.toString;
Now this has a couple of problems:
-
the messages methods have to be chained together in a certain way - if we are replicating this in numerous areas in the app, each of these message calls have to be updated at the same time otherwise Kaboom!
The solution ?
delegate your order to a waiterUse Delegate to get closer to the callmove to a quieter barMove or Extact the call into a new method with the logic inside
Middle Man
Somehow in the process of ordering all of your drinks all night, and paying tips to the waiter
you realise that all of this delegation
is getting very expensive. The use of a waiter object has cut you off from the source, so why not just engage the barman directly ? You could also do like below, and allow yourself to get drinks from both the waiter
or the barman
directly when required.
The solution ?
order straight from the barRemove the Delegator and interface directly with object.just drink at home for a simpler experienceMaybe you have refactored too much ? back it up cowboy.
Conclusion
As you can see from the above examples, Couplers
can really mess with our coding chi. A main take away is that by tightly coupling our couple we make it brittle, fragile not agile, and hard to change or maintain. By allowing your Objects to talk easily, or be injected to or from other Objects you allow your codebase to be scalable and maintainable in parts, with everyone not having to be an expert on the whole codebase just to change a small part of it.
references: