Study with me for Code Refactoring ( Story 6— Long Parameter List & Data Clumps)

Thaw Zin Toe
3 min readFeb 20, 2023

Long Parameter List — is a parameter lists more than three or four parameters of a method

Sign & Symptoms

fun createAccount(
name: String,
email: String,
phone: String,
address: String,
city: String,
state: String,
country: String,
zipCode: String,
creditCardNumber: String,
expirationDate: String,
cvv: String
)

How to solve it?

Replace parameter with Method calls — used to simplify a function’s parameter list by replacing one or more of the parameters with a method call that returns the required value.

Problem

class UserProfileViewModel : ViewModel() {
fun updateUserProfile(userId: String, name: String, email: String) {
val profile = Profile(userId, name, email)
saveUserProfile(profile)
}
}

Solution

class UserProfileViewModel : ViewModel() {
fun updateUserProfile(name: String, email: String) {
val userId = getUserId()
val profile = Profile(userId, name, email)
saveUserProfile(profile)
}

fun getUserId(): String {
// retrieve from data base
}
}

Benefit

  • rid off unneeded parameter
  • simplify method call
  • Encapsulation
  • Reduced errors

Instead of passing a bunch of data to a method and receiving from another object as parameters — use Preserve Whole Object.

Problem

class Calculator {
fun performCalculation(
val x: Double,
val y: Double,
val z: Double,
val a: Double,
val b: Double,
val c: Double,
val d: Double
){
// perform calculation for result
}
}

Solution

data class CalculatorInput(
val x: Double,
val y: Double,
val z: Double,
val a: Double,
val b: Double,
val c: Double,
val d: Double
)

class Calculator {
fun performCalculation(input: CalculationInput): Double {
// perform calculation for result
}
}

If there are many several unrelated data parameters. To avoid this issue, you can create data class or use Domain Specific Language (DSL) or use Introduce Parameter Object.

Problem

fun processData(data: List<Any>) {
// Perform some operations
}

val data = listOf("apple", 42, true, 3.14, "banana")
processData(data)

Solution

data class DataParams(
val strings: List<String>,
val numbers: List<Number>,
val booleans: List<Boolean>
)

fun processData(params: DataParams) {
// Perform some operations
}

Data Clumps

In software development, it is common to use the same group of variables across multiple parts of the code. When these groups of variables are repeated across different parts of the code, it can make the code less maintainable and harder to understand. This is referred to as a “Data Clump”.

Often these data groups are due to poor program structure or “copypasta programming”.

How to solve it?

  • If repeating data comprises the fields of a class, use Extract Class to move the fields to their own class. (already explain in early article)
  • If the same data clumps are passed in the parameters of methods, use Introduce Parameter Object to set them off as a class. (already explain in early article)
  • 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. (already explain in early article)

Benefit

  • Reduce Code Size
  • Improve code readability, maintainability
  • Better organization for code

This is the end of bloaters for code smell.

See you next time, bye-bye 👋

Story 1 — https://thawzintoe.medium.com/study-with-me-for-code-refactoring-story1-introduction-50d0f9b95cee

Story 2 — https://thawzintoe.medium.com/study-with-me-for-code-refactoring-story2-code-smell-4a5dbac61a0b

Story 3 — https://thawzintoe.medium.com/study-with-me-for-code-refactoring-story3-long-method-of-bloaters-461473504a1b

Story4 — https://medium.com/@thawzintoe/study-with-me-for-code-refactoring-story4-large-class-of-bloaters-24e4aa472020

Story5 — https://medium.com/@thawzintoe/study-with-me-for-code-refactoring-story-5-primitive-obsession-of-bloaters-2890c69adb15

Reference

https://blog.susomejias.dev/blog/code-smell-data-clumps

--

--