
Wikis & learning
Write a typed helper function
A capacity calculation is easier to reuse when you give it a name, parameter types and a clearly defined return type.
A capacity calculation is easier to reuse when you give it a name, parameter types and a clearly defined return type.
How it works
The helper below limits a remaining quantity to zero. Returning a value keeps the calculation independent of how it is displayed.
PHP example
<?php
function remaining(int $total, int $used): int {
return max(0, $total - $used);
}
echo remaining(9, 4) . ', ' . remaining(4, 9);Expected result
5, 0
Try a variation
Try zero for both arguments. Think through the result before running it.
Discussion
Sign in to participate.



