Get a Whiff of This
April 25, 2020
These are my notes from the youtube video: Get a Whiff of This presented by Sandi Metz at Rails Conf 2016.
The talk is about “Code Smells”
Kent Beck and Martin Fowler collaborated on Refactoring
- The third chapter of that book is about code smells
Most people cannot list 5 code smells
Sandi lists 22 smells that all have percise meanings
Learning code smells by name help you communicate why code might have a problem.
Not all “smells” are worth addressing, but they give you hint.
The refactoring book is a very dry book. Its a very much a receipe book. Its very much worth reading.
22 code smells are hard to remember. You can catagorize the list to make it easier to digest.
Category One - Bloaters: Things that make code too big
- Long Method
- Large Class
- Data Clumps
- Long Parameter List
- Primitive Obsession
- “This is when you have an instance of a base class that you pass around and objects decide what to do based
Category Two - Tool Abusers - OOP Misuse
- Switch Statements
- Refused Bequest
- Alternate Classes /w Different Interfaces
- Temporary Field
- Maybe you should have made a method with a name
Category Three - Change Preventers - make code hard to change
- Divergent Change
- Shotgun Surgery
- Parallel Inheritance Hierarchies “Ugly embarrassing code that doesn’t change really isn’t that big a deal - it works and you don’t need to touch it”
Category Four - Dispensables -Pointless Classes
- Lazy Class: class that doesn’t do enough
- Speculative Generality: Over design of a class for something we don’t need.
- Data Class: class that only has data not data and behavior
- Duplicated Code:
“Most of the time we spend reading code. Make it easy it read.”
Category Five - Couplers - things that bind objects together
- Feature Envy:
- Inappropriate Intimacy: Using Private methods of another class
- Message Chains: To many “dots” with type changes
- Middle Man: A object that just fowards messages to others
Refactoring has Recipes
example Fowler 149: Extract a class
All the refactorings work
Refactoring - Re arrange code without changing behavior
Every Code Smell Maps to a Curative Refactoring Recipe
There’s a Refactoring Quick Reference Guide Cheat Sheet created by Industrial Logic
There’s a lot of knowledge that has been lost. We should re-invest our latest generation on this.
Sandi shared some Examples of refactoring using code smells
Code Smell => Curative REfactoring Recipe
The refactoring book is good and worth learning. It will make you better at programming if you learn these methods.
Reek is a static analysis tool that will tell you what code smells you have.
Smells are a good way improve your code. They are not bad. They are just information, and most of the information is neutral.
Techniques change with time. Looking back at your old code you will see something really bad.