Code Maintenance CS Thinking Example 1
Follow the full solution, then compare it with the other examples linked below.
Example 1
easyA program uses the number 0.2 (tax rate) in 15 different places. Why is this a maintenance problem, and how should it be fixed?
Solution
- 1 Step 1: If the tax rate changes, you must find and update all 15 occurrences. Missing even one creates a bug.
- 2 Step 2: Fix: define a named constant: CONST TAX_RATE = 0.2. Use TAX_RATE everywhere instead of 0.2.
- 3 Step 3: Now a rate change requires editing only one line. This is the 'magic number' anti-pattern โ unexplained numbers scattered through code.
Answer
Replace the magic number with a named constant (TAX_RATE = 0.2). Changes then require editing only one line.
Magic numbers make code hard to maintain and understand. Named constants make the meaning clear and changes easy. This is one of the simplest and most impactful code quality improvements.
About Code Maintenance
The ongoing process of updating, fixing, and improving software after its initial release to correct bugs, adapt to new requirements, improve performance, and keep dependencies current. Maintenance includes four types: corrective (fixing bugs), adaptive (adapting to new environments), perfective (improving functionality), and preventive (reducing future problems).
Learn more about Code Maintenance โMore Code Maintenance Examples
Example 2 medium
List four practices that make code easier to maintain, and explain why each helps.
Example 3 mediumIdentify three maintenance problems in this code: x = INPUT(). y = x * 0.175. z = x + y. OUTPUT z. R
Example 4 hardExplain the term 'technical debt' and give two examples of how it accumulates. How does it affect lo