Python
Intermediate
40 mins
Teacher/Student led
+85 XP

Documenting Your Code

Read a deliberately unclear Python program, then document it by renaming variables and adding comments. You will not write new code, only make existing code clear enough for someone else to understand.

Teacher Class Feed

Load previous activity

    1 - Start: Code Someone Else Has to Read ~4 mins

    Illustration for Start: code someone else has to readYou will read a short working Python program that scores GAA matches, and you will meet it exactly as a stranger would: named with single letters, no comments, nothing to tell you what it does.

    Key point

    Then you will document it, giving the names real meaning and adding comments, until another person could pick it up and follow it. Documentation is the skill today, not new code.

    2 - Read the Mystery Program ~8 mins

    Key point

    Read it, but do not run it yet. Predict what it prints for the two matches listed, then work out what a, b and t each stand for. This is deliberately hard to read, that is the whole point.

    Now run it and check your prediction against the output.

    def s(a, b):
        t = a * 3 + b
        return t
    
    m1 = s(2, 8)
    m2 = s(1, 15)
    print(m1)
    print(m2)

    3 - Give the Names Meaning ~8 mins

    Key point

    The fastest way to document code is to rename things so they explain themselves. Rewrite the program with real names: score_match for the function, goals and points for what goes in, total for the result, and match1 and match2 for the two matches.

    Type this over your program, then run it. The output is exactly the same, 14 then 18, because renaming changes nothing the computer does. It only changes what a reader understands.

    def score_match(goals, points):
        total = goals * 3 + points
        return total
    
    match1 = score_match(2, 8)
    match2 = score_match(1, 15)
    print(match1)
    print(match2)

    4 - Add Comments That Earn Their Place ~8 mins

    Key point

    Comment the why, not the what. Do not comment the obvious: print(match1) # prints match1 is noise.

    Now add comments. A comment starts with # and Python ignores it, so it is there purely for the reader. Add a comment above the function saying what it does, and one on the tricky line explaining why goals are multiplied by 3.

    Run it to confirm the comments changed nothing about how it works.

    # Work out a GAA match score in total points.
    # A goal is worth 3 points, a point is worth 1.
    def score_match(goals, points):
        total = goals * 3 + points   # 3 points per goal, plus the points
        return total
    
    match1 = score_match(2, 8)
    match2 = score_match(1, 15)
    print(match1)
    print(match2)

    5 - Your Turn ~6 mins

    Open one of your own earlier Python programs and document it the same way. If it is missing from the shared device, ask your teacher or quickly retype a short one you remember.

    Done looks like:

    • Every single-letter or vague name replaced with one that says what it holds.
    • One comment at the top saying what the program does.
    • One comment on the trickiest line explaining why, not what.
    • The program still runs and gives the same result as before.
    123learn · Online learning platform

    Unlock the full learning experience

    You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.

    Hundreds of curriculum-aligned lessons
    Interactive activities in every lesson
    Printable resources & progress tracking
    Copyright Notice
    This lesson is copyright of Coding Ireland 2017 - 2025. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more