Channels ▼

Bil Lewis

Dr. Dobb's Bloggers

Eschew ELSE

February 20, 2010

IF...THEN...ELSE is almost never what I want.

Early in my teaching career, I realized that saying "Do X or else..."
meant that I was giving them a choice. Not much of a choice, but still
a choice. And I usually didn't want to give 3rd graders a choice, I wanted
them to do X. 

So I stopped making the implied threat of the "or else" and simply
said "We're going to do X." And if a student challenged me "What if we
don't?" I would smile and simply reply "But we will. Now please get
out your history books..."

It worked quite well.

By analogy, the same is true in programming.

I rarely want to give my program a choice. I want it to do X.

So while many programs look sort of like this:


public Thing process(List things) {
  Thing result;
  if (condition1()) {
    result = execute1(things);
    } else if (condition2()) {
    result = execute2(things);
    } else if (condition3()) {
    result = execute3(things);
    } else {
    result = executeDefault(things);
    }
  return result;
}


I would write that code like this:


public Thing process(List things) {
  if (condition1()) 
    return execute1(things);

  if (condition2()) 
    return execute2(things);

  if (condition3()) 
    return execute3(things);

  return executeDefault(things);
}

The two examples give identical results and the complier may well
produce identical binary code for them. Which is better?

I claim that the second example is much easier to read and understand
than the first and much easier to modify. The point is that in the
first example, you have to keep the potential value of RESULT in mind
as you read through the code, while in the second example you don't.

This becomes even more pronounced when the code gets more complex.

if (condition1()) {
  result = execute1(things);
  if (isAnomolous(result)) {
    result = dealWithAnomoly(result);
    } else {
    result = finishExecution(result);
    }
  } else if...

verses:

if (condition1()) 
  return execute1WithAnomoly(things);
...


public Thing execute1WithAnomoly(List things) {
  Thing result = execute1(things);

  if (isAnomolous(result))
    return dealWithAnomoly(result);

  return finishExecution(result);
}


Now the first process() for condition1 has grown significantly more complex,
while the second version remains two simple lines. Could the first
have pulled out a version of execute1WithAnomoly()? Of course. But it
rarely happens that way, because the mind-set of the programmer is
different and s/he doesn't see the opportunity that is so obvious in
the second example.

IMHO

What's your HO?

-Bil

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