Code Maintenance CS Thinking Example 3

Follow the full solution, then compare it with the other examples linked below.

Example 3

medium
Identify three maintenance problems in this code: x = INPUT(). y = x * 0.175. z = x + y. OUTPUT z. Rewrite it with better practices.

Solution

  1. 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. 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