Re: Only one point of return
On Aug 1, 11:09 am, Ian Collins <ian-n...@hotmail.com> wrote:
cppques...@googlemail.com wrote:
A colleague told me that there is a rule about good stype that a
function in C++ should have only one point of return (ie. return
statement). Otherwise there might be trouble.
I never heard about it and doubt it.
Anybody heard of it? What would be the advantage?
It is a fairly common coding standard.
In my opinion it makes more sense for C than C++, provided your code is
exception safe an early return shouldn't do any harm. Some may argue
that a single point of return makes debugging easier.
The real argument is that it makes understanding the code and
reasoning about the correction of the code a lot easier, so you
don't have to debug. It's more than just common; I'd say that
it's present almost always where code review is taken seriously
(which in turn means everywhere where the quality of code is
taken seriously).
About the only exceptions I've seen is clearing out
pre-conditions at the top of the function, and in a few cases,
if the function consists only of a single switch or a single
list of chained if/else if/else, with all branches ending in a
return. (The latter case is rarely allowed, probably because it
is difficult to formulate precisely.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34