PythonpytestAdvanced~8hPart 5 of 6

Python Design

Make it hold its next change

The program runs and the tests pass. That is not the same as being able to change it. This is the part where you find out what a bad shape actually costs, take a working program apart into pieces that each own one decision, and then get handed a requirement nobody warned you about.

Modules
7
Drills
13
Build steps
15
Time
~8h

Walk out with

A single-file program rebuilt into modules and objects with its tests green throughout, and a new requirement that landed in one file.

What you'll do

01

Count what a change costs

Six places, or one. The number is the whole argument.

02

Split it across files

Imports, namespaces, and the four ways it goes wrong.

03

Decide what needs a class

Often nothing. Sometimes one thing. Know which.

04

Rebuild a working program

Tests green throughout, then a requirement you did not see.

The pitch

What you practise, and what you leave with

A shift scheduler rebuilt from one file into modules and objects, its suite green at every step, and a second site added in one place.

You will practise

  • Count the places one change has to touch
  • Say where a responsibility belongs
  • Choose between a class and four other shapes
  • Predict what an import binds and runs

Afterwards you can

  • Split a program across files without breaking it
  • Say what a change will cost before making it
  • Decide when something should not be a class
  • Reshape code with the tests green throughout

Modules

7 modules, 44 items

Lessons explain one idea. Drills repeat it until it sticks. Build steps make something that exists afterwards.

  1. 1

    What the change costs

    ~39 min

    Count the places one requirement has to touch, and name why.

    • LessonThe bill for a small change9 min
    • LessonThe same program, one place6 min
    • DrillCount what the change costs12 min
    • DrillName the structural trouble12 min
  2. 2

    Give the work a name

    ~56 min

    Split one job into named pieces, each of which says what it needs.

    • LessonOne function, four jobs8 min
    • LessonSay what the piece needs6 min
    • DrillSay what it needs12 min
    • Build stepCut at the seam16 min
    • Build stepGive the rule a name14 min
  3. 3

    More than one file

    ~1h

    Split the program across files and keep every name pointing where you meant.

    • LessonWhat an import actually does8 min
    • LessonTwo files that need each other7 min
    • DrillWhat the import binds12 min
    • DrillBreak the import cycle12 min
    • Build stepFive files20 min
    • Build stepChange a rule without opening the code12 min
  4. 4

    When an object earns it

    ~1h

    Decide what should be a class — and what should not.

    • LessonDoes this need to be a class?6 min
    • LessonWhat a class actually buys8 min
    • DrillPick the shape it should be12 min
    • DrillWhat the object holds now10 min
    • DrillWhich method breaks the rule12 min
    • DrillSpell it in Python10 min
    • Build stepWrite the one class18 min
  5. 5

    Objects that hold objects

    ~58 min

    Decide which unit owns a behaviour, and let the others ask.

    • LessonHas-a, and who answers6 min
    • LessonWhen the container is wrong8 min
    • DrillWhere the responsibility belongs12 min
    • Build stepMove the reaching-in16 min
    • Build stepA thing that holds the things16 min
  6. 6

    Inheritance, and where it stops

    ~52 min

    Tell an is-a from a has-a from a field, and know which body runs.

    • LessonIs-a, and the three ways it is not6 min
    • LessonWhat a base class promises8 min
    • DrillIs-a, has-a, or neither12 min
    • DrillWhich body runs10 min
    • Build stepA second output, and no new base class16 min
  7. 7

    The rebuild

    ~2h

    Reshape an unfamiliar program, keep it green, then absorb a change nobody warned of.

    • LessonMeet the program7 min
    • LessonThe order to do it in6 min
    • DrillDid the behaviour survive12 min
    • Build stepGreen first8 min
    • Build stepPull the policy out16 min
    • Build stepSplit the files18 min
    • Build stepThe one class16 min
    • Build stepRetire the dictionaries14 min
    • Build stepThe design log18 min
    • Build stepThe requirement nobody mentioned20 min
    • LessonWhat it cost6 min
    • LessonWhat you carry out of here5 min

It works. It is also the wrong shape.

The first item is free. ~8h of focused work, at your own pace.

Advanced~8h7 modules