Binus University Code Reengineering Bad Code Smell - Bloater

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Subject : CODE REENGINEERING

Year : 2017

Bad Code Smell: The


Bloater
Session 03 & 04
Learning Outcomes

LO 1: Evaluate Basic refactoring and its application

COMP6047 - Algorithm and Programming


Sub Topics

- Long Method
- Large Classes
- Primitive Obsession
- Long Parameter List
- Data Clumps

COMP6047 - Algorithm and Programming


Bad Code Smell

Group Smell Code Description

The Bloaters Long method It seems likely that these smells grow a


little bit at a time. Hopefully nobody
designs, e.g., Long Methods

Large Class A classes that handle everything

Primitive Obsession Primitive Obsession is actually more of a


symptom that causes bloats than a bloat
itself

Long parameter list Since these data items are not


encapsulated in a class this increases the
Data Clumps sizes of methods and classes

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.

To reduce the length of a method body, use Extract Method.

If local variables and parameters interfere with extracting a method, use 


Replace Temp with Query, Introduce Parameter Object or Preserve Whole Object.

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.

COMP6047 - Algorithm and Programming


Long Method Treatment:
Replace Temp with Query

Problem Solution

You place the result of an expression in Move the entire expression to a


a local variable for later use in your separate method and return the result
code. from it. Query the method instead of
using a variable. Incorporate the new
method in other methods, if necessary.

COMP6047 - Algorithm and Programming


Long Method Treatment:
Introduce Parameter Object

Problem Solution

Your methods contain a repeating Replace these parameters with an


group of parameters. object.

COMP6047 - Algorithm and Programming


Long Method Treatment:
Preserved Whole Object

Problem Solution

You get several values from an object Instead, try passing the whole object.
and then pass them as parameters to a
method.

COMP6047 - Algorithm and Programming


Long Method Treatment:
Replace Method with Method
Object
Problem Solution

You get several values from an object Instead, try passing the whole object.
and then pass them as parameters to a
method.

COMP6047 - Algorithm and Programming


Long Method Treatment:
Decompose Conditional

Problem Solution

You have a complex conditional (if- Decompose the complicated parts of


then/else or switch). the conditional into separate methods:
the condition, then and else.

COMP6047 - Algorithm and Programming


Bloater: Large Class

A class contains many fields/methods/lines of code.

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.

COMP6047 - Algorithm and Programming


Large Class: Extract Subclass

Problem Solution

When one class does the work of two, When one class does the work of two,
awkwardness results. awkwardness results.

COMP6047 - Algorithm and Programming


Large Class: Extract Interface

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.

COMP6047 - Algorithm and Programming


Large Class: Duplicate Observed
Data

Problem Solution

Is domain data stored in classes Then it is a good idea to separate the


responsible for the GUI? data into separate classes, ensuring
connection and synchronization
between the domain class and the GUI.

COMP6047 - Algorithm and Programming


Bloater: 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 USER_ADMIN_ROLE = 1 for


referring to users with administrator rights.)

• Use of string constants as field names for use in data arrays.

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.

COMP6047 - Algorithm and Programming


Primitive Obsession: Replace
Type code with Subclasses

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.

COMP6047 - Algorithm and Programming


Primitive Obsession: Replace
Array with Object

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.

COMP6047 - Algorithm and Programming


Primitive Obsession: Replace
Type Code with State/Strategy

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".

COMP6047 - Algorithm and Programming


Primitive Obsession: Replace
Type Code with Class

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.

COMP6047 - Algorithm and Programming


Bloater: Long Parameter List

• More than three or four parameters for a method.

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.

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.

COMP6047 - Algorithm and Programming


Bloater: 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.

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

Book: Principle-Based Refactoring

Book: Improving the Design of Existing Code, Martin Flower

You might also like