Coding is the step in which the design is turned into a program. A program is read many more times than it is written, so clarity matters as much as correctness.
Goals of the Coding Phase
The goal of coding is to produce code that is correct, readable, easy to test and easy to maintain. The code should follow the design, since the design has already been checked. Efficiency is a goal, but it must not make the code hard to understand. Time spent on clear code is repaid in testing and maintenance.
Programming Style
Programming style is a set of guidelines that make the code readable. The guidelines include the use of meaningful names for variables and functions, a consistent layout with indentation, comments that explain why the code does something and not what is obvious, short functions that do one thing, a limit on the use of global variables, avoiding complex expressions, avoiding deep nesting, and handling errors properly. A coding standard of the organisation makes all programs look alike. Code review and walkthroughs find defects and spread good practice.
Definition: Structured programming. A programming approach in which programs are built from the three control structures of sequence, selection and iteration, without the unrestricted use of goto.
Structured Programming
Structured programming builds a program using only three control structures. Sequence runs statements one after another. Selection chooses between paths with if-else or switch. Iteration repeats statements with a loop. Each structure has a single entry and a single exit. The objectives are to make the program easy to write, understand, test and maintain. The principles are top-down design, modular programming, the avoidance of goto statements, and the use of meaningful names and comments. The advantages are clarity, easier testing and debugging, lower maintenance cost and better productivity. The disadvantages are that data are not protected, as global data can be changed from anywhere, that it is hard to model real-world objects, and that large systems become hard to manage compared with object-oriented methods.
Table 3.5: Control structures
| Structure | Purpose | Example |
|---|---|---|
| Sequence | Statements in order | a = 1; b = 2 |
| Selection | Choose a path | if marks >= 40 |
| Iteration | Repeat | for, while |
Example 3.5: Structured version of a program
Problem: Write a structured program that finds the sum of the first n natural numbers and tells whether it is even or odd, using only sequence, selection and iteration.
Solution:
- Use a loop to add the numbers.
- Use if-else to test the sum.
Result: For n = 10 the sum is 55, which is odd.
n = 10
total = 0
for i in range(1, n + 1):
total += i
if total % 2 == 0:
print(total, "even")
else:
print(total, "odd")
Output
55 odd
Common mistakes
- Using goto to jump around, which makes the flow hard to follow.
- Writing comments that repeat the code instead of explaining the purpose.
- Writing one long function of several hundred lines.
Key takeaways
- The goals of coding are correctness, readability, testability and maintainability.
- Good style uses meaningful names, indentation, comments and short functions.
- Structured programming uses sequence, selection and iteration.
- Structured programs have single-entry, single-exit structures.
Practice questions
- Name the three control structures of structured programming.
- Give two rules of good programming style.
- Give one disadvantage of structured programming.
Answers to the practice questions
- Sequence, selection and iteration.
- Use meaningful names, and write short functions (also indent consistently and comment well).
- Data are not protected, because global data can be changed from anywhere.
Sources
- Official BCA syllabus Advanced Software Engineering, course code 25BCA405DS01, Unit III
