Skip to content

Functions

  • Keep Functions Small: Functions should be concise and focused, accomplishing a single task without unnecessary complexity. Aim for brevity and clarity in function implementation.
  • Single Responsibility Principle (SRP): Functions should have a clear purpose and perform one task effectively. Avoid mixing multiple functionalities within a single function, as this can lead to code that is difficult to understand and maintain.
  • One Level of Abstraction: Each function should operate at a consistent level of abstraction, meaning that it should either perform low-level operations or coordinate higher-level logic, but not both. This ensures readability and facilitates easier debugging and modification.
  • Avoid Side Effects: Functions should not modify external state or have unintended consequences outside of their defined scope. Minimizing side effects enhances predictability and simplifies testing and debugging.
  • Prefer Exceptions Over Error Codes: When encountering errors or exceptional conditions, prefer raising exceptions rather than returning error codes. Exception handling provides a more structured and flexible approach to error management, improving code readability and maintainability.