This post was inspired by the book Refactoring, which was written by Martin Fowler and Kent Beck.
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.
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.