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

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s