I have to create a method
CalculateMean(int[] x)
If the input is an empty array, should I return 0 (or some number), or throw exception?
I have to create a method
CalculateMean(int[] x)
If the input is an empty array, should I return 0 (or some number), or throw exception?
In my opinion, you should definitely throw an exception (unless the return type is nullable).
Since the empty array has no mean, there is no correct number that a CalculateMean
method could return when called on the empty array. Returning any number would be incorrect.
Since returning any number would be incorrect, there are only two options that would be correct:
CalculateMean
nullable, and return null
.Whatever you do, make sure that the documentation for CalculateMean
states exactly what it does when you pass it an empty array.