Code Maintenance CS Thinking Example 3
Follow the full solution, then compare it with the other examples linked below.
Example 3
mediumIdentify three maintenance problems in this code: x = INPUT(). y = x * 0.175. z = x + y. OUTPUT z. Rewrite it with better practices.
Solution
- 1 Step 1: Problems: (1) Variable names x, y, z are meaningless. (2) 0.175 is a magic number (what does it represent?). (3) No comments or validation.
- 2 Step 2: Rewrite: CONST VAT_RATE = 0.175. price = INPUT('Enter price'). vat = price * VAT_RATE. totalPrice = price + vat. OUTPUT 'Total: ' + STRING(totalPrice).
Answer
Problems: unclear names, magic number, no comments. Fix with descriptive names (price, vat, totalPrice), named constant (VAT_RATE), and clear output.
Simple refactoring โ renaming variables and extracting constants โ dramatically improves code readability. The rewritten version is self-documenting: anyone can understand its purpose.
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 1 easy
A program uses the number 0.2 (tax rate) in 15 different places. Why is this a maintenance problem,
Example 2 mediumList four practices that make code easier to maintain, and explain why each helps.
Example 4 hardExplain the term 'technical debt' and give two examples of how it accumulates. How does it affect lo