Refactoring is essential
Introduction
What is refactoring?
// Before refactoring
package main
import "fmt"
func main() {
fmt.Println(add(2, 3))
fmt.Println(subtract(5, 2))
}
func add(x int, y int) int {
return x + y
}
func subtract(x int, y int) int {
return x - y
}
// After refactoring
package main
import "fmt"
func main() {
fmt.Println(calculate(2, 3, add))
fmt.Println(calculate(5, 2, subtract))
}
func add(x int, y int) int {
return x + y
}
func subtract(x int, y int) int {
return x - y
}
func calculate(x int, y int, operation func(int, int) int) int {
return operation(x, y)
}
This article presents a simple refactoring example in Go language. It starts with two basic functions for addition and subtraction. The code is then refactored to include a new function 'calculate' which accepts the operation as a parameter. This demonstrates how to make the code more flexible and reusable by abstracting the operation.
This is a block quote
/


