💻 Programming Section: Develop Code - Comprehensive Study Notes

Hello Future Coders! Welcome to the "Develop Code" chapter. This is where the planning and algorithms you learned about turn into actual, working software! Learning to code isn't just about typing commands; it’s about solving problems logically and ensuring your solution is robust (strong and reliable).

Don't worry if writing code seems tricky at first. Think of it like learning to cook: you follow the recipe (algorithm), you mix the ingredients (variables and inputs), and eventually, you produce something great (the program)!

1. The Software Development Life Cycle (SDLC)

Developing a program is a structured process. You wouldn't build a skyscraper without blueprints! Following these stages ensures your code is efficient, error-free, and meets the user’s needs.

Step 1: Planning and Analysis
  • Identify the problem and define the requirements (what the program must do).
  • Design the solution (creating the algorithm, structure charts, etc.).
Step 2: Coding and Implementation (Developing the Code)
  • Write the code using a suitable programming language.
  • Translate the algorithm (like a flowchart) into actual instructions.
  • Focus on making the code easy to read and maintain (see Section 2).
Step 3: Testing and Debugging
  • Run the code and check if it works correctly.
  • Identify and fix any errors (bugs) that appear.
  • Test with various types of data to make sure the program is robust.
Step 4: Evaluation and Maintenance
  • Evaluate the final product against the initial requirements. Does it meet the brief?
  • If necessary, update the code to fix new bugs or add new features later on.

Key Takeaway: Developing code is not linear! You often cycle back between Coding, Testing, and Debugging until the program is ready.


2. Characteristics of Good Program Code

Writing code that works is one thing; writing code that is clean, clear, and easy for others (or future you!) to understand is vital. This is known as writing maintainable code.

A. Readability and Structure
  • Meaningful Variable Names: Instead of naming a variable a or x1, use descriptive names like userAge or totalScore. This instantly tells the reader what the variable holds.
  • Indentation and Spacing: Use consistent indentation (tabbing) to show which lines of code belong to which control structure (like an IF statement or a FOR loop). This makes the program structure clear.
  • Modularisation: Breaking large tasks into smaller, manageable sections called modules, procedures, or functions. Analogy: A single chapter in a long book is easier to read than one giant, continuous paragraph.
B. The Importance of Comments

Comments are lines of text added to the code that the computer ignores. They are there purely for humans!

  • Comments explain how a complex section of code works.
  • They explain the purpose of important variables or functions.
  • They are essential for debugging and maintenance. If you come back to your code six months later, comments remind you what you did!

Did You Know? Professional programmers often spend more time reading and maintaining existing code than writing brand new code! Clean code saves time and money.


3. Understanding Errors (Bugs)

A bug is an error in the program that causes it to behave unexpectedly or crash. Finding and fixing these errors (debugging) is a huge part of the development process.

The Three Types of Errors

Don't worry! Everyone makes errors. The key is knowing how to identify them.

  1. Syntax Errors

    These are errors in the grammar or rules of the programming language. The program cannot understand the instruction and therefore cannot run.
    Example: In Python, misspelling print as prnt, or forgetting a colon (:) in an IF statement.

    Detection: Usually caught automatically by the programming environment (IDE) before the code even tries to execute.

    Analogy: A spelling mistake or grammatical error in an essay. Your teacher (the computer) can’t figure out what you meant.

  2. Logic Errors

    The code is grammatically correct and runs successfully, but it does something you didn't intend it to do. The output is incorrect.
    Example: You wanted to calculate the average of two numbers but accidentally subtracted them instead of adding them: average = (num1 - num2) / 2.

    Detection: These are the trickiest! They can only be found through careful testing and checking the actual output against the expected output.

    Analogy: A recipe says "add 2 tablespoons of salt" but you only add 2 teaspoons. The recipe runs, the food is edible, but it tastes wrong (incorrect output).

  3. Run-time Errors

    These errors occur while the program is actually running, often due to an impossible operation the computer only discovers mid-execution. The program usually crashes or freezes.
    Example: Attempting to divide a number by zero (e.g., \(10 / 0\)). This is mathematically impossible, causing a crash.

    Detection: Detected only when the specific line of code is executed.

Quick Review: Errors

Syntax = Spelling/Grammar (won't run)
Logic = Lies/Wrong Answer (runs, but bad output)
Run-time = Random Crash (happens during execution)


4. The Art of Testing

Testing is a systematic way to check if your program is robust (handles bad data gracefully) and delivers the correct results. You must test your program with different types of data before you can say it is complete.

The Three Types of Test Data

To ensure robustness, we use three categories of data:

  1. Normal Data (Valid Data)

    Data that you expect the user to enter and that should definitely be accepted by the program. This data falls within the accepted range.
    Example: If a program accepts ages between 18 and 65, Normal Data would be 30, 45, or 50.

  2. Boundary Data (Extreme Data)

    Data that sits right on the edges or limits of the acceptable range. Programmers often make mistakes when dealing with limits (e.g., using < instead of ≤). Testing boundaries ensures the limits are handled correctly.
    Example: For the 18 to 65 age range, Boundary Data would be 18 and 65. You should also test the numbers just outside the boundary: 17 and 66.

  3. Erroneous Data (Exceptional/Invalid Data)

    Data that should be rejected by the program, often resulting in an error message. This tests how robust your input validation is.
    Example: For the 18 to 65 age range, Erroneous Data would be -5, 100, or entering text like "twenty" instead of a number.

Key Takeaway: A program is only robust if it handles all three types of data correctly, especially Erroneous Data.


5. Debugging Techniques: The Trace Table

When you have a Logic Error, the computer won't tell you where the mistake is. You need to become the computer! A Trace Table (or Dry Run) is a powerful manual technique used to track the values of variables step-by-step as the algorithm executes.

How to Use a Trace Table (The Detective’s Tool)

Imagine the following simple algorithm that adds an input number to a running total, but only if the number is greater than 10.

The Trace Table helps you manually check if the logic is right.

Step 1: Setup
Create columns for every variable used in the program, plus an Output column and a Condition column (for IF statements or loops).

Step 2: Execution
Go through the code line by line. Every time a variable changes value, record the new value in the table. Record the output only when the program displays something.

Example Trace Table Structure:

Line Number Input (N) Total Condition (N > 10) Output
1 - 0 - -
2 (Input 5) 5 0 False -
2 (Input 15) 15 0 True -
3 (If True) 15 15 (0 + 15) True -
4 (Output) 15 15 - Total is 15

By tracing the steps, you can pinpoint exactly where a variable holds an unexpected value, helping you identify the line of code causing the Logic Error. If the final output is 15, and you expected 20, you can trace backwards to see where the number 5 was incorrectly handled.

Trace Table Tip!

Always start with the initial value of every variable (usually zero or empty) and never skip a step. If a variable doesn't change on a line, carry its old value forward to the next step.


6. Common Development Pitfalls to Avoid

As you develop code, keep an eye out for these common errors:

  • Off-by-one Errors: A classic Logic Error where loops run one iteration too many or one too few (e.g., looping 10 times when you needed 9). Always check boundary conditions carefully!
  • Ignoring Input Validation: Assuming the user will always enter the right type of data. If you ask for a number, you must write code to handle it if they enter text (Erroneous Data).
  • Ignoring Data Types: Confusing numerical data (for calculation) with string data (for text). Example: Trying to add the number 5 to the text "10".
  • Not Testing Enough: Only testing with Normal Data. This is dangerous! You must use Boundary and Erroneous Data to find the tricky bugs.

This chapter provides the essential tools to move from designing algorithms to building reliable programs. Keep practicing your tracing and testing skills!


*** End of Chapter Notes ***