Featured image of post Refactoring - Part 1

Refactoring - Part 1

Refactoring - Part 1

This post was inspired by the book Refactoring, which was written by Martin Fowler and Kent Beck.

Extract Function

The term “function” can be interchanged with “method” in an object-oriented language. The idea behinds it is that you look at a fragment of code, understand what it is doing, then extract it into its own function named after its purpose. With this principle, you can develop a habit of writing very small functions.

Take a look at the code below. We have a struct Rectangle and a method PrintInfo() to log the name and area of it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import "fmt"

type Rectangle struct {
	name   string
	width  int
	height int
}

func (s *Rectangle) PrintInfo() {
	fmt.Println("name:" + s.name)
	area := s.height * s.width
	fmt.Println("Area:" + fmt.Sprintf("%d", area))
}

func main() {
	rect := Rectangle{"Rect 1", 10, 5}
	rect.PrintInfo()
	// Name:Rect 1
	// Area:50
}

Applying the extract function rule here, we can extract calculation part area := s.height * s.width into a new method GetArea. Imagine you have a ton of calculations in PrintInfo(), the extract function will improve readability in your code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package main

import "fmt"

type Rectangle struct {
	name   string
	width  int
	height int
}

func (s *Rectangle) PrintInfo() {
	fmt.Println("name:" + s.name)
	fmt.Println("Area:" + fmt.Sprintf("%d", s.GetArea()))
}

func (s *Rectangle) GetArea() int {
	return s.height * s.width
}

func main() {
	rect := Rectangle{"Rect 1", 10, 5}

	rect.PrintInfo()

	// Name:Rect 1
	// Area:50
}
Built with Hugo
Theme Stack designed by Jimmy