Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Good Programming Practice 📝

Learning Goals

By the end of this section you will:

  • understand why good programming practice improves reliability, efficiency, and teamwork

  • know how to apply style guides like PEP 8 to format and organise code

  • know how to write maintainable code using naming conventions, comments, and small functions

  • understand the importance of reducing cognitive load in code structure

  • know how to write dependable code that handles errors and unexpected inputs

  • understand and apply strategies to improve algorithmic and code efficiency

  • know how to test programs using exploratory, unit, and integrated testing

  • know how to identify and fix syntax, runtime, and logic errors

  • know how to write docstrings and block comments to explain code clearly

  • understand what code portability is and how to write cross-platform Python code

xkcd goto

Good programming practice is important because it makes code easier to read and understand, which helps developers quickly identify and fix errors. It improves the reliability and performance of digital solutions, making them more effective and efficient. Following consistent practices also supports teamwork, as well-structured code is easier for others to read, maintain, and build upon. Additionally, it ensures that solutions are scalable and adaptable, allowing for future updates or changes without introducing new problems.

There are many good programming practices that should be followed. These need to be applied from the very beginning, starting with algorithm development.

Dependability 📝

Code dependability refers to how reliable and consistent a program is when it runs. Dependable code performs as expected under different conditions, handles errors gracefully, and does not crash or behave unpredictably. It includes writing code that is well-tested, handles edge cases, and can recover from unexpected inputs or failures. Dependable code is essential for building trust in the digital solution and ensuring a good user experience.

error messages

In systems engineering, dependability is a measure of a system’s maintainability and reliability.

Maintainability

code quality XKCD

Developers often say that code is read more than it is written. This is especially true when multiple people are working on the same project. Even in a classroom setting, maintainability matters — your code might be reviewed by a classmate, a teacher, or even your future self trying to make sense of what you once wrote.

Maintainability is about writing code that is easy to read, understand, and update. It reduces cognitive load by keeping code clear and predictable, making it easier to identify and safely change the specific parts that need updating, without accidentally breaking other parts of the program.

dependency XKCD

Here are some simple ways to improve your code’s maintainability:

Reliability

Reliability is how likely a program is to run without errors or failures when performing a task.


Efficiency 📝

Efficiency is when a system completes its tasks using the least possible time and processing power. There are two main types:

Algorithmic Efficiency

This refers to how reliably and quickly an algorithm solves a problem, using clear and structured programming techniques to keep solutions concise and effective.

To achieve algorithmic efficiency in your programs, you can follow these simple practices:

Code Efficiency

Code efficiency is closely related to algorithmic efficiency. It focuses on reducing resource usage and execution time while maintaining stability. For example, using a for loop instead of repeating multiple if, then, else statements improves both performance and readability.

efficiency&nbsp

To achieve code efficiency in your programs, you can follow these tips:


Effectiveness

Effectiveness measures how well an algorithm solves the intended problem. While simple problems can be tested with a desk check, most algorithms must be written, run, and tested in the correct context to determine if they work as expected.

To ensure your algorithm is effective, follow these steps:


Testing

testing

Testing is the process of systematically checking that your code performs as expected. It plays a key role in ensuring a solution is effective (solves the intended problem), efficient (runs smoothly and quickly), and dependable (works reliably under different conditions). Testing should happen throughout development and involves three main types:

By combining all three types of testing, you can refine your solution to be more reliable, solve the problem accurately, and perform well.


Debugging

debugging

Debugging is the process of identifying and fixing errors (called bugs) in a program that stop it from working correctly. These bugs might cause the program to crash, behave unexpectedly, or give incorrect output.

In Python, bugs can happen for many reasons — such as syntax mistakes, incorrect variable names, bad logic, or unexpected data types. Because Python runs line-by-line, a single error can stop the whole program. That’s why debugging is an important part of development.

If you’re using Visual Studio Code (VSCode) to write Python, it has built-in tools that make debugging easier.

VSCode also supports print-based debugging, where you add print() statements to show what your code is doing at certain points. This is useful for quick checks or when you’re not sure where the bug is hiding.

By using these tools and strategies, you can quickly find and fix bugs to make your Python programs more reliable.


Error Correction

Fixing errors is a normal and essential part of programming. Over time, experienced developers become skilled at identifying and correcting the mistakes they make.

xkcd error fixing

During program development, three main types of errors can occur:

Syntax Errors

Syntax errors happen when the code breaks the rules of the programming language. These errors prevent the program from running at all and must be fixed before execution. Most integrated development environments (IDEs) will highlight syntax errors automatically.

Examples include:

Runtime Errors

Runtime errors happen while the program is running. These usually cause the program to crash. They often occur when the program tries to perform an action that isn’t allowed, like accessing a list index that doesn’t exist.

Examples include:

Logic Errors

Logic errors occur when the program runs but doesn’t behave as expected. These mistakes don’t crash the program, but they cause it to produce incorrect results or actions.

Examples include:


Coding Conventions

code conventions

Code Simplicity

Code simplicity means writing programs in a way that is easy to read and understand. It focuses on using clear logic, meaningful variable names, and avoiding unnecessary complexity.

Simple code is easier to test, fix, and update, making it more efficient and less likely to contain errors.

Naming Conventions in Python

naming conventions

Python has naming rules (which must be followed or you’ll get an error) and naming conventions (which help others understand your code better).

Naming rules:

Naming conventions:

Commenting

If you use meaningful names following naming conventions, most of your code will be easy to understand without extra explanation.

commenting comic

However, comments are still important in two key places:

def calculate_area(width, height):
    """
    Calculates the area of a rectangle.

    Parameters:
    width (float): The width of the rectangle.
    height (float): The height of the rectangle.

    Returns:
    float: The area of the rectangle.
    """
    return width * height
# Find the index of the first prime number in the list
# This uses a basic method and stops once it finds one
for i in range(len(numbers)):
    if is_prime(numbers[i]):
        prime_index = i
        break

Code Portability

Code portability means writing code that works across different platforms and systems without needing changes. This makes the code more flexible and useful. We’ll be using Python, which runs on Windows, macOS, Linux, as well as embedded systems, iOS, Android, and the web.