DEV Community

Yadier Betancourt Martinez
Yadier Betancourt Martinez

Posted on • Originally published at javadevelop.hashnode.dev on

The Power of Small and Focused Functions in Software Development

Introduction

In software development, writing clean code is essential for creating high-quality applications that are easy to understand, maintain, and modify. One of the main principles of clean code is the use of small and focused functions.

What are small and focused functions?

Small and focused functions are those that focus on a single task and are as short as possible. Instead of having a single long function that performs multiple tasks, it is better to divide the function into several smaller ones, each of which handles a specific task.

For example, instead of having a long function that validates a user registration form, it is better to divide the function into several smaller and focused functions, such as one to check that the fields are complete, another to validate the email address, and another to check the password security.

Why are small and focused functions important?

Small and focused functions are important for several reasons:

  1. They improve code readability: Smaller functions are easier to read and understand, making the code easier to maintain and modify. Additionally, being more focused on a single task, it is easier to understand what each function does.

  2. They reduce the number of errors: If a function has an error, it is easier to find and correct if the function is smaller and more focused. Additionally, being focused on a single task, it is less likely that errors will be made in implementing the function.

  3. They facilitate code maintenance and modification: Smaller functions are easier to maintain and modify, reducing the time and effort required to make changes and corrections.

How to implement small and focused functions in code?

To implement small and focused functions in code, it is important to follow some key software design principles, such as the principle of single responsibility and the principle of separation of concerns.

The principle of single responsibility states that each module or class should have a single responsibility. In the context of functions, this means that each function should do one thing and do it well.

The principle of separation of concerns states that different concerns in a system should be separated into distinct modules or classes. In the context of functions, this means that different tasks should be separated into different functions.

By following these principles, we can create small and focused functions that are easy to understand, maintain, and modify.

Code examples

User registration form validation

To illustrate how to implement small and focused functions in code, consider the following example of a function that validates a user registration form:

//🚫 WRONG
public boolean validateForm() {
  // Check that fields are complete
  // Validate email address
  // Check password security
  if (fieldsComplete && validEmail && securePassword) {
    return true;
  } else {
    return false;
  }
}

Enter fullscreen mode Exit fullscreen mode

In this example, the validateForm function performs three different tasks: checking that fields are complete, validating the email address, and checking password security. To make this function smaller and more focused, it is recommended to divide it into three smaller and focused functions, each of which performs a specific task:

// RIGTH
public boolean validateForm() {
  if (fieldsComplete() && validEmail() && securePassword()) {
    return true;
  } else {
    return false;
  }
}

public boolean fieldsComplete() {
  // Check that fields are complete
}

public boolean validEmail() {
  // Validate email address
}

public boolean securePassword() {
  // Check password security
}

Enter fullscreen mode Exit fullscreen mode

In this example, the validateForm function calls three smaller and more focused functions: fieldsComplete, validEmail, and securePassword. Each of these functions performs a specific task and returns a boolean value indicating whether the task was completed or not.

Calculation of the total price of an online purchase

Another example of how to implement small and focused functions in code is calculating the total price of an online purchase. Consider the following example of a function that calculates the total price of an online purchase:

//🚫 WRONG
public double calculateTotalPrice() {
  double totalPrice = 0.0;
  // Calculate the price of each item
  // Apply discounts to the total price
  // Calculate tax on the total price
  return totalPrice;
}

Enter fullscreen mode Exit fullscreen mode

In this example, the calculateTotalPrice function performs three different tasks: calculating the price of each item, applying discounts to the total price, and calculating the tax on the total price. To make this function smaller and more focused, it is recommended to divide it into three smaller and more focused functions, each of which performs a specific task:

// RIGTH
public double calculateTotalPrice() {
  double totalPrice = 0.0;
  totalPrice += calculateItemPrice();
  totalPrice -= applyDiscounts();
  totalPrice += calculateTax();
  return totalPrice;
}

public double calculateItemPrice() {
  // Calculate the price of each item
}

public double applyDiscounts() {
  // Apply discounts to the total price
}

public double calculateTax() {
  // Calculate tax on the total price
}

Enter fullscreen mode Exit fullscreen mode

In this example, the calculateTotalPrice function calls three smaller and more focused functions: calculateItemPrice, applyDiscounts, and calculateTax. Each of these functions performs a specific task and returns a numerical value that is used to calculate the total price of the purchase.

Conclusion

Small and focused functions are a powerful tool in software development. By dividing a long function into several smaller and more focused functions, we can improve code readability, reduce the number of errors, and facilitate code maintenance and modification. Additionally, it is important to follow key software design principles to effectively implement small and focused functions in code.

Top comments (0)