💻 TechnologyGrade 5Lesson 31
← Back🔒 PDF with full access

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.

  1. Repeating lines → wrap them in a loop.
  2. Magic numbers → store them in a named constant like SECONDS_PER_DAY.
  3. Long scripts → split them into small functions, each doing one job.
  4. Confusing names → rename variables and functions to say what they mean.
  5. Dead code → delete it — version control still has the history.
  6. Nested ifs → use an early return to handle the special case first.

The Refactor Loop

  1. Read your code out loud — the parts you stumble over are clues.
  2. Pick ONE smell to fix first. Small steps beat big rewrites.
  3. Make the change.
  4. Run the program (or its tests) to check it still works.
  5. Repeat until the code reads cleanly. Then stop — done is done.
Before / After RefactorBefore / After RefactorMESSYprint("hi")print("hi")print("hi")x = 86400def doStuff(): if a: if b: if c:refactorCLEANfor i in range(3): print("hi")SECONDS_PER_DAY = 86400def greet(): if not a: return
Refactor ChecklistRefactor ChecklistShorterClearer namesSmaller piecesDelete dead codeTest still passesSame behavior. Better code.
💡

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.

Vocabulary
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.
Free · 7 days · No credit card

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
Start your 7-day free trial →
← Lesson 30: Code a Story or QuizLesson 32: Coding Showcase Rehearsal →