Code Refracting
Technique2023-09-10 18:31 (KST)
Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations.
Refactoring
In software engineering, it means "reorganizing the structure of the code without changing the results."
No function is added when refactoring.
Sometimes it's easier to write a new code than refactoring.
Goal
To improve the quality of code and to make software development and maintenance efficient, it is carried out with the following purposes:
Improved readability
- Software becomes easier to understand.
- It makes it easier for developers to collaborate.
Improved Maintenance
- The maintenance process is simplified.
- It can reduce potential bugs by eliminating code duplication.
- Structuring and modularity make it easier to add new features or fix bugs
When
Code Review: You can improve and improve quality while reviewing code with fellow developers.
Just before adding features: it's easy to add new features to existing code, so existing code can be refactored.
Bug Fix: Refactoring in the process of fixing bugs makes it easier to understand and fix bugs.
Regular maintenance: improves and optimizes code periodically.
Way
Replace hard-coded numbers with constants
- Before
if (temperature > 100) { // boiling point
// Do something if temperature is above boiling
}
- After
val BOILING_POINT = 100
if (temperature > BOILING_POINT) {
// Do something if temperature is above boiling
}
To remove duplicate code
- Before
fun process(user: User) {
if (user.status == UserStatus.SPROUT) {
println("======= ## ${user.status} ## =======")
// ...
} else if (user.status == UserStatus.ROSE) {
println("======= && ${user.status} && =======")
// ...
} else if (user.status == UserStatus.TREE) {
println("======= @@ ${user.status} @@ =======")
// ...
}
}
- After
fun process(user: User) {
if (user.status == UserStatus.SPROUT) {
printStatus(user.status, "#")
// ...
} else if (user.status == UserStatus.ROSE) {
printStatus(user.status, "&")
// ...
} else if (user.status == UserStatus.TREE) {
printStatus(user.status, "@")
// ...
}
}
private fun printStatus(status: UserStatus, decorate: String) {
println("======= $decorate$decorate $status $decorate$decorate =======")
}
Use simple conditional statements like trisom operators (Kotlin does not have trisom operators)
- Before
var text: String
if (zipCode != null) {
text = "$zipCode) $address"
} else {
text = address
}
- After
val text = if (zipCode != null) "$zipCode) $address" else address
val text = zipCode?.let { "$zipCode) $address" } ?: address
For conditional statements with multiple conditions, when is sometimes better.
- Before
val agePeriod = if (age >= 65) "old age"
else if (age >= 45) "middle age"
else if (age >= 30) "midlife"
else if (age >= 19) "young adulthood"
else if (age >= 7) "boyhood"
else "one's"
- After
val agePeriod = when {
age >= 65 -> "old age"
age >= 45 -> "middle age"
age >= 30 -> "midlife"
age >= 19 -> "young adulthood"
age >= 7 -> "boyhood"
else -> "one's"
}
To isolate a long method
- Before
fun process() {
// Logic for validation
// ...
// Logic for calculations
// ...
// Logic for state changes
// ...
}
- After
fun process() {
validation()
caluration()
changeState()
}
fun validation() {
// Logic for validation
}
fun caluration() {
// Logic for calculations
}
fun changeState() {
// Logic for state changes
}
Use meaningful names (variable names, method names, etc.)
- Before
fun process(num: Int): Boolean {
return if (num % 4 == 0) !(num % 400 != 0 && num % 100 == 0) else false
}
- After
fun checkLeapYear(year: Int): Boolean {
return if (year % 4 == 0) !(year % 400 != 0 && year % 100 == 0) else false
}
https://www.techtarget.com/searchapparchitecture/definition/refactoring
https://dev.to/documatic/5-code-refactoring-techniques-to-improve-your-code-2lia
https://woogong80.tistory.com/213#google_vignette
https://velog.io/@rlrhs11/Code-Refactoring코드-리펙토링-이란
https://ikkison.tistory.com/82
https://jeongkyun-it.tistory.com/66
https://dmdwn3979.tistory.com/13
https://2minmin2.tistory.com/72
https://taes-k.github.io/2-where-when-refactoring.html
Previous Article
Framework vs Library vs API
Technique