On Tue, 29 Jun 2021 15:17:58 +0959
Reuben ua Bríġ <u5644...@anu.edu.au> wrote:

> i found the following:
> 
>         if (strcmp(progname, BINM_MAN) == 0)
>                 search.argmode = ARG_NAME;
>         else if (strcmp(progname, BINM_APROPOS) == 0)
>                 search.argmode = ARG_EXPR;
>         else if (strcmp(progname, BINM_WHATIS) == 0)
>                 search.argmode = ARG_WORD;
>         else if (strncmp(progname, "help", 4) == 0)
>                 search.argmode = ARG_NAME;
>         else
>                 search.argmode = ARG_FILE;
> 
> much more readable as:
> 
>         search.argmode =
>                 strcmp(progname, BINM_MAN) == 0 ?       ARG_NAME :
>                 strcmp(progname, BINM_APROPOS) == 0 ?   ARG_EXPR :
>                 strcmp(progname, BINM_WHATIS) == 0 ?    ARG_WORD :
>                 strncmp(progname, "help", 4) == 0 ?     ARG_NAME :
>                 ARG_FILE;

I disagree.

The former uses universally understood `if` statements, with clear
formatting of the condition expression and the true/false branches.
Some might quibble about a lack of braces, but the indentation make
things clear enough.

Even someone who didn't know "C", would be able to more-or-less
understand what is being done.  It's almost pseudo-code like.  About
the only trickiness is understanding `strcmp`/`strncmp`.

The latter uses undelimited nested ternaries, with no clear demarcation
between the ternary expression and its true/false branches.  If you
didn't know what a ternary was, you'd be totally snookered
understanding that blob of code.

Maybe it looks "prettier", but if I wanted to write "pretty" C code,
I'd enter the IOCCC.

If I absolutely *had* to use ternaries (and you'd pretty much have to
do it at gun-point, threatening some innocent bystander):

        search.argmode = (
                (strcmp(progname, BINM_MAN) == 0)
                ? ARG_NAME
                : (
                        (strcmp(progname, BINM_APROPOS) == 0) == 0)
                        ? ARG_EXPR
                        : (
                                (strcmp(progname, BINM_WHATIS) == 0)
                                ? ARG_WORD
                                : (
                                        (strncmp(progname, "help", 4) == 0)
                                        ? ARG_NAME
                                        : ARG_FILE
                                )
                        )
                )
        );

Now, it should be immediately apparent why nesting this many ternaries
is a bad idea.  The above is a marginal improvement, you can clearly see
a ternary is involved and what bits are what, but it's blindingly
obvious why I wouldn't nest that many ternaries and why `if`/`else` or
`switch` is a better solution.

Yeah, it kinda sucks there's no way to do a `switch` with strings, but
the `if`/`else` solution isn't a bad one, and on some rare occasions
(I've personally observed this on MSP430 with gcc), using `if`/`else`
generates tighter code than `switch` does.

You might argue about "operator precedence", and most languages follow
the same rules, but there's enough subtle differences between them, and
I bounce between 4 of them in my day job, that I prefer to use brackets
more than is strictly necessary.

Neither of the "ternary" examples here would qualify for OpenBSD coding
standards.

I do hope the coding styles you're showcasing to us aren't what they're
teaching at ANU.  I've heard of "Canberra bubble" referring to the
reality distortion field that the politicians down there live in, but I
didn't think it extended to the local uni there!
-- 
Stuart Longland (aka Redhatter, VK4MSL)

I haven't lost my mind...
  ...it's backed up on a tape somewhere.

Reply via email to