Code Smells
Malik Nabeel Ahmed Awan, PhD
Code Smells
• Bloaters are code, methods and classes that have increased to such gargantuan
Bloaters proportions that they are hard to work with.
Object-orientation • Not implementing object-oriented programming principles as they should be.
abusers
Change Preventors • Same code over multiple places make code hard to fix.
• A pointless and unneeded code whose absence would make the code cleaner,
Dispensable more efficient and easier to understand.
• All the smells in this group contribute to excessive coupling between classes or
Couplers show what happens if coupling is replaced by excessive delegation.
Bloaters
Long Methods
A method longer than ten lines should make you start asking questions.
Large Classes
A class contains many fields/methods/lines of code.
Primitive Obsession
Use of primitives instead of small objects for simple tasks (such as
currency, ranges, special strings for phone numbers, etc.)
Use of constants for coding information (such as a constant
ADMIN_ROLE=1for referring to users with administrator rights.)
Use of string constants as field names for use in data arrays.
Bloaters
Long Parameters Lists
More than three or four parameters for a method.
Data Clumps
Sometimes different parts of the code contain identical groups of variables
(such as parameters for connecting to a database). These clumps should be
turned into their own classes.
Object-Oriented Abusers
Switch Statement
Complex switch or multiple if statements.
Temporary Fields
Temporary fields get their values only under certain circumstances. Outside of
these circumstances, they’re empty.
Refused Bequests
If a subclass uses only some of the methods and properties inherited from its
parents, the hierarchy is off-kilter. The unneeded methods may simply go unused
or be redefined and give off exceptions.
Alternative Classes with Different Interfaces
Two classes perform identical functions but have different method names.
Change Preventers
Divergent Changes
You find yourself having to change many unrelated methods when you make
changes to a class. For example, when adding a new product type, you must
change the methods for finding, displaying, and ordering products.
Shotgun Surgery
Making any modifications requires that you make many small changes to many
different classes.
Parallel Inheritance Hierarchies
Whenever you create a subclass for a class, you find yourself needing to create a
subclass for another class.
Dispensable
Comments
A method is filled with explanatory comments.
Duplicate Code
Two code fragments look almost identical.
Lazy Class
If a class doesn’t do enough to earn your attention, it should be deleted.
Data Class
A data class refers to a class that contains only fields and crude methods for accessing
them (getters and setters).
Dead Code
A variable, parameter, field, method or class is no longer used (usually because it’s
obsolete).
Speculative Generality
There’s an unused class, method, field or parameter.
Couplers
Feature Envy
A method accesses the data of another object more than its own data.
Inappropriate Intimacy
One class uses the internal fields and methods of another class.
Message Chain
In code you see a series of calls resembling
Middleman
If
a class performs only one action, delegating work to another class, why
does it exist at all?
THANK YOU