Code Refracting
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
Kotlin
1
2
3
if (temperature > 100) { // boiling point
// Do something if temperature is above boiling
}After
Kotlin
1
2
3
4
5
val BOILING_POINT = 100
if (temperature > BOILING_POINT) {
// Do something if temperature is above boiling
}
To remove duplicate code
Before
Kotlin
1
2
3
4
5
6
7
8
9
10
11
12
fun process(user: User) {
if (user.status == UserStatus.SPROUT) {
println("======= ## NULL ## =======")
// ...
} else if (user.status == UserStatus.ROSE) {
println("======= && NULL && =======")
// ...
} else if (user.status == UserStatus.TREE) {
println("======= @@ NULL @@ =======")
// ...
}
}After
Kotlin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
Kotlin
1
2
3
4
5
6
var text: String
if (zipCode != null) {
text = "$zipCode) $address"
} else {
text = address
}After
Kotlin
1
val text = if (zipCode != null) "$zipCode) $address" else addressKotlin
1
val text = zipCode?.let { "$zipCode) $address" } ?: addressFor conditional statements with multiple conditions, when is sometimes better.
Before
Kotlin
1
2
3
4
5
6
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
Kotlin
1
2
3
4
5
6
7
8
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
Kotlin
1
2
3
4
5
6
7
8
fun process() {
// Logic for validation
// ...
// Logic for calculations
// ...
// Logic for state changes
// ...
}After
Kotlin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
Kotlin
1
2
3
fun process(num: Int): Boolean {
return if (num % 4 == 0) !(num % 400 != 0 && num % 100 == 0) else false
}After
Kotlin
1
2
3
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

민갤
Back-End Developer
백엔드 개발자입니다.

Framework vs Library vs API
framework,library,api
Technique

[Spring Boot] ERROR: No tests found for given includes
How to troubleshoot spring boot errors
Spring Boot