Concept Explanation
This lesson is about cleaning up working code without changing what the program actually does. That sounds simple, but beginners often make the same mistake here: they start refactoring, then quietly change the behavior at the same time. Once that happens, it becomes hard to tell whether the script is cleaner or just broken in a new way. A good refactor keeps the result the same while making the code easier to read, easier to explain, and easier to extend later. In this lesson, you will take a tiny script with repeated formatting and turn it into something more structured by moving the repeated logic into a helper function and keeping the entry point small. By the end of this lesson, you will be able to: • explain what it means to refactor without changing behavior, • spot repeated formatting or repeated string-building in a small script, • move that repetition into a clearly named helper function. Why this matters: once your files grow beyond a few lines, messy code slows you down more than hard syntax. Learning to clean code early saves you time in every lesson after this one.
Where to Put the Code
- Define color and position variables at the top.
- Create shape drawing or placement logic in the middle.
- Render output (print, canvas, SVG, or styled block) at the end.
Command Reference
- Refactor one small thing at a time, then run the file again before touching anything else.
- When the same formatting appears more than once, move it into a helper instead of rebuilding it in multiple places.
- Keep `main()` focused on orchestration: prepare values, call helpers, and print the final result.
- If the output changes during a refactor, stop and compare the old and new versions before continuing.
Step-by-step Guide
- Run the script once and write down the exact two lines it prints.
- Look for the part of the code that would become annoying to repeat if you needed five more names.
- Rename the helper function and variables so their purpose is obvious even without comments.
- Add a third example name with extra spaces and make sure the output is still clean and consistent.
- Write one sentence explaining what stayed the same after the refactor and what became easier to read.
Practice Exercises
- Take a short script you wrote earlier and find one repeated string or repeated print pattern you can move into a helper.
- Explain why `DEFAULT_NAME` is easier to maintain than writing the same fallback value in multiple places.
- Change the banner format once and confirm that every printed result updates automatically because the logic lives in one place.
Coding Challenges
- Add a second helper that returns a plain greeting line, then call both helpers from `main()` without making the file harder to read.
- Refactor the script so a future teammate could switch the banner style in one place only, with no changes to the print calls.
Mini Practice Tasks
- Use your editor’s rename feature once instead of renaming by hand everywhere.
- Run the file after every small change instead of waiting until the end.
- Write one note to yourself: clean code should be easier to understand, not just shorter.
Common Mistake
Mixing x and y axes or using wrong coordinate origin causes shapes to appear in unexpected places.
Real-life Mini Challenge
Draw one square, one triangle, and one circle, then move X marker 2 steps right and 1 step down.