Coding Review & Refactor
Lesson preview: the Mini Lesson and Vocabulary are open to everyone. Guided practice, exercises, the parent checklist and the printable PDF worksheet come with full access (free 7-day trial or membership).
Learning Objectives
- 1Spot common code smells in a short program
- 2Refactor messy code into shorter, clearer pieces
- 3Use loops, functions, and named constants to remove repetition
- 4Reflect on what improved and what still needs work
Mini Lesson
Real coders rarely get the code right on the first try. They write something that works, then come back and clean it up. That second pass is called a refactor — same behavior, better code. Refactoring is how a good program turns into a great one.
What is a Code Smell?
A code smell is a hint that something inside your program is messier than it needs to be. The code might still run — but it is hard to read, hard to fix, and easy to break. Code smells do not mean you wrote bad code. They mean it is time for a polish.
- Repeating the same lines — three identical print statements in a row.
- Magic numbers — a value like 86400 sitting in the code with no name.
- Long scripts — one giant block of code that does five different jobs.
- Confusing names — variables called x, y, and tmp instead of speed or score.
- Dead code — old lines that are never used or always skipped.
- Nested if-if-if — checks tucked three levels deep that are hard to follow.
The Refactor Moves
Each code smell has a simple fix. You do not rewrite the whole program — you make small, safe moves and run the tests after each one to make sure nothing broke.
- Repeating lines → wrap them in a loop.
- Magic numbers → store them in a named constant like SECONDS_PER_DAY.
- Long scripts → split them into small functions, each doing one job.
- Confusing names → rename variables and functions to say what they mean.
- Dead code → delete it — version control still has the history.
- Nested ifs → use an early return to handle the special case first.
The Refactor Loop
- Read your code out loud — the parts you stumble over are clues.
- Pick ONE smell to fix first. Small steps beat big rewrites.
- Make the change.
- Run the program (or its tests) to check it still works.
- Repeat until the code reads cleanly. Then stop — done is done.
Tip: refactor in tiny steps and run the program after each one. If it breaks, you only have one small change to undo — much safer than redoing everything.
Unlock the full HSEA system for 7 days.
Add your first kid profile and your 7 days start: every lesson in every grade, saved progress for each child. Nothing to set up, no card required, nothing to cancel.
- ✓ All 1,680 lessons, Grades 1–6, every subject
- ✓ Profiles + saved progress for every kid
- ✓ Printable lessons with answer key