I have an internal JavaScript helper that averages count properties from objects returned by a server. The server documents counts as nonnegative numbers. Should the helper check every value, or can it trust that contract?
How much should an internal function validate its inputs?
Answers
I'd validate the response when it enters the application, then let internal helpers work with the validated data. That gives you one place to reject a bad response before other code starts using it, instead of scattering checks through every utility.
Check the shape as well as the values: the response should contain the expected array, each item should be an object, and each count should be a finite number greater than or equal to zero. The stated contract allows fractional numbers, so only require integers if that's a separate requirement.
Reply to this answer
For a small private helper, a quick check can be worthwhile if a bad count would otherwise produce a confusing result. Throwing an error with a useful message makes the failure easier to track than a mysterious NaN or a misleading average.
I'd document what the helper accepts and be clear about where errors are handled. Checks aren't free, though. Repeating the same validation everywhere can make the code harder to maintain.
Reply to this answer
Decide what invalid data means before choosing a check. Rejecting the response makes a broken contract visible. Skipping bad items keeps the calculation going, but the result is then an average of only the remaining items, which may not be what the user expects.
Don't silently filter values just because it's convenient. If partial results are acceptable, make that behavior explicit.
Reply to this answer
For tests, start with the behavior you've promised: several valid counts, zero values, a single item, and an empty array. Your current function returns zero for an empty array, so that's worth making explicit.
Test negative values, missing counts, and numeric strings at the validation boundary if those are rejected there. Add tests for them on the helper itself only if you've decided the helper has a defined response, such as throwing. Otherwise you risk freezing accidental JavaScript behavior into the contract.
7h~bytewicket_593:
So if I choose to reject invalid counts in the helper, those rejection cases become part of its tests too?
_f2!canvaslatch+:
Exactly. Test the behavior you intend callers to rely on, including the error behavior you choose.
Reply to this answer
JavaScript can make a bad value look less obviously broken than you'd expect. Adding a string to a number can concatenate instead of summing, while a missing count can lead to NaN. A server contract is useful documentation, but it doesn't itself enforce runtime types.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Vexnaro+4j:
That assumes every path into the helper goes through the validation. If another caller can construct the array directly, it needs to honor the same contract.