Form 4 · Chapter 1

Programming Best Practices

Best practices such as comments, meaningful names, indentation and debugging make code easier to understand, maintain and keep free of errors.

Why Do Best Practices Matter?

Best practices in programming are habits of writing code that is clear, tidy and easy to understand. Good programs not only work correctly but are also easy for other people, or for the programmer in future, to read and maintain.

Comments, Meaningful Names and Indentation

Comments are notes written in the code to explain the meaning or purpose of a part. The computer ignores comments when the program runs; they are only for humans to read.

Meaningful names for variables and functions make code easier to understand. For example, totalMark is better than x. Indentation is the spacing at the start of a line that shows the structure of code blocks, such as the body of a loop or a selection.

Example

# Calculate the average of two marks
total = mark1 + mark2
average = total / 2      # indentation shows structure

The first line is a comment; total and average are meaningful names.

Syntax Errors and Logic Errors

Debugging is the process of finding and fixing errors in a program. There are two main kinds of error:

  • Syntax error — breaks the grammar rules of the programming language, for example a missing colon or bracket. The program will not run.
  • Logic error — the program runs but gives a wrong result because its logic is incorrect, for example using addition where multiplication was intended.

Key idea

A syntax error stops the program from running; a logic error lets the program run but produces the wrong output.

Remember

Syntax errors are usually easier to spot because the system reports them; logic errors are harder and require tracing step by step.

Stuck on this topic? A verified JomKelas tutor can walk you through it.

Find a verified tutor