Channels ▼
RSS

JVM Languages

Bug Opportunity Levels


Level 3: If there is a bug, the program will work incorrectly. In this case, there is no error message to warn you of the problem. The program simply behaves in some way that is incorrect.

This is worse for a couple of reasons:

  • First, the behavior might be incorrect in some subtle, non-obvious way. A large amount of careful testing and analysis might be required to detect the bug. In the worst case, you might not catch it at all, and only learn of its existence when a customer reports it to you.
  • Second, even when you learn of the existence of a bug, it may not be obvious what is causing it. This is especially serious when you do not know a reliable way to reproduce it. A long, painful debugging process may be required before you finally track down the cause of the problem and fix it.

Suppose you are writing a method to perform some operation, and realize that in certain cases, it is impossible to do exactly what you intend. For example, suppose it is meant to set the value of some parameter. That value is stored both in memory and on disk, and the method is supposed to update it in both places.

What if an error prevents you from updating the value on disk, perhaps because another thread already has the file open for writing? There are two obvious choices for what to do:

  • Update the value only in memory, then return.
  • Throw an IOException and force whatever code is invoking the method to handle it.

Inexperienced programmers will often choose the first approach. They will claim this is "more robust", since the method partly succeeds instead of completely failing. They will say they are making the code "fault tolerant". They will say it produces an easier to use API, since programmers invoking the method are not forced to worry about IOExceptions. They are, however, completely wrong.

In fact, what they are doing is replacing a level 2 bug opportunity -- an exception that clearly tells you what has happened-with a subtle, hard to identify source of intermittent bugs. Every method should precisely define what its behavior is. If it cannot do exactly what is expected, it should signal this in a way that alerts either the programmer or the user to the problem.

A level 3 bug opportunity is bad, and you should try hard to avoid them, but there is something even worse:


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video