Stack Overflow Asked by snapo on November 4, 2021
A friend and me had a big discussion about a single function, the function itself does not make any sense, but it is false in my view.
The function is the following:
//get tomorrows date
int getTomorrowsDate(){
sleep(1*60*60*24);
return getCurrentDate();
}
If i execute the function in and we get the result, it is already wrong as the tomorrow day already got today.
After discussing this a long time, my friend is at the position that a function is correct on execution time and i am in the opposite and say a function is correct at the resulting time. May some one please elaborate to me why my view is wrong as i do not understand it.
IMO opinion, this function has the semantics of getCurrentDate
and could keep that name. Because getCurrentDate
is expected to return some date which is guaranteed to occur between the moment of the call and the moment of the return. (Think of what happens if you call the standard function ten nanoseconds before midnight, so that the date changes during the call.)
By the way, I don't even know if the existing implementations enforce the above "betweenness" requirement. (For example, the rule could break if the function attempted to compensate its own latency.)
Answered by Yves Daoust on November 4, 2021
Consider this pseudo code:
fun getTomorrowsDate()
sleep(getRandomValue())
today = getCurrentDate()
sleep(getRandomValue())
ret = today + 1
sleep(getRandomValue())
return ret
This is not very far from what is actually happening during EVERY function call. The operating system might interrupt at any time, so in a sense, those sleep calls actually do exist.
So unless you have taken very cautious steps of making your function atomic, the only difference between the above pseudo and your code example is that you have ensured an event that always have a non-zero probability to happen to have 100% probability.
David and John gave good answers, so I will not elaborate more. I just wanted to add this example.
Answered by klutt on November 4, 2021
As David Schwartz says, status reporting operations such as getting disk free space, getting file size, checking if a file exists, etc., are fundamentally unreliable. A good way to think about them is that they return good faith estimates of their measurements, but there's the caveat that callers must not rely upon the values being correct as they could change at any time before, after, or during the measurement.
There's also the matter of requirements, both stated and unstated. Pretty much every function you write will have an unstated performance requirement that it not take 24 hours to execute. Certainly that's true for getting a date. What does it matter what you call the result of this function when the function is completely unusable? It's a purely academic distinction. In practical terms, the function is broken, and no result is ever correct.
To go further, I'm not even sure it's an academic distinction. You're asking if the "execution time" or the "result time" is correct. To be "correct" implies that the distinction will lead to either a right or wrong action and you need to know which it will be. What would those actions be?
If I'm right then I would do X, but if my friend is right I'd do Y.
What are actions X and Y? What would you do differently based on how this argument is resolved? For example, if this were a real issue in a ticketing system, would you end up closing the issue based on the outcome of your argument—neither of you fixing the 24-hour sleep? I hope not!
Answered by John Kugelman on November 4, 2021
A status-reporting function's result is correct if its result was valid at at least one time in-between when it was called and when it returns. This is the case for all status-reporting functions such as getting disk free space, getting file size, and so on.
At some point it has to get the status, and it can't do anything about the status changing before it gets it or after it gets it. So this has to be the rule or it would be impossible to write correct functions.
People often get this wrong. For example, they check for free space on a disk and then assume a subsequent write won't fail due to insufficient space. Or they call select
and then assume a subsequent operation won't block. These are all mistakes.
Answered by David Schwartz on November 4, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP