Best practice for handle error checking

To add comments, please log in or register
Ian Flanagan
549
Ian Flanagan  

Hi! I normally search for answers instead of asking (I've learned so much from this forum! Thanks to everyone!!!) but... I can't figure this one out.

Calling FileOpen returns the handle or "-1" if there was an error.

So why do I always see error checking done with:

 if(handle<1);

or

 if(handle<0);

Why do people avoid using?

 if(handle==-1);

Thanks in advance for any tips 

Simon Gniadkowski
17602
Simon Gniadkowski  
alladir:

Hi! I normally search for answers instead of asking (I've learned so much from this forum! Thanks to everyone!!!) but... I can't figure this one out.

Calling FileOpen returns the handle or "-1" if there was an error.

So why do I always see error checking done with:

Mostly it will be personal preference or just what has become habit for people,  but there is also a small consideration to covering undocumented possibilities or future changes . . .  if MetaQuotes change the return value in the case of an error to -999 the first 2 options would work,  the third would not.
Ian Flanagan
549
Ian Flanagan  

Ok, great, thanks.

Also well done on all your hard work on these forums.

I don't reply to every thread that helps but you've helped me countless times already! 

Simon Gniadkowski
17602
Simon Gniadkowski  
alladir:

Ok, great, thanks.

Also well done on all your hard work on these forums.

I don't reply to every thread that helps but you've helped me countless times already! 

You are most welcome  
whroeder1
17940
whroeder1  
alladir: So why do I always see error checking done with:
 if(handle<1);

or

 if(handle<0);

Why do people avoid using?

 if(handle==-1);
  1. The first is wrong, a handle of zero COULD be a good value, just as a ticket number zero COULD be  a good one.
  2. Currently -1 on an error. If it was ever modified it could return -1 for one type and -2 for another type, etc. The test would still work.
  3. Personal preferences and/or less typing (< vs ==)
  4. Doubles rarely compare equal so testing for equality is usually wrong. So the habit transfers to this case.
To add comments, please log in or register