Class organization

  • (Java: List of variables first, in order: public static constants, private static variables, private instance variables)
  • Python:
    • Class variables first
    • Then "magic" functions (__init__, __length__ etc)
    • Then public functions
  • Private utility functions called by a function should be after the function calling it (stepdown rule)

Classes should be small

  • Classes should have only a single responsibility (SRP)
    • Only one reason to change
  • Cohesion: "Each method of a class should manipulate one or more of the instance variables"
  • High cohesion: When each instance variable is used by many methods. We want high cohesion.
  • Maintaining cohesion results in many small classes

Discussion

  • Should one collect similar "static" functions in class in Python?
  • Position of methods within a class in Python
  • Getters and setters
    • Using functions / decorators

Classes

By ivarg

Classes

  • 384