octave-maintainers
[Top][All Lists]
Advanced

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

Re: goto vs. return?


From: Rik
Subject: Re: goto vs. return?
Date: Sun, 24 Jan 2016 07:55:49 -0800

On 01/24/2016 02:49 AM, Kozma, Endre wrote:
On Sat, 2016-01-23 at 14:27 -0800, 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.

--Rik
If we accept that none of them is the programming attitude to be followed,

I don't think much of goto, but I do accept intermediate returns as a valid style.  What I dislike is the Arrow coding style (http://blog.codinghorror.com/flattening-arrow-code/) of which there is a lot.  Early returns, a.k.a guard statements, are one way of reducing massive indentation.  One versus multiple returns seems to be something that programmer's like to debate.  There are multiple questions on Stack Overflow about this, but this one gives a flavor of the discussion (http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement).  The consensus seems to be that it is okay in C++ to use multiple returns.

--Rik

then there is only one right answer: refactor the whole function.
Such things otherwise are usually good indicators that refactoring is necessary. So you might have to establish multiple smaller functions/methods instead of having a single big one.
The related term is: "clean code".

Endre


reply via email to

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