On 07/07/16 18:09, Alf P. Steinbach wrote:
> I think this code is just fine, nothing particularly wrong, but I'm
> concerned I've changed into a mindset where I think so because I don't
> /see/ how wonderfully clearly it could be expressed otherwise?
>
> I remember discussing SESE/SEME with Andrei Alexandrescu over in clc++m
> once, where I insisted on SESE being more clear and less error prone.
> That view was, I now think, very much colored by experience maintaining
> some ugly C code with umpteen-hundred line functions. But anyway I now
> see things very differently, and I can't recall how I then thought.
>
> Is there some natural non-`return` way to code this that isn't
> needlessly complex or inefficient, perhaps even better in some way?
>
Remind me what group this is, and what language we are using here? C++
left the "SESE" idea behind when exceptions were introduced, and the
whole RAII concept is what turns multiple exits from a risky strategy
into a safe one. Every possible exception is not only an extra exit to
the function, but it is a hidden and undocumented one - yet I am sure
you are happy to use exceptions in your code.
A key argument against multiple exits in a function is the risk of
leaking resources or leaving your data structures inconsistent when
exiting before any necessary clean-up (or having to duplicate said
clean-up code). With RAII and synchronous destructors, this is a
non-issue in well-written C++ code.
The other argument against multiple exits is that it can make it hard to
see the code flow. It is simply a matter of writing your code in a way
that makes the code flow clear - and often multiple exits can do that
much more clearly than "SESE" workarounds. Certainly I find your code
with multiple returns /far/ clearer than Öö's version with assignment in
boolean expressions, and multiple function calls within an && expression.