Don’t use flags as function parameters

Follow best practices and adhere to the single responsibility principle by not using flags as function parameters. Improve code comprehension and modularity.


Usage

πŸ“ Guideline

Don't Use Flags as Function Parameters: Functions should have a single responsibility and flags indicate that a function does more than one thing.

Flags are often used as function parameters to control the behavior of a function. However, this practice can make the code harder to understand and maintain. Instead, it is recommended to split the function into multiple smaller functions, each with a clear and single purpose.

πŸ› οΈ How to Apply

  • Split functions with flags into separate functions or methods, each responsible for a single task. πŸš€
  • Refactor the code to remove conditional statements based on flags and replace them with calls to the appropriate functions. πŸ”„
  • Ensure that each function or method has a clear and well-defined responsibility. 🎯

Pros and Cons

πŸ‘ Pros

  • Improved Readability: Functions with clear and single responsibilities are easier to understand, reducing cognitive load. πŸ“–
  • Enhanced Maintainability: Smaller, focused functions are easier to test, debug, and modify when needed. πŸ› οΈ
  • Modularity: By splitting functions based on different tasks, code becomes more modular, promoting code reuse. 🧩

πŸ‘Ž Cons

  • Increased Function Count: Splitting functions may result in an increase in the number of functions, which can make the codebase larger. πŸ“ˆ
  • Potential Overhead: In some cases, creating multiple functions instead of using flags might introduce slight overhead due to function call overhead. ⏱️
  • Code Duplication: Splitting functions without proper abstraction can lead to code duplication, which may introduce bugs and maintenance issues. 🐞

Examples

❌ Bad

// Bad: Using a flag to determine the behavior of the function
function drawShape(shape: string, fill: boolean) {
  if (fill) {
    // Draw filled shape
    console.log(`Drawing a filled ${shape}`);
  } else {
    // Draw outline only
    console.log(`Drawing an outlined ${shape}`);
  }
}
 
// Usage
drawShape("circle", true); // Draw a filled circle
drawShape("rectangle", false); // Draw an outlined rectangle

βœ… Good

// Good: Separate functions for each shape drawing task
function drawFilledShape(shape: string) {
  console.log(`Drawing a filled ${shape}`);
}
 
function drawOutlinedShape(shape: string) {
  console.log(`Drawing an outlined ${shape}`);
}
 
// Usage
drawFilledShape("circle"); // Draw a filled circle
drawOutlinedShape("rectangle"); // Draw an outlined rectangle

References

  • Single Responsibility Principle: Functions should have a single responsibility, which aligns with not using flags as function parameters. 🎯
  • Code Reusability: By splitting functions, code becomes more modular and can be reused in different contexts. πŸ”„
  • Separation of Concerns: Divide functions into smaller functions, each handling a specific concern. πŸ“¦