Binus University Code Reengineering Bad Code Smell - Bloater
Binus University Code Reengineering Bad Code Smell - Bloater
Binus University Code Reengineering Bad Code Smell - Bloater
Year : 2017
- Long Method
- Large Classes
- Primitive Obsession
- Long Parameter List
- Data Clumps
Bloater smells represents something that has grown so large that it cannot be effectively
handled
Source: http://mikamantyla.eu/BadCodeSmellsTaxonomy.html
COMP6047 - Algorithm and Programming
Bloater: Long Method
A method contains too many lines of code. Generally, any method longer than ten lines
should make you start asking questions.
Source: https://sourcemaking.com/refactoring/smells/long-method
COMP6047 - Algorithm and Programming
Long Method Problem
Like the Hotel California, something is always being added to a method but nothing is ever
taken out. Since it is easier to write code than to read it, this "smell" remains unnoticed until
the method turns into an ugly, oversized beast.
Mentally, it is often harder to create a new method than to add to an existing one: "But it's
just two lines, there's no use in creating a whole method just for that..." Which means that
another line is added and then yet another, giving birth to a tangle of spaghetti code.
Source: https://sourcemaking.com/refactoring/smells/long-method
COMP6047 - Algorithm and Programming
Long Method Treatment
As a rule of thumb, if you feel the need to comment on something inside a method, you
should take this code and put it in a new method. Even a single line can and should be split
off into a separate method, if it requires explanations. And if the method has a descriptive
name, nobody will need to look at the code to see what it does.
If none of the previous recipes help, try moving the entire method to a separate object via
Replace Method with Method Object.
Conditional operators and loops are a good clue that code can be moved to a separate
method. For conditionals, use Decompose Conditional. If loops are in the way, try
Extract Method.
Source: https://sourcemaking.com/refactoring/smells/long-method
COMP6047 - Algorithm and Programming
Long Method Treatment:
Extract Method
Problem Solution
You have a code fragment that can be Move this code to a separate new
grouped together. method (or function) and replace the
old code with a call to the method.
Problem Solution
Problem Solution
Problem Solution
You get several values from an object Instead, try passing the whole object.
and then pass them as parameters to a
method.
You get several values from an object Instead, try passing the whole object.
and then pass them as parameters to a
method.
Problem Solution
Source: https://sourcemaking.com/refactoring/smells/large-class
COMP6047 - Algorithm and Programming
Large Class Problem
Classes usually start small. But over time, they get bloated as the program grows.
As is the case with long methods as well, programmers usually find it mentally less taxing to
place a new feature in an existing class than to create a new class for the feature.
• Extract Class helps if part of the behavior of the large class can be spun off into a
separate component.
• Extract Subclass helps if part of the behavior of the large class can be implemented in
different ways or is used in rare cases.
• Extract Interface helps if it is necessary to have a list of the operations and behaviors that
the client can use.
• If a large class is responsible for the graphical interface, you may try to move some of its
data and behavior to a separate domain object. In doing so, it may be necessary to store
copies of some data in two places and keep the data consistent. Duplicate Observed Data
offers a way to do this.
Source: https://sourcemaking.com/refactoring/smells/large-class
COMP6047 - Algorithm and Programming
Large Class: Extract Class
Problem Solution
When one class does the work of two, When one class does the work of two,
awkwardness results. awkwardness results.
Problem Solution
When one class does the work of two, When one class does the work of two,
awkwardness results. awkwardness results.
Problem Solution
Multiple clients are using the same part Move this identical portion to its own
of a class interface. Another case: part interface.
of the interface in two classes is the
same.
Problem Solution
• Use of primitives instead of small objects for simple tasks (such as currency, ranges,
special strings for phone numbers, etc.)
Source: https://sourcemaking.com/refactoring/smells/primitive-obsession
COMP6047 - Algorithm and Programming
Primitive Obsession Problem
Primitives are often used to "simulate" types. So instead of a separate data type, you have a
set of numbers or strings that form the list of allowable values for some entity. Easy-to-
understand names are then given to these specific numbers and strings via constants, which
is why they are spread wide and far.
• If you have a large variety of primitive fields, it may be possible to logically group some of
them into their own class. Even better, move the behavior associated with this data into
the class too. For this task, try Replace Data Value with Object.
• If the values of primitive fields are used in method parameters, go with
Introduce Parameter Object or Preserve Whole Object.
• When complicated data is coded in variables, use Replace Type Code with Class,
Replace Type Code with Subclasses or Replace Type Code with State/Strategy.
• If there are arrays among the variables, use Replace Array with Object.
Source: https://sourcemaking.com/refactoring/smells/primitive-obsession
COMP6047 - Algorithm and Programming
Primitive Obsession: Replace
Data Value with Object
Problem Solution
A class (or group of classes) contains a Create a new class, place the old field
data field. The field has its own and its behavior in the class, and store
behavior and associated data. the object of the class in the original
class.
Problem Solution
You have a coded type that directly Create subclasses for each value of the
affects program behavior (values of this coded type. Then extract the relevant
field trigger various code in behaviors from the original class to
conditionals). these subclasses. Replace the control
flow code with polymorphism.
Problem Solution
You have a coded type that directly Create subclasses for each value of the
affects program behavior (values of this coded type. Then extract the relevant
field trigger various code in behaviors from the original class to
conditionals). these subclasses. Replace the control
flow code with polymorphism.
Problem Solution
You have a coded type that affects Replace type code with a state object. If
behavior but you cannot use subclasses it is necessary to replace a field value
to get rid of it. with type code, another state object is
"plugged in".
Problem Solution
A class has a field that contains type Create a new class and use its objects
code. The values of this type are not instead of the type code values.
used in operator conditions and do not
affect the behavior of the program.
Source: https://sourcemaking.com/refactoring/smells/long-parameter-list
COMP6047 - Algorithm and Programming
Long Parameter List Problem
A long list of parameters might happen after several types of algorithms are merged in a
single method. A long list may have been created to control which algorithm will be run and
how. Long parameter lists may also be the byproduct of efforts to make classes more
independent of each other.
• Check what values are passed to parameters. If some of the arguments are just results of
method calls of another object, use Replace Parameter with Method Call. This object can
be placed in the field of its own class or passed as a method parameter.
• Instead of passing a group of data received from another object as parameters, pass the
object itself to the method, by using Preserve Whole Object.
• If there are several unrelated data elements, sometimes you can merge them into a
single parameter object via Introduce Parameter Object.
Source: https://sourcemaking.com/refactoring/smells/long-parameter-list
COMP6047 - Algorithm and Programming
Primitive Obsession: Replace
Parameter with Method Call
Problem Solution
Before a method call, a second method Instead of passing the value through a
is run and its result is sent back to the parameter, place the value-getting code
first method as an argument. But the inside the method.
parameter value could have been
obtained inside the method being
called.
Problem Solution
Before a method call, a second method Instead of passing the value through a
is run and its result is sent back to the parameter, place the value-getting code
first method as an argument. But the inside the method.
parameter value could have been
obtained inside the method being
called.
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.
Source: https://sourcemaking.com/refactoring/smells/data-clumps
COMP6047 - Algorithm and Programming
Data Clumps Problem
Often these data groups are due to poor program structure or "copypasta programming”.
If you want to make sure whether or not some data is a data clump, just delete one of the
data values and see whether the other values still make sense. If this is not the case, this is a
good sign that this group of variables should be combined into an object.
• If repeating data comprises the fields of a class, use Extract Class to move the fields to
their own class.
• If the same data clumps are passed in the parameters of methods, use
Introduce Parameter Object to set them off as a class.
• If some of the data is passed to other methods, think about passing the entire data
object to the method instead of just individual fields. Preserve Whole Object will help
with this.
• Look at the code used by these fields. It may be a good idea to move this code to a data
class.
Source: https://sourcemaking.com/refactoring/smells/long-parameter-list
COMP6047 - Algorithm and Programming
References