Computer Science
Grade 9
20 min
Code Quality and Style
Code Quality and Style
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Explain the importance of code readability and maintainability.
Apply consistent naming conventions for variables and functions.
Use comments effectively to explain the purpose of their code.
Correctly use indentation to structure their code logically.
Identify and correct poorly styled code in a given program.
Write a simple program that adheres to basic code quality and style guidelines.
Have you ever tried to read someone else's messy handwriting? 📝 Writing code can be the same way if you don't keep it neat and organized!
In this lesson, you'll learn how to write code that is not only correct but also clean, readable, and easy for you and others to understand. This is a crucial skill for any programmer, as it makes debugging, updating,...
2
Key Concepts & Vocabulary
TermDefinitionExample
ReadabilityHow easily a human can read and understand the purpose of a piece of code. High readability means the code is clear, well-organized, and self-explanatory.A function named `calculate_user_age` is much more readable than one named `proc1`.
Naming ConventionA set of rules for choosing the character sequence to be used for identifiers (names of variables, functions, etc.) in source code.Using `snake_case` (e.g., `first_name`) or `camelCase` (e.g., `firstName`) consistently throughout a project.
IndentationThe use of spaces or tabs at the beginning of a line of code to show its relationship to the code above and below it, especially for loops and conditional statements.In Python:
if score > 90:
print('Excellent!')
The `print` statement is inde...
3
Core Syntax & Patterns
Descriptive Naming
Variable and function names should clearly describe their purpose or the data they hold.
Use this rule every time you create a new variable or function. Instead of short, generic names like `x` or `temp`, use descriptive names like `user_age` or `calculate_tax()`.
Consistent Indentation
Use a consistent number of spaces (usually 4) or a single tab to indent code blocks inside loops, functions, and conditional statements.
Apply this to structure your code visually. Every time you open a new block (like with `if`, `for`, or a function definition), indent the code inside it. Un-indent when the block is finished.
Commenting the 'Why', Not the 'What'
Write comments to explain *why* a piece of code is doing something complex or non-obvi...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A large, critical program for a business was written with poor code style (cryptic names, no comments, inconsistent formatting). Although it currently works, what is the MOST significant long-term risk for the business?
A.The program will use slightly more electricity.
B.The program will be very slow and expensive to update or fix when business needs change.
C.New programmers will not be able to run the program.
D.The program will stop working after one year.
Challenging
You are tasked with writing a function that takes a list of numbers and returns the highest number. Which function definition BEST demonstrates the principles of good code quality and style from the tutorial?
A.def func(l):
# code here
B.def find_maximum_value(list_of_numbers):
# code here
C.def findMax(numberList):
# code here
D.def get_the_biggest_number_from_the_list_of_numbers_you_give_it(a_list):
# code here
Challenging
You are given this poorly styled function:
`def h(data, threshold):
res = []
for item in data:
if item > threshold: res.append(item)
return res`
What is the single MOST impactful first step to improve its quality?
A.Add a comment explaining what `res` is.
B.Change the indentation of `res.append(item)` to its own line.
C.Rename the function and its parameters to be descriptive.
D.Rewrite the `for` loop to be more efficient.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free