Who should check values?

47290574945c1b91e33fe252a4125ccc
0
duarteroso 101 Apr 21, 2012 at 14:24

Hi everyone,

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.

2 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Apr 21, 2012 at 20:38

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.).

47290574945c1b91e33fe252a4125ccc
0
duarteroso 101 Apr 23, 2012 at 22:02

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 ;)