Posted by: terryhowe | October 1, 2009

Clean Code: Error Handling

I’m reading Robert C. Martin’s Clean Code and I’m not as big an Exception fan as Martin, but I do like the special case pattern. Here are some interesting points from his Error Handling chapter:

  • Use Exceptions rather than returns codes:  Martin prefers exceptions to return codes.  I never use error specific return codes, but I’m a big fan of functions that return true or false.
  • Use unchecked Exceptions:  Declaring exceptions in your method signature forces the caller to handle the exception or declare the exception in their signature.  In most cases this is not useful except perhaps when you are defining a public user interface.
  • Define Exception Classes in Terms of caller’s Needs:  Rather than generating several different exceptions for a problem that is going to be handled the same way, use one exception with different text.
  • Use the special case pattern: Rather than throwing an exception return a class that provides some default behavior.  Use this for exceptions rather than returning or passing null.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: