This article explores high-order functions that accept or return functions, enhancing code modularity, readability, and maintainability through efficient composition.
In this lesson, we explore high-order functions—functions that either accept another function as an argument or return a function as output. High-order functions enable modular composition, allowing you to build complex operations from small, focused functions. This approach not only minimizes bugs but also enhances code readability and maintainability.
Mastering high-order functions empowers you to write more efficient and maintainable code. For example, consider calculating various properties of a circle. Given a circle’s radius, users might want to compute its area, perimeter, or diameter by choosing the corresponding query (1, 2, or 3).
High-order functions allow you to abstract recurring operations, making your code scalable as you incorporate additional properties or shapes.
Begin by defining basic functions to calculate the area, perimeter, and diameter of a circle. Each function receives the circle’s radius as a float64 and returns a float64 value.
A common approach is to use conditional statements to decide which calculation to perform based on user input. The following code demonstrates this method:
Copy
Ask AI
func main() { var query int var radius float64 fmt.Print("Enter the radius of the circle: ") fmt.Scanf("%f", &radius) fmt.Printf("Enter \n 1 - area \n 2 - perimeter \n 3 - diameter: ") fmt.Scanf("%d", &query) if query == 1 { fmt.Println("Result: ", calcArea(radius)) } else if query == 2 { fmt.Println("Result: ", calcPerimeter(radius)) } else if query == 3 { fmt.Println("Result: ", calcDiameter(radius)) } else { fmt.Println("Invalid query") }}
When running this program, the console interaction might look like:
Copy
Ask AI
>>> go run main.goEnter the radius of the circle: 9.1Enter 1 - area 2 - perimeter 3 - diameter: 1Result: 260.0234Thank you!
While this solution works, it can quickly become cumbersome as you add more shape properties. Leveraging high-order functions simplifies the code and enhances scalability.
The following sections demonstrate how to refactor the code using high-order functions. We introduce two helper functions: one to display the result and another to map the query number to the appropriate calculation function.
Refactored Main Function Using High-Order Functions
By integrating these high-order functions, the main function becomes succinct. It reads user input, selects the appropriate calculation function via getFunction, and passes it to printResult.
Copy
Ask AI
func main() { var query int var radius float64 fmt.Print("Enter the radius of the circle: ") fmt.Scanf("%f", &radius) fmt.Printf("Enter \n 1 - area \n 2 - perimeter \n 3 - diameter: ") fmt.Scanf("%d", &query) printResult(radius, getFunction(query))}
Upon running the refactored program, you may observe the following console interaction:
Copy
Ask AI
>>> go run main.goEnter the radius of the circle: 7Enter 1 - area 2 - perimeter 3 - diameter: 3Result: 14Thank you!
High-order functions reduce code redundancy while clarifying the logical separation between input handling and processing logic—making it easier to extend and maintain your application.
By adopting high-order functions, you enhance code clarity, modularity, and scalability. Try integrating these concepts into your projects to unlock more robust and maintainable code designs.