Skip to content
Computer Science · Grade 11 · Object-Oriented Programming and Design · Term 2

Refactoring and Code Quality

Explore techniques for improving existing code without changing its external behavior, focusing on readability, maintainability, and efficiency.

Ontario Curriculum ExpectationsCS.HS.D.2CS.HS.P.5

About This Topic

Refactoring improves existing code without altering its external behavior. Students focus on techniques that enhance readability through clear variable names and concise functions, boost maintainability with modular structures, and increase efficiency by removing redundancies. In the Ontario Grade 11 Computer Science curriculum, this topic builds on object-oriented programming principles from Unit 2. Students analyze 'bad code' characteristics like long methods or duplicated logic, then apply refactoring strategies to real examples.

This content addresses key questions about code quality in long-term projects. High-quality code reduces bugs, eases collaboration, and supports scalable software design. Students practice identifying issues in provided snippets and redesigning them, connecting to standards CS.HS.D.2 on design processes and CS.HS.P.5 on problem-solving with algorithms.

Active learning suits refactoring because students gain skills through iterative practice. Pair programming lets them critique and revise code together, while group challenges with timed improvements make abstract concepts concrete and foster peer feedback.

Key Questions

  1. Analyze the characteristics of 'bad code' and how refactoring addresses them.
  2. Explain the importance of maintaining code quality in long-term software projects.
  3. Design a refactoring strategy for a given piece of inefficient or hard-to-read code.

Learning Objectives

  • Analyze common code smells in provided code snippets, identifying specific indicators of poor quality.
  • Evaluate the impact of specific refactoring techniques on code readability and maintainability.
  • Design a refactoring plan for a given code segment, outlining the steps to improve its structure and clarity.
  • Compare the efficiency of different code implementations for the same task before and after refactoring.
  • Critique refactored code, assessing whether external behavior has been preserved and quality has improved.

Before You Start

Introduction to Object-Oriented Programming

Why: Understanding classes, objects, methods, and properties is foundational for refactoring object-oriented code.

Basic Programming Constructs

Why: Students need a solid grasp of variables, data types, control flow (loops, conditionals), and functions to identify and modify code.

Key Vocabulary

Code SmellA surface indication in the source code that potentially points to a deeper problem. Examples include duplicated code or long methods.
RefactoringThe process of restructuring existing computer code without changing its external behavior. It aims to improve non-functional attributes of the software.
ReadabilityThe ease with which human readers can comprehend the purpose, structure, and operation of source code. Clear naming and formatting contribute to readability.
MaintainabilityThe ease with which software can be modified to correct defects, improve performance, or adapt to a changed environment. Well-structured code is more maintainable.
Duplicated CodeIdentical or very similar code structures appearing in multiple places within a program. This violates the 'Don't Repeat Yourself' principle.

Watch Out for These Misconceptions

Common MisconceptionRefactoring always changes what the code does.

What to Teach Instead

Refactoring preserves external behavior while improving internals. Students test outputs before and after to verify this. Active pair testing helps them see unchanged results firsthand and builds confidence in safe changes.

Common MisconceptionGood code just needs more comments.

What to Teach Instead

Comments help but self-documenting code with clear names and structure is better. Group code reviews reveal when refactoring reduces comment reliance. Peer discussions clarify why readable code trumps heavy commenting.

Common MisconceptionRefactoring is only for fixing bugs.

What to Teach Instead

Refactoring prevents bugs by improving structure proactively. Hands-on challenges with 'working but smelly' code show students how small changes enhance maintainability. Collaborative redesigns highlight long-term benefits over quick fixes.

Active Learning Ideas

See all activities

Real-World Connections

  • Software developers at Google use refactoring daily to improve the vast codebase of Android applications. This ensures new features can be added efficiently and bugs are easier to fix, impacting millions of users worldwide.
  • Game development studios like Ubisoft employ refactoring to optimize game engines for performance and stability. This process is crucial for delivering smooth gameplay experiences across different gaming platforms, from PCs to consoles.
  • Financial technology companies, such as Wealthsimple, rely on refactored code for their trading platforms. Maintaining high code quality is essential for security, speed, and reliability in handling sensitive financial transactions.

Assessment Ideas

Quick Check

Present students with a short code snippet exhibiting a common code smell (e.g., duplicated code). Ask them to identify the smell and write one sentence explaining why it is problematic. Collect responses to gauge understanding.

Discussion Prompt

Pose the question: 'Imagine you are joining a project with a large, existing codebase. What are the first three things you would look for to assess its quality and identify areas for refactoring?' Facilitate a class discussion on their responses.

Peer Assessment

In pairs, students refactor a given piece of code. After refactoring, they swap their original and refactored versions. Each student writes two bullet points: one positive comment about their partner's refactored code and one suggestion for further improvement.

Frequently Asked Questions

What are key refactoring techniques for grade 11 computer science?
Core techniques include extracting methods to break long functions, renaming variables for clarity, removing code duplication, and simplifying conditionals. Students apply these to object-oriented code, ensuring tests pass before and after. Practice with incremental steps builds habits for maintainable software in team projects.
Why is code quality important in OOP projects?
Quality code supports collaboration, reduces debugging time, and scales for complex designs. In long-term projects, poor code leads to technical debt. Refactoring teaches students to prioritize readability and modularity, aligning with professional practices and curriculum standards.
How can active learning help teach refactoring?
Active approaches like pair programming and station rotations engage students in critiquing and revising code hands-on. They test changes immediately, discuss trade-offs with peers, and iterate quickly. This makes refactoring tangible, improves retention, and mirrors real development workflows better than lectures.
How to assess student understanding of refactoring?
Use rubrics evaluating before-and-after code for readability, efficiency, and test preservation. Portfolios of refactored projects with reflection logs show growth. Peer reviews add feedback, helping students internalize quality metrics through practical application.