Intermediate35 minRefactoringReuse

Refactoring for Reuse

Clean up repetitive JavaScript so features stay readable, testable, and easier to extend.

Learning Objectives

By the end of this lesson, you'll be able to:

  • βœ“ Recognise when working code has become too repetitive
  • βœ“ Extract small helper functions with clearer responsibilities
  • βœ“ Separate feature setup, state updates, and rendering concerns
  • βœ“ Refactor without changing the user-facing goal

Why This Matters:

Refactoring keeps plain JavaScript approachable as features get more realistic.

Before You Start:

You should be familiar with:

Spotting Repetition

Repetition in JavaScript often shows up as the same DOM query in multiple places, the same formatting logic repeated for different states, or several event handlers that all rebuild the UI in nearly identical ways.

Extracting Helpers

Helper functions are useful when a repeated step has one clear purpose: formatting a label, filtering records, toggling a class set, rendering a message, or resetting a form state. The goal is not many tiny functions. The goal is fewer repeated ideas.

  1. Before

    Repeated selectors, repeated updates, and mixed responsibilities make the feature tiring to maintain.

  2. After

    Small helpers and clearer boundaries make the state and render flow easier to follow.

Refactoring is not about cleverness. It is about turning repeated effort into named, reusable structure.

Feature Boundaries

A healthy plain-JavaScript feature often has three boundaries: setup, state changes, and rendering. The exact code can vary, but separating those concerns helps you debug, reuse, and extend the feature with less friction.

Running Example: Refactoring the Quiz Flow

Instead of rebuilding question logic in several places, this Quiz Game version extracts a few helpers and keeps rendering responsibilities together. See the full Quiz Game project

script.js

Why this structure helps: Small helpers like getCurrentQuestion and getProgressLabel reduce repetition without hiding what the feature is doing.

⏸️ Checkpoint for Understanding

Before moving forward, can you answer these?

  1. What kind of repetition often signals that refactoring would help?
  2. Why is it useful to separate setup, state changes, and rendering?
Check Your Answers
  1. Repeated selectors, repeated conditionals, repeated DOM updates, or multiple handlers that mostly do the same work.
  2. Because each part becomes easier to understand, test, and change without disturbing the others.

How confident are you with this concept?

πŸ˜• Still confused | πŸ€” Getting there | 😊 Got it! | πŸŽ‰ Could explain it to a friend!

Guided Practice: Refactor a Small Feature Plan

Start from a working but repetitive feature rather than from broken code.

Step 1: Highlight the repeated lines or ideas

Look for selectors, conditions, or DOM updates that appear more than once. Group them by purpose.

πŸ’‘ Need a hint?
Repeated ideas matter more than repeated line text.

Step 2: Name one helper worth extracting

Choose one repeated responsibility and give it a plain-English name such as renderResults, updateStatusMessage, or setActiveFilter.

πŸ’‘ Need a hint?
Strong names make the refactor easier to trust.

Step 3: Re-separate the feature flow

Write a short plan that shows setup code, update logic, and rendering as distinct responsibilities.

πŸ’‘ Need a hint?
The feature should feel easier to follow after the rewrite.

You're on track if you can:

  • ☐ You identified one worthwhile repeated responsibility
  • ☐ You named a helper clearly
  • ☐ You separated the feature flow into cleaner responsibilities

πŸ’ͺ Independent Practice

Refactor a builder-path feature conceptually before changing code.

Your Task:

Choose a feature such as a filterable gallery, quiz, calculator, or form. Describe three parts: the repetitive code smell, the helper or structure you would extract, and the cleaner flow after refactoring. Focus on readability and responsibility, not on advanced patterns.

Requirements:
  • Name the repeated problem clearly
  • Describe at least one extracted helper or boundary
  • Explain how the refactor improves future maintenance
Stretch Goals (Optional):
  • Note one variable or selector you would centralise
  • Explain how the new structure would help debugging

Success Criteria:

CriteriaYou've succeeded if...
Problem diagnosisThe learner identifies meaningful repetition instead of cosmetic preferences.
Refactor qualityThe proposed helpers or boundaries make the feature easier to read.
User focusThe refactor keeps the same user-facing goal while improving structure.

Recap

  • Refactoring is about clearer responsibility, not clever abstraction.
  • Repeated selectors, conditions, and UI updates are strong cleanup signals.
  • Helpful plain-JavaScript boundaries include setup, state changes, and rendering.

Lesson Complete: What You Learned

Key Takeaways:

  • Working code can still be hard to maintain if repetition keeps growing.
  • Refactoring is strongest when it removes repeated responsibility clearly.
  • Small helpers and clearer feature boundaries make debugging and extension easier.

Learning Objectives Review:

Look back at what you set out to learn. Can you now:

  • βœ… Spot stronger refactoring opportunities Check!
  • βœ… Extract reusable helpers more deliberately Got it!
  • βœ… Separate feature responsibilities more clearly Can explain it!

If you can confidently answer "yes" to most of these, you're ready to move on!

🎯 Looking Ahead:

The final lesson combines state, rendering, validation, debugging, and reuse in one bounded capstone feature plan.

Recommended Next Steps

Practice Projects

Apply what you've learned with these hands-on projects:

Todo List 2

A strong candidate for extracting repeated list, filter, and render logic.

JavaScriptArrays
Start Project

Calculator

Useful for thinking about helper functions and clearer feature boundaries.

JavaScriptApp
Start Project

Progress tracking is disabled. Enable it in to track your completed tutorials.