octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: goto vs. return?


From: John W. Eaton
Subject: Re: goto vs. return?
Date: Mon, 25 Jan 2016 14:46:10 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.7.0

On 01/23/2016 05:27 PM, Rik wrote:
According to the Appendix in the manual on Contributing to Octave,

"Avoid comma expressions, labels and gotos, and explicit typecasts. If you need
to typecast, use the modern C++ casting operators. In functions, minimize the
number of return statements—use nested if statements if possible."

Which do we dislike more: goto statements or return from the middle of a
function?

I ask because I count 134 uses of goto in liboctave.  I think most of these
can be avoided now that the error_handler routine no longer returns.  The
others could probably be removed if we used return rather than goto in the
middle of a function.

I personally dislike goto more than return and would remove it, but maybe
there is a consensus in the other direction.

I'm not sure, but I think I typically chose goto when there might be some extra common processing to do before returning.

Also, I used to prefer the style

  if (condition_that_must_be_met_to_proceed_normally)
    {
       if (other_condition_that_must_be_obeyed)
         {
           // Do the thing we want to do.
         }
       else
         error ("nope, still not happening");
    }
  else
    error ("not happening");

because error did not throw an exception and because I like to read the conditions that must be met to succeed rather than those that can cause errors. But this style is not perfect because it leads to lots of indentation for the part of the code that does the real work and, more importantly, it separates the call to error from the condition. Now that error throws an exception, it seems fine to me to write

  if (! condition_that_must_be_met_to_proceed_normally)
    error ("not happening");

  if (! other_condition_that_must_be_obeyed)
    error ("nope, still not happening");

  // Do the thing we want to do.

Unfortunately, I don't think any particular style can guarantee clarity.

jwe







reply via email to

[Prev in Thread] Current Thread [Next in Thread]