Computer Science Grade 9 20 min

Improving Your Work

Improving Your Work

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Identify and fix bugs in a program using systematic print statement debugging. Refactor repetitive code into a reusable function to follow the DRY principle. Improve code readability by using meaningful variable names and adding explanatory comments. Explain the concept of code efficiency and identify simple opportunities for improvement. Replace 'magic numbers' with named constants to make code more maintainable. Articulate why writing clean, readable code is as important as writing functional code. Ever written code that worked, but when you looked at it a week later, you had no idea what it did? 🤯 Let's learn how to write code that's not just correct, but also clear and smart! This lesson moves beyond just making your code run. We...
2

Key Concepts & Vocabulary

TermDefinitionExample DebuggingThe process of finding and fixing errors, or 'bugs', in your source code. It's like being a detective for your program.If a program is supposed to add 5 + 3 and gives 7, debugging is the process of finding the line of code with the mistake (e.g., `5 + 2`) and correcting it. RefactoringThe process of restructuring existing computer code—changing the internal structure—without changing its external behavior. The goal is to improve the code's readability and reduce its complexity.You have three separate `for` loops that each print numbers 1 to 5. You could refactor this by creating one function `print_numbers(start, end)` and calling it three times. Code ReadabilityA measure of how easily a human can read, understand, and follow the logic of...
3

Core Syntax & Patterns

The Print Statement Debugging Pattern print(f"At this point, variable X is: {X}") When your program isn't working as expected, strategically place print statements to display the values of key variables at different stages of execution. This helps you trace the program's flow and pinpoint where things go wrong. The Function Refactoring Pattern def function_name(parameters): # Repeated code block goes here return result When you see the same or very similar blocks of code appearing in multiple places, extract that code into a function. You can then 'call' the function wherever you need it, making your code shorter, easier to read, and simpler to update. The Constant Variable Rule CONSTANT_NAME_IN_CAPS = value For any 'magic n...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
You are given a 50-line script with vague variable names (e.g., `x`, `lst`), several magic numbers (e.g., `price * 1.13`), and a 10-line block of code that is copied and pasted three times. What is the MOST critical first step to improve this code for long-term maintenance?
A.Add comments to every single line to explain what it does.
B.Refactor the repeated 10-line block into a function.
C.Change all the magic numbers to named constants.
D.Make the code more efficient by finding a faster algorithm.
Challenging
A function works correctly for positive numbers but fails for negative numbers. What is the most systematic way to debug this using print statements?
A.Add one print statement at the very end of the function to see the final result.
B.Add print statements randomly throughout the code until the bug is found.
C.Add print statements at the start, end, and before and after key logical branches (like if-statements) to trace the execution path and variable values for both a positive and a negative input.
D.Delete the function and rewrite it from scratch, as it is clearly broken.
Easy
According to the tutorial, what is the primary purpose of 'debugging' in programming?
A.To add new features to the code
B.To find and fix errors or 'bugs' in the source code
C.To make the code run faster
D.To rewrite the code to be more readable

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.