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).
What you will learn
- 1Spot common code smells in a short program
- 2Choose the refactor move that fixes each code smell
- 3Explain how loops, functions, and named constants 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 program 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 — a saved copy of your project still has the old lines.
- Nested ifs → check the special case first and stop early (an early return), so the checks do not pile up.
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.
Words to know
refactor
To clean up code without changing what it does.
code smell
A hint that part of the code is messier than it needs to be.
function
A named block of code that does one job and can be reused.
naming
Choosing words for variables and functions that say what they mean.
simplify
To make something easier to read and understand without losing meaning.
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,880 lessons, Grades 1–6, every subject
- ✓ Profiles + saved progress for every kid
- ✓ Printable lessons with answer key