[Tutor] When to use classes

Steven D'Aprano steve at pearwood.info
Fri Aug 18 22:56:08 EDT 2017


Mostly for Bob, but also for anyone else interested:

When To Use Classes

http://kentsjohnson.com/stories/00014.html


He says:

    You may have several functions that use the same state 
    variables, either reading or writing them. You are passing
    a lot of parameters around. You have nested functions that
    have to forward their parameters to the functions they use.
    You are tempted to make some module variables to hold the
    state.

    You could make a class instead!



Indeed you could, and sometimes you should, but we can see a problem 
here. The situation described is a bad situation to be in: there is too 
much coupling between functions, too much state being passed around.

Ideally we should try to *reduce the coupling* between components, if 
possible, and hence make the program less complex. OO design tries to 
encapsulate the coupling within (hopefully) a single class, but it does 
nothing to reduce the coupling or complexity.

So I would say:

- if your code is complex, with lots of connections between 
  components (coupling), try to reduce the coupling and make
  the code simper;

- if you can't, encapsulate it in classes.



-- 
Steve


More information about the Tutor mailing list