Error codes from functions

Many functions in third party libraries, APIs or in your own code often return some kind of error indication as to the success or otherwise of the function call.

Always check errors when a library (or other) function returns an error (even if you are going to ignore it !).

Why do we make this statement? For all the following reasons.

a) It documents in your source code the fact that the function returns an error (you may decide to check this later).

b) It makes it easier to check the error status when you are debugging your code as/when it doesn’t work.

c) It makes it easier to add error checking at a later date.

d) It encourages you to think “should I be really ignoring the error code returned by a function” at the time that you write your code. In most cases the answer to this question is probably “no”. Checking errors almost always adds little to code complexity and often saves time in the long run.

 

Advertisements