Channels ▼
RSS

Tools

Programming with Reason: Why is goto Bad?


When is break Broken?

The break keyword in languages such as C/C++ and Java accomplishes the same thing: It abnormally exits from a block of control code (i.e. a loop or switch..case statement). In fact, to avoid mid-method returns, you may need to place a break within a while(true) loop to escape from it. The use of break in these situations makes sense to me.

Another occasion where a break makes sense is when performance is a consideration, such as when checking for a condition within a for loop as this example shows:



    private boolean loginUser(String username) {
        boolean found = false;
        for ( User user: Users )
            if ( user.username.equals(username) ) {
                found = true;
                // login user here…
                break;
            }
        }
        return found;
    }

In my opinion, this break is fine, although it does upset code flow somewhat, and it can be considered as offensive as goto (it essentially jumps to the return statement in the method). However, since it does so only to avoid needless processing of all of the users in the list once the appropriate one is found, it's acceptable. Also, since the user-login processing is still done in place, and it's obvious when reading the code where that is, this use of break is even more acceptable.

Using more than one break in a loop, however, is not acceptable to me. This is just as bad as a mid-method return, as it makes the code more difficult to read and requires more analysis to determine where to make modifications to the code. Experience has shown me that in just about every case where multiple breaks exist within a loop, the code structure can be improved to avoid this and make the code more readable.


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