Indicators
A Programming Pattern |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
baseFine = 10.00; repeatOffenderPenalty = 35.00; totalFine = baseFine + (ticketedIndicator * repeatOffenderPenalty);
baseRate = 19.95; ageSurcharge = 10.00; multiSurcharge = 5.00; rate = baseRate + (multiIndicator * multiSurcharge) + (youngIndicator * ageSurcharge);
if
Statements?
w = -2200.0 + (148.2 * g); if (smoker) { w -= 238.6; }
baseRate = 19.95; ageSurcharge = 10.00; multiSurcharge = 5.00; rate = baseRate; if (areMultipleDrivers) { rate += multiSurcharge; } if (areYoung) { rate += ageSurcharge; }
if
statements but they are still more verbose
than indicatorsbaseRate = 19.95; ageSurcharge = 10.00; multiSurcharge = 5.00; rate = baseRate; rate += areMultipleDrivers ? multiSurcharge : 0; rate += areYoung ? ageSurcharge : 0;