Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Coding style for small if statements Date: Sat, 22 Feb 2020 08:28:45 -0800 Organization: A noiseless patient Spider Lines: 63 Message-ID: <86eeumd1s2.fsf@linuxsc.com> References: <20200131234613.59047e037939ceb549b8e6d6@gmail.com> <86sgjafygj.fsf@linuxsc.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="a1e79922b2c02e9149a52088916eaff2"; logging-data="16010"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+KVnqbiEz6I7BiFcDkas93tFlQbiK9Vc0=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:ImcIfPApnoXep5YfYRblNeZsUqo= sha1:MSPrbH6jitCDRADdvP7RN/GktrY= Xref: csiph.com comp.lang.c:150150 Jorgen Grahn writes: > On Sun, 2020-02-16, Tim Rentsch wrote: > >> Jorgen Grahn writes: >> >>> On Fri, 2020-01-31, Anton Shepelev wrote: >>> >>>> Hello, all >>>> >>>> What do you think about the formatting and readability of >>>> the following code of mine from a display-setup function for >>>> the DOSBox emulator: >>>> >>>> if( dblh != dblw ) >>>> if( dblh ) gfx_flags |= GFX_DBL_H; else >>>> gfx_flags |= GFX_DBL_W; >>> >>> ... >>> >>>> What are your opinions? >>> >>> My opinion is simple: I like and use a short, simple, one-line if: >>> >>> if (dblh) gfx_flags |= GFX_DBL_H; >> >> I am curious. Would your criteria for shortness and simpleness >> admit the whole ball of wax here as a single one-line statement: >> >> if( dblh != dblw ) gfx_flags |= dblx ? GFX_DBL_H : GFX_DBL_H; > > ^ ^ > You mean: > > if( dblh != dblw ) gfx_flags |= dblh ? GFX_DBL_H : GFX_DBL_W; Yes I did. Fortunately you have trained your neural network to recognize and correct these minor typographical errors. > which suggests that the names are the biggest problem in that > code. (Someone mentioned the names upthread, too.) > >> My own criteria for admitting a one-line 'if' include the question >> "Does this make sense being read as a single line?", which probably >> gets a Yes answer in this case. (The "probably" arises because >> there may be other not-yet-known context that would bear on the >> question.) > > "Maybe, depending on context" here, so almost the same answer. I'm not > a big fan of conditionals- within-conditionals, and would stop to ask > myself "is there an obvious way to simplify this"? I'm not a big fan of conditionals, and so even less of a fan of conditionals-within-conditionals. However I think for me there is also something more fundamental. Using if() or if()/else is about expressing differences. Using ?: is about emphasizing similarities. Here the common elements of the (original two) assignments deserve to be highlighted, and ?: does that very effectively. That the result fits nicely into a single-line if() statement is just icing on the cake. Still it's nice to hear we arrive at (at least almost) the same destination.