Functions & Classes

Functions & Classes#

The primary tool for reusable code: the function.

Benefits
  • Modularity: break problems into small pieces

  • Abstraction: hide the “how,” expose the “what”

  • Testability: verify functions in isolation

  • Collaboration: team members build different functions

Good design
  • Descriptive verb-noun name (calculate_mean, not do_math)

  • Single responsibility: one function, one job

  • No global variables: pass everything as arguments

More advanced codebases can benefit greatly from a more versatile structural coding pattern: the class.

Benefits
  • Encapsulation: bundle data and behavior

  • State management: retain data across operations

  • Inheritance: extend and reuse existing structures

Good design
  • Descriptive noun name (PascalCase, e.g., DataProcessor)

  • High cohesion: methods tightly related to class data

  • Low coupling: minimized dependencies on external components