Welcome to your next step in mastering Clean Code! 🚀 Previously, we emphasized the significance of naming conventions in clean coding. Now, we delve into the realm of functions, which serve as the backbone of application logic and are crucial for code organization and execution. Structuring these functions effectively is vital for enhancing the clarity and maintainability of a codebase. In this lesson, we'll explore best practices and techniques to ensure our code remains clean, efficient, and readable.
Let's outline the key principles for writing clean functions:
- Keep functions small. Small functions are easier to read, comprehend, and maintain.
- Focus on a single task. A function dedicated to one task is more reliable and simpler to debug.
- Limit arguments to three or fewer. Excessive arguments complicate the function signature and make it difficult to understand and use.
- Avoid boolean flags. Boolean flags can obscure the code's purpose; consider separate methods for different behaviors.
- Eliminate side effects. Functions should avoid altering the external state or depending on external changes to ensure predictability.
- Implement the DRY principle. Employ helper functions to reuse code, minimizing redundancy and enhancing maintainability.
Now, let's take a closer look at each of these rules.
Functions should remain small, and if they become too large, consider splitting them into multiple, focused functions. While there's no fixed rule on what counts as large, a common guideline is around 15 to 25 lines of code, often defined by team conventions.
Below, you can see the processOrder
function, which is manageable but has the potential to become unwieldy over time:
Go1func processOrder(order Order, inventory Inventory, logger Logger) { 2 // Step 1: Validate the order 3 if !order.isValid() { 4 logger.log("Invalid Order") 5 return 6 } 7 8 // Step 2: Process payment 9 if !order.processPayment() { 10 logger.log("Payment failed") 11 return 12 } 13 14 // Step 3: Update inventory 15 inventory.update(order.items) 16 17 // Step 4: Notify customer 18 order.notifyCustomer() 19 20 // Step 5: Log order processing 21 logger.log("Order processed successfully") 22}
Given that this process involves multiple steps, it can be improved by extracting each step into a dedicated function, as shown below:
Go1func processOrder(order Order, inventory Inventory, logger Logger) { 2 if !validateOrder(order, logger) || !processPayment(order, logger) { 3 return 4 } 5 updateInventory(order, inventory) 6 notifyCustomer(order) 7 logOrderProcessing(logger) 8} 9 10// Step 1: Validate the order 11func validateOrder(order Order, logger Logger) bool { 12 if !order.isValid() { 13 logger.log("Invalid Order") 14 return false 15 } 16 return true 17} 18 19// Step 2: Process payment 20func processPayment(order Order, logger Logger) bool { 21 if !order.processPayment() { 22 logger.log("Payment failed") 23 return false 24 } 25 return true 26} 27 28// Step 3: Update inventory 29func updateInventory(order Order, inventory Inventory) { 30 inventory.update(order.items) 31} 32 33// Step 4: Notify customer 34func notifyCustomer(order Order) { 35 order.notifyCustomer() 36} 37 38// Step 5: Log order processing 39func logOrderProcessing(logger Logger) { 40 logger.log("Order processed successfully") 41}
A function should embody the principle of doing one thing only. If a function handles multiple responsibilities, it may include several logical sections. Below, you can see the saveAndNotifyUser
function, which is both too lengthy and does multiple different things at once:
Go1func saveAndNotifyUser(user User, dataSource DataSource, webClient WebClient) { 2 // Save user to the database 3 if err := saveUser(user, dataSource); err != nil { 4 fmt.Println(err) 5 return 6 } 7 8 // Send a welcome email to the user 9 if err := notifyUser(user, webClient); err != nil { 10 fmt.Println(err) 11 } 12}
To enhance this code, you can create two dedicated functions for saving the user and sending the welcome email. This results in dedicated responsibilities for each function and clearer coordination:
Go1// Save user to the database 2func saveUser(user User, dataSource DataSource) error { 3 stmt := "INSERT INTO users (name, email) VALUES (?, ?)" 4 conn, err := dataSource.getConnection() 5 if err != nil { 6 return err 7 } 8 defer conn.close() 9 10 _, err = conn.execute(stmt, user.name, user.email) 11 if err != nil { 12 return err 13 } 14 return nil 15} 16 17// Send a welcome email to the user 18func notifyUser(user User, webClient WebClient) error { 19 response, err := webClient.post("/sendWelcomeEmail", user) 20 if err != nil { 21 return err 22 } 23 24 if response.isError() { 25 return fmt.Errorf("Failed to send email") 26 } 27 return nil 28}
Try to keep the number of function arguments to a maximum of three, as having too many can make functions less understandable and harder to use effectively. 🤔
Consider the saveAddress
function below with five arguments, which makes the function less clean:
Go1func saveAddress(street, city, state, zipCode, country string) { 2 // Logic to save address 3}
A cleaner version encapsulates the details into an Address
struct type, reducing the number of arguments and making the function signature clearer:
Go1type Address struct { 2 Street, City, State, ZipCode, Country string 3} 4 5func saveAddress(address Address) { 6 // Logic to save address 7}
Boolean flags in functions can create confusion, as they often suggest multiple pathways or behaviors within a single function. Instead, use separate methods for distinct behaviors. 🚫
The setFlag
function below uses a boolean flag to indicate user status, leading to potential complexity:
Go1func setFlag(user User, isAdmin bool) { 2 // Logic based on flag 3}
A cleaner approach is to have distinct methods representing the different behaviors:
Go1func grantAdminPrivileges(user User) { 2 // Logic for admin rights 3} 4 5func revokeAdminPrivileges(user User) { 6 // Logic to remove admin rights 7}
A side effect occurs when a function modifies some state outside its scope or relies on something external. This can lead to unpredictable behavior and reduce code reliability.
Below, the addToTotal
function demonstrates a side effect by modifying an external state:
Go1// Not Clean - Side Effect 2func addToTotal(value int) int { 3 total += value // modifies external state 4 return total 5}
A cleaner version, calculateTotal
, performs the operation without altering any external state:
Go1// Clean - No Side Effect 🌟 2func calculateTotal(initial, value int) int { 3 return initial + value 4}
Avoid code repetition by introducing helper functions to reduce redundancy and improve maintainability.
The printUserInfo
and printManagerInfo
functions below repeat similar logic, violating the DRY principle:
Go1func printUserInfo(user User) { 2 fmt.Println("Name:", user.name) 3 fmt.Println("Email:", user.email) 4} 5 6func printManagerInfo(manager Manager) { 7 fmt.Println("Name:", manager.name) 8 fmt.Println("Email:", manager.email) 9}
To adhere to DRY principles, use a generalized printInfo
function that operates on an InfoProvider
interface:
Go1type InfoProvider interface { 2 GetName() string 3 GetEmail() string 4} 5 6func printInfo(info InfoProvider) { 7 fmt.Println("Name:", info.GetName()) 8 fmt.Println("Email:", info.GetEmail()) 9}
In this lesson, we learned that clean functions are key to maintaining readable and maintainable code. By keeping functions small, adhering to the Single Responsibility Principle, limiting arguments, avoiding side effects, and embracing the DRY principle, you set a strong foundation for clean coding. Next, we'll practice these principles to further sharpen your coding skills! 🎓