Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #6239

Re: Back to Basics - IF statements

From Jim Lee_0 <jlee54@gmail.com>
Newsgroups comp.programming
Subject Re: Back to Basics - IF statements
Date 2016-01-21 00:44 -0800
Organization A noiseless patient Spider
Message-ID <n7q5jp$3gh$1@dont-email.me> (permalink)
References <5fb53de5-a057-493f-b58c-5c99572513aa@googlegroups.com> <n7ivv5$6cq$2@dont-email.me> <n7ke7v$ke4$1@dont-email.me> <n7knip$b26$1@dont-email.me>

Show all headers | View raw


Richard Heathfield wrote:

> On 19/01/16 04:34, LinuxNext wrote:
>> Richard Heathfield wrote:
>>
>>
>>>> if (id = 1) {
>>>> set $piece(^|"app"|global("index"),"^",1)=$horolog
>>>> } elseif (id = 2) {
>>>> set $piece(^|"app"|global("index"),"^",3)=$horolog
>>>> } elseif (id = 3) {
>>>> set $piece(^|"app"|global("index"),"^",5)=$horolog
>>>> } elseif (id = 4) {
>>>> set $piece(^|"app"|global("index"),"^",7)=$horolog
>>>> } elseif (id = 5) {
>>>> set $piece(^|"app"|global("index"),"^",9)=$horolog
>>>> }
>>>
>>> set $piece(^|"app"|global("index"),"^",id * 2 - 1) = $horolog
>>>
>>
>> While elegant, your solution does not behave as the original code.
> 
> Yes, it does.

No, it doesn't.

> 
>> The original would only set $piece if id was in the range of 1-5.
> 
> As the OP says, "The variable (id) is only going to be equal to one of
> the five each time it reaches this point" - so I have a pre-condition on
> which I am allowed to rely. Nevertheless:
> 

The OP's verbal guarantee is not the same thing as the actual code.
I can't count how many times I've seen such assumptions spawn subtle
code bugs. For instance, although the OP said 'id' will always be
in the range [1..5], the calling routine could have a bug that
short-circuits the setting of 'id' altogether, leaving it =0.  In that
case, your code would index to -1 which is likely a bad thing.  
Likewise, stack corruption at the OS level, ISR non-reentrancy, and
any number of other things can invalidate such assumptions.
Granted, I'm nit-picking on general principles which may not be so
important for a one-off shell script - but my point is that snippet
A != snippet B.

> id < 1 || id > 5 || (set $piece(^|"app"|global("index"),"^",id * 2 - 1)
> = $horolog)
> 
> will add the range check for you. Less elegant, of course.
> 
We are in agreement here.

Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Back to Basics - IF statements mack.altmaniii@gmail.com - 2016-01-18 07:18 -0800
  Re: Back to Basics - IF statements Richard Heathfield <rjh@cpax.org.uk> - 2016-01-18 15:24 +0000
    Re: Back to Basics - IF statements Kaz Kylheku <kaz@kylheku.com> - 2016-01-18 23:29 +0000
    Re: Back to Basics - IF statements LinuxNext <nobody@nospam.net> - 2016-01-18 20:34 -0800
      Re: Back to Basics - IF statements Richard Heathfield <rjh@cpax.org.uk> - 2016-01-19 07:14 +0000
        Re: Back to Basics - IF statements Jim Lee_0 <jlee54@gmail.com> - 2016-01-21 00:44 -0800
          Re: Back to Basics - IF statements Öö Tiib <ootiib@hot.ee> - 2016-01-21 08:53 -0800
  Re: Back to Basics - IF statements John McCue <jmccue@jmcnet1.bstnma.east.verizon.net> - 2016-01-18 22:26 +0000
    Re: Back to Basics - IF statements Richard Heathfield <rjh@cpax.org.uk> - 2016-01-18 22:33 +0000
  Re: Back to Basics - IF statements BartC <bc@freeuk.com> - 2016-01-19 13:11 +0000

csiph-web