Study with me for Code Refactoring ( Story2 — Code Smell)

Code smells can be found when working with complex team and across large team. It leads to serious failures and kill an application’s performance.

Typical examples of code smells are

  1. Bloaters

Bloaters

A Bloater smell represents a code element that has grown so large that it cannot be effectively handled.

Everyone does their best to write excellent code from scratch. When adding new features and functionalities , the code became large pieces of code that hard to read and maintain.

If you can identify any of the smells below your components , you can make more cleaner. Let’s dig in

  1. Large Class — are classes that span too many lines ( It is responsible for more tasks than it should be) . It can be the fields , methods and many lines of code
# Large Class
class LargeClass {
# Many class variables
# Many fields
# Many Methods
}

2. Long Method — are methods that span too many lines. Generally the lines of the function method are not greater than 10. But we need to split functions of their responsibility . I will explain more in the details next topics

# Long Method
fun longMethod() {
# Do first thing
# Do second thing
# Do third thing
# Do fourth thing
}

3. Primitive Obsession — uses primitive type instead of creating new small objects for simple tasks.

# Primitive Obsession
val dollars = 28.27
val cents = (dollars * 100 % 100).toInt()
print(cents)

4. Long parameter List — are methods that have many arguments (parameters). we need to change small object or data class when parameter counts in method are greater than three or four.

# Long parameter List
fun longParameterList(
val argument1: Any,
val argument2: Any,
val argument3: Any,
val argument4: Any,
val argument5: Any
)

5. Data Clumps — are sets of related primitives (eg. 1, 3.14, “Hello”, false) that always appear together. Data Clumps can be avoided by encapsulating them together in a class.

# Data Clumps
val xCoordinate = 4
val yCoordinate = -7
val zCoordinate = 43

distance = sqrt(
(xCoordinate*2) + (yCoordinate*2) + (zCoordinate*2)
) # Pythagorean theorem
print(distance)

This is the end of the story 2. We will the deep dive into symptoms and treatment for Bloaters in next Story

See you next time, bye bye 👋

--

--

Android Engineer @ Codigo - Mobile App Company Singapore

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store