Writing and Testing Reusable Functions
The computepay function encapsulates pay calculation logic with two parameters: hours and rate.
One Rule, Many Calculations
Suppose a program needs to calculate pay for several employees, or recalculate pay whenever hours change. Repeating the same calculation each time makes the program harder to maintain and creates more opportunities for mistakes. A reusable computepay function puts the rule in one place: hours up to 40 receive regular pay, while hours beyond 40 receive time-and-a-half pay.
The function accepts two inputs, hours and rate, makes the overtime decision internally, and returns the total pay.
Following the Overtime Decision
The central question is whether hours is 40 or less. If it is, every hour uses the regular rate. If it is greater than 40, the calculation must split the work into two portions: the first 40 hours at the regular rate and the remaining hours at 1.5 times the rate.
Tracing a Regular Case
Consider a call with hours equal to 35 and rate equal to 10. The function receives both arguments, checks whether 35 is greater than 40, and follows the regular-pay branch because it is not. The result is 35 multiplied by 10, or 350.
Thirty-Five Hours at Ten per Hour
Calculate pay when hours is 35 and rate is 10.
Receive inputs: computepay receives hours equal to 35 and rate equal to 10.
Check the branch: Because 35 is not greater than 40, overtime does not apply.
Calculate regular pay: Multiply all 35 hours by the regular rate of 10.
The returned pay is 350.
Building the Function
The function's two parameters represent everything this pay rule needs: hours worked and the hourly rate. The conditional branch keeps the two pay scenarios separate. For hours at or below 40, the calculation is hours multiplied by rate. For hours above 40, the calculation pays the first 40 hours at rate and the excess hours at rate multiplied by 1.5. Returning the result makes the computed value available to the code that called the function.
The regular branch returns hours multiplied by rate. The overtime branch returns the regular pay for 40 hours plus time-and-a-half pay for every hour beyond 40.Tracing an Overtime Case
Now trace hours equal to 45 and rate equal to 10. The overtime check succeeds because 45 is greater than 40. The first 40 hours produce 400 in regular pay. The remaining 5 hours produce 5 multiplied by 10 multiplied by 1.5, which is 75. Adding those portions gives total pay of 475.
Forty-Five Hours at Ten per Hour
Calculate pay when hours is 45 and rate is 10.
Receive inputs: computepay receives hours equal to 45 and rate equal to 10.
Choose the branch: Because 45 exceeds 40, the overtime branch is selected.
Calculate regular pay: The first 40 hours are paid at the regular rate: 40 multiplied by 10 equals 400.
Calculate overtime pay: There are 5 hours beyond 40. Those hours are paid at time and a half: 5 multiplied by 10 multiplied by 1.5 equals 75.
Combine the portions: Add 400 and 75 to obtain the total pay.
The returned pay is 475.
Testing the Branch Boundary
Testing should cover the two branches and the point where the branches meet. Use an input below 40 to test regular pay, exactly 40 to test the boundary, and above 40 to test the overtime calculation. Comparing each returned value with the expected result helps reveal whether the decision or one of the pay portions is wrong.
| Scenario | Hours | Rate | Expected pay | Branch |
|---|---|---|---|---|
| Below threshold | 35 | 10 | 350 | Regular |
| At threshold | 40 | 10 | 400 | Regular |
| Above threshold | 45 | 10 | 475 | Overtime |
Generated test cases for both pay branches and the 40-hour boundary.
Mistakes in Pay Logic
Forgetting the overtime check
Hours above 40 would not receive time-and-a-half pay.
Fix:
Branch on whether hours is 40 or less, and use the overtime calculation when hours exceeds 40.Applying the overtime multiplier to all hours
Only hours beyond 40 receive the overtime multiplier.
Fix:
Pay the first 40 hours at the regular rate and apply 1.5 only to hours minus 40.Miscalculating the overtime portion
The overtime portion should contain only the hours beyond 40.
Fix:
Calculate the excess as hours minus 40, then multiply that excess by rate and 1.5.Forgetting to return the value
The calling code cannot receive the computed pay.
Fix:
Return the result from both the regular and overtime branches.
Practice the Trace
Trace computepay for hours equal to 50 and rate equal to 12. Identify the branch, separate the regular and overtime hours, calculate each pay portion, and determine the returned total.
Hints
- Compare 50 with the 40-hour threshold.
- The regular portion contains 40 hours.
- The overtime portion contains hours minus 40.
- Apply the 1.5 multiplier only to the overtime portion.
What do you think happens?
For hours equal to 50 and rate equal to 12, what total pay should computepay return?
Reveal answer
Answer: 660
The first 40 hours produce 40 multiplied by 12, which is 480. The remaining 10 hours produce 10 multiplied by 12 multiplied by 1.5, which is 180. Adding the two portions gives 660.
Reusable Logic Checklist
- Define computepay with the two parameters hours and rate.
- Use a conditional branch to distinguish hours at or below 40 from hours above 40.
- For regular hours, calculate hours multiplied by rate.
- For overtime hours, pay the first 40 hours regularly and the remaining hours at 1.5 times the rate.
- Test below 40 hours, exactly 40 hours, and above 40 hours, then trace the decision point when results differ from expectations.
computepay demonstrates how a function can encapsulate a decision-making process. Once the logic is written and tested, the same function can be called repeatedly for different employees or changing hour values. Keeping the rule in one function reduces repeated code and gives future corrections one place to make the change.
Key Takeaways
- computepay takes hours and rate as its two parameters and returns total pay.
- Hours at or below 40 use regular pay, while hours above 40 split into regular and overtime portions.
- Only hours beyond 40 receive the 1.5 time-and-a-half multiplier.
- Tracing the overtime branch helps locate incorrect calculations.
- Testing below 40, exactly 40, and above 40 checks both branches and the boundary.