On Monday, 13 July 2020 09:20:24 UTC+3, Juha Nieminen wrote:
> Öö Tiib <
oot...@hot.ee> wrote:
> > Yes you are correct that the pointless optional init statement
> > was added by C++17
>
> Pointless...
>
> I suppose the 'for' loop syntax is pointless too, because the exact same
> thing can be achieved with a 'while' loop.
It was added for backward compatibility with C so why to bother?
Bart in comp.lang.c keeps critiquing it sometimes ... I don't care.
Something with backward compatibility to seventies may look awkward,
it is fine.
>
> While we are at it, 'while' is pointless too, because the same thing
> can be achieved with 'if' and 'goto'.
Yeah why C++17 did not add pointless optional init statement
to while as well to make it somewhat like half of that "good old" for?
> And 'switch' is pointless because
> the same thing can be done with 'if ... else if ...' (In fact,
> concatenated ifs are even more versatile than 'switch' because the
> comparison and the types involved don't have to all be the same,
> which makes 'switch' even more pointless.)
Switch got also that pointless init statement. What is wrong with
switch is that there must be "break;" everywhere while it is more
logical to have "do not break;" on that very rare case when we do
not want to break. But again for backward compatibility with code
written in seventies it is OK perhaps.
> And why do we even need support for declaring variables inside blocks?
> It's enough to declare all variables at the beginning of the function.
> Everything else is pointless.
Because features that make code easier to follow are not pointless.
That is logically sound:
value x = source();
if ( x.has(certain_property) ) x.do_something();
I read it "value x is acquired from source, if x has certain property
then do something with x".
Garbage features that make code less logically sound, uglier and
harder to follow however are clearly pointless. That is logically
unsound:
if ( type x = source(); x.has(some_property) ) x.do_something();
Even Yoda did not have his reasoning so messed up.
Better have block if you want to narrow the scope of x;
no need to screw up its coherence:
{
value x = source();
if ( x.has(certain_property) ) x.do_something();
}