On 16/02/2011 11:43 a.m., ken.willi...@thomsonreuters.com wrote:

On 2/15/11 4:35 PM, "Gabor Grothendieck"<ggrothendi...@gmail.com>  wrote:

I think the real good programming practice is to have a single point
of exit at the bottom.

I disagree, it can be extremely useful to exit early from a function.  It
can also make the code much more clear by not having 95% of the body in a
huge else{} block.


If that is how you program all your functions
then you don't need to explicitly put a return in since it always
returns from the bottom anyways and the return would just clutter your
code.

For someone else reading your code, they wouldn't know that you always do
this unless they're very familiar with your coding style.  Even then, it
needs to be manually checked by inspection because nobody sticks with the
"rule" 100% of the time, so it renders the benefit moot.

--
Ken Williams
Senior Research Scientist
Thomson Reuters
Phone: 651-848-7712
ken.willi...@thomsonreuters.com
http://labs.thomsonreuters.com


Some interesting discussion on this point. Enlightening for me at least.

A quick test showed me that an explicit return does produce about a 20% time hit in a one-line function (obviously a lesser % in a non-trivial function) but enough to convince me not to use an explicit return in functions where what is being returned is obvious.

Gabor's point is a good one, there *should* be a single exit point at the bottom, but I have certainly had situations where an early exit seems preferable as Ken suggests. Then an explicit return may make the code sufficiently clear for a violation of Gabor's principle to be acceptable.

David Scott






--
_________________________________________________________________
David Scott     Department of Statistics
                The University of Auckland, PB 92019
                Auckland 1142,    NEW ZEALAND
Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055
Email:  d.sc...@auckland.ac.nz,  Fax: +64 9 373 7018

Director of Consulting, Department of Statistics

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to