On 13/01/17 12:36, JiiPee wrote:
> On 13/01/2017 11:06, David Brown wrote:
>> In some situations, that might make it easier to spot the problem. In
>> other situations, it is far worse. Suppose this is code in your
>> internet-enabled fridge, which is measuring the average amount of coke
>> you drink so that it can pre-order enough for your expected needs next
>> month. If you don't drink coke, and it is averaging an empty set, would
>> you rather your fridge ordered 0 cokes for next month, of 99999999999999
>> bottles?
>
> But I would not code it like that. I would do like this:
> avCoke = average(..);
> if(avCoke > 9999)
> something_is_wrong_so_report_about_this();
> else
> orderCokes();
>
> so i would check that value *before action*. The problem with 0 is that
> even if we are checking it it does not raise any concerns as it is a
> valid value (zero cokes looks just fine ... but would be wrong here).
>
> This was my whole idea, that we check the value *before doing action*.
> So odd values can be identified.
If you check the value /before/ doing action, then you would check the
size of the set before finding the average, and thus write /correct/ code.
What you are asking is for a way to call a function with "garbage in,
garbage out", and somehow distinguish the resulting "garbage out".
And I would far rather that my fridge ordered 99999999999999 bottles of
coke - because the shop would refuse the order and tell me my fridge is
broken. If it ordered 9999 bottles, the shop might just think I was
planning a huge party.
Your example here simply demonstrates that there is /no/ good choice of
a value that you can return that indicates a failure. If you want to
indicate a failure, you need to do it with a /specific/ and /explicit/
mechanism - such as an exception, or returning a <success, value> pair.
If you are willing to accept that the function user's mistake is the
function user's problem, then returning 0 is just as good as any other
value, because /all/ other values could cause problems for badly written
code, just as 0 could.
>
>> It is/always/ the function user's responsibility to pass
>> correct data to a function.
>
> We are talking here if that fails... so security after that.
>
You /cannot/ fix the broken function user's code from within /your/
function. You can somewhat reduce the risk of further damage, and you
can sometimes make it easier to debug the problem, but that is all.
There are times when the /best/ thing to do when detecting bad input is
to crash the program with an error message - and times when that is the
/worst/ think to do.
Please understand this, and stop trying to come up with different return
values and different circumstances. There is /no/ correct answer here.
If people pass invalid input to a function, you cannot give a sensible
result. You may /specify/ a given result for a given input, in which
case that input is no longer valid - but it is again up to the
function's user to understand what the function does.
This has been understood since the beginning of programmable computers
some 200 years ago, and is just as true today:
<
https://www.brainyquote.com/quotes/authors/c/charles_babbage.html>
> On two occasions I have been asked, 'Pray, Mr. Babbage, if you put
> into the machine wrong figures, will the right answers come out?' I
> am not able rightly to apprehend the kind of confusion of ideas that
> could provoke such a question.