duarteroso at April 21st, 2012 10:24 — #1
I'm actually working on an engine and came across a question I cannot answer.
Who do you think should check if the values are correct?
Lets take an example. Suppose we have a function like this:
void Rotate(float _fAngle);
which obviously rotates something, doesn't matter what.
- Should it be the engine who should checks if the angle is between [-360,360], assert, compute the correct angle, do nothing?
- Should it be the guy using the engine who should check the angles before calling the Rotate function?
- Or perhaps both should check?
In my opinion the engine shouldn't be responsible for this simply because it would mean giving it too much responsibilities.
On the other hand, I've been a young developer and know that people don't always check theirs values before calling a method.
Let me know what you think.
reedbeta at April 21st, 2012 16:38 — #2
Well, in the case of rotation specifically it doesn't matter since "out-of-range" angles will still work in all the trig functions in the rotation formula.
But to answer your question in general: where a function has constraints on its arguments, IMO it's appropriate for the function itself to check those constraints, as long as it is reasonable for it to do so (i.e. it has all the relevant information available, checking the constraints is not too expensive, etc).
More generally, the appropriate level and strategy of "defensive coding" really depends on what kind of code you're writing, and what the consequences of an undetected error are. To take two extreme examples: for high-performance math code, I'd do very little error checking, and limit it to debug builds, since checking errors can be a drag on performance and math functions generally don't cause anything bad to happen if you call them on bad input. Garbage in, garbage out, but it won't cause a crash or anything terrible. (An exception is if an error leads to creating NaNs, since those can infect all your data quite rapidly.) On the other hand, for a web service that needs to be running 24/7 I'd be meticulous about error checking, and go to great lengths to ensure the system can recover and continue to operate after an error (and that errors don't cause it to corrupt its data, etc.).
duarteroso at April 23rd, 2012 18:02 — #3
I agree with you. Depending on the kind of code, the called function should check for everything including what it receives. I do so on small application only I use and where I have the complete control of the code.
We are talking about game engines here. The engine may return an error code, but no actual crash should happen because of an incorrect parameter.
Of course checking inside the function works and it's a good practice, but that means the code who calls my function is already wrong somewhere and that's not the engines fault nor should it be the engine to correct it. Doing so just hides the real problem.
Anyway, it's always good the have another opinion on the subject (even if it's only one...)
Thanks for taking the time to answer