Skip to content

Information Technology · Ch 2 — Introduction to Programming

Programming Guidelines — Modular Approach, Debugging and Problem Solving

6

Programming Guidelines — Modular Approach, Debugging and Problem Solving

Writing a working program is only half the job; writing one that is easy to read, test and correct is the other half. This is what programming guidelines are about.

The problem-solving procedure

Good programmers do not start by typing code. They follow a sequence:

  1. Understand the problem — what are the inputs, what output is wanted, and what are the rules connecting them?
  2. Design a solution — write the logic as an algorithm (numbered steps in plain language) or draw a flowchart (a diagram of the logic).
  3. Code — translate the algorithm into Java.
  4. Test — run the program with sample data, including tricky "edge" cases, and compare the output with the expected answer.
  5. Debug and refine — find and fix any errors, then test again.

Algorithm (find the larger of two numbers):

Step 1: Start
Step 2: Read A and B
Step 3: If A > B, then print A
Step 4: Otherwise print B
Step 5: Stop

Drawn as a flowchart, the logic runs: Start -> input A and B -> a decision box Is A > B? -> on Yes, print A; on No, print B -> both branches join at Stop.

The modular approach

Instead of one long main, split a program into small, self-contained modules — in Java these are methods. Each method does one well-defined job, can take parameters (inputs) and can return a result. Benefits: the code is easier to read, a method can be reused many times, and each piece can be tested on its own.

public class AreaDemo {

    // a reusable module: it takes two inputs and returns one result
    static int rectangleArea(int length, int breadth) {
        return length * breadth;
    }

    public static void main(String[] args) {
        System.out.println(rectangleArea(5, 3));   // 15
        System.out.println(rectangleArea(10, 2));  // 20 — reused, not rewritten
    }
}

Types of errors

  • Syntax errors break the grammar of the language (a missing ; or brace). The compiler catches these and refuses to build the program.
  • Runtime errors appear while the program runs, e.g. dividing by zero. The program stops with an error message (an exception).
  • Logical errors are the most dangerous: the program runs and produces output, but the output is wrong because the logic is faulty (e.g. using + where * was meant). No error message appears — only careful testing reveals them.

Debugging techniques

Debugging is the process of finding and removing errors. Common techniques:

  • read the compiler's error message carefully and go to the line it names;
  • add temporary System.out.println(...) statements to see the values of variables at each stage; …
Definition 1Algorithm

A finite, ordered set of clear steps, written in plain language, that solves a problem — design …

Definition 2Flowchart

A diagram that shows the logic of a solution using standard symbols (terminator, input/output, process, decisio …

Definition 3Module / method

A small, named, self-contained block of code that does one job, may take parameters and may return a result; the basis of …

Definition 4Syntax / runtime / logical error

Syntax errors break the language's grammar (caught by the compiler); runtime errors stop the program while it runs; logical errors let it run …

Definition 5Debugging

The process of locating and correcting errors in a program, using error messages, print statements and …