[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.programming

Back to Basics - IF statements

mack.altmaniii

1/18/2016 3:18:00 PM

I started web designing when I was a teen, which at that time web sites were what is now referred to as 'static' in nature. However, I grew with the community and began transitioning from designer to developer to create more 'dynamic' web sites using various various languages I picked up along the way. Although I had my own web design company as a teenager in high school, I pursued a career in accounting while considering my 'tech' skills a hobby.. Unfortunately, I was ill-advised by many saying it would never amount to anything. Fastforward a decade, I sit in a position where I use Caché (Mumps) on a daily basis. However, I often question my 'self taught' skills. While I would say I learned them often by tested (time and again) trial and error, I still doubt them.

While I believe this can be answered by any mathematics lover (Who didn't love proofs? Ha!), I want to ask so I stop questioning myself and educate those I'm having to come behind. I feel its a waste of performance, and want to feel confident in saying it is. So here goes.


The following is what I am referring to.

if (id = 1) {
set $piece(^|"app"|global("index"),"^",1)=$horolog
}
if (id = 2) {
set $piece(^|"app"|global("index"),"^",3)=$horolog
}
if (id = 3) {
set $piece(^|"app"|global("index"),"^",5)=$horolog
}
if (id = 4) {
set $piece(^|"app"|global("index"),"^",7)=$horolog
}
if (id = 5) {
set $piece(^|"app"|global("index"),"^",9)=$horolog
}

I feel each pass through the above wastes performance fivefold. The variable (id) is only going to be equal to one of the five each time it reaches this point, as it is not changed within the if() so why would we verify it hasn't changed when there's nothing telling it to change. I feel the above should be written as follows. Would anyone write the above in the same way or would everyone use the following approach?

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
}
9 Answers

Richard Heathfield

1/18/2016 3:25:00 PM

0

On 18/01/16 15:18, mack.altmaniii@gmail.com wrote:
> I started web designing when I was a teen, which at that time web sites were what is now referred to as 'static' in nature. However, I grew with the community and began transitioning from designer to developer to create more 'dynamic' web sites using various various languages I picked up along the way. Although I had my own web design company as a teenager in high school, I pursued a career in accounting while considering my 'tech' skills a hobby. Unfortunately, I was ill-advised by many saying it would never amount to anything. Fastforward a decade, I sit in a position where I use Caché (Mumps) on a daily basis. However, I often question my 'self taught' skills. While I would say I learned them often by tested (time and again) trial and error, I still doubt them.
>
> While I believe this can be answered by any mathematics lover (Who didn't love proofs? Ha!), I want to ask so I stop questioning myself and educate those I'm having to come behind. I feel its a waste of performance, and want to feel confident in saying it is. So here goes.
>
>
> The following is what I am referring to.
>
> if (id = 1) {
> set $piece(^|"app"|global("index"),"^",1)=$horolog
> }
> if (id = 2) {
> set $piece(^|"app"|global("index"),"^",3)=$horolog
> }
> if (id = 3) {
> set $piece(^|"app"|global("index"),"^",5)=$horolog
> }
> if (id = 4) {
> set $piece(^|"app"|global("index"),"^",7)=$horolog
> }
> if (id = 5) {
> set $piece(^|"app"|global("index"),"^",9)=$horolog
> }
>
> I feel each pass through the above wastes performance fivefold.
> The variable (id) is only going to be equal to one of the five
> each time it reaches this point, as it is not changed within
> the if() so why would we verify it hasn't changed when there's
> nothing telling it to change. I feel the above should be written
> as follows. Would anyone write the above in the same way or would
> everyone use the following approach?
>
> 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

--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

John McCue

1/18/2016 10:26:00 PM

0

mack.altmaniii@gmail.com wrote:
<snip>

> if (id = 1) {
> set $piece(^|"app"|global("index"),"^",1)=$horolog
> } elseif (id = 2) {
<snip>

I cannot stand 'elseif', I would try a figure out a different
way of doing this. Massive if statements should be avoided
if possible.

John

PS, please figure out how to wordwrap, usually I ignore posts
formatted as yours was, but i hate 'elseifs' worse :)

Richard Heathfield

1/18/2016 10:34:00 PM

0

On 18/01/16 22:26, John McCue wrote:
> mack.altmaniii@gmail.com wrote:
> <snip>
>
>> if (id = 1) {
>> set $piece(^|"app"|global("index"),"^",1)=$horolog
>> } elseif (id = 2) {
> <snip>
>
> I cannot stand 'elseif', I would try a figure out a different
> way of doing this. Massive if statements should be avoided
> if possible.

I don't see any need for even a lightweight if statement. :-)

--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

Kaz Kylheku

1/18/2016 11:30:00 PM

0

On 2016-01-18, Richard Heathfield <rjh@cpax.org.uk> wrote:
> On 18/01/16 15:18, mack.altmaniii@gmail.com 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

Unfortunately, you've erased all traces of humor inherent in the
original.

The goal is to deliver the punchline without boring the audience.

The following howler makes essentially the same gag, without the verbose
repetition of similar code:

for (i = 1; i <= 5; i++) { // replace with loop syntax for language
if (id = i)
set $piece(^|"app"|global("index"),"^", i * 2 - 1) = $horolog
}

T

Kerry Edwin Gerhard

1/19/2016 4:35:00 AM

0

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.

The original would only set $piece if id was in the range of 1-5.

Your code will set $piece for any value of id without checking.


Richard Heathfield

1/19/2016 7:14:00 AM

0

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.

> 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:

id < 1 || id > 5 || (set $piece(^|"app"|global("index"),"^",id * 2 - 1)
= $horolog)

will add the range check for you. Less elegant, of course.

--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

Bartc

1/19/2016 1:12:00 PM

0

On 18/01/2016 15:18, mack.altmaniii@gmail.com wrote:

> if (id = 1) {
> set $piece(^|"app"|global("index"),"^",1)=$horolog
> }
> if (id = 2) {
> set $piece(^|"app"|global("index"),"^",3)=$horolog
> }
> if (id = 3) {
> set $piece(^|"app"|global("index"),"^",5)=$horolog
> }
> if (id = 4) {
> set $piece(^|"app"|global("index"),"^",7)=$horolog
> }
> if (id = 5) {
> set $piece(^|"app"|global("index"),"^",9)=$horolog
> }
>
> I feel each pass through the above wastes performance fivefold.

Not necessarily, assuming the 'set $piece' assignment takes much longer
to execute than 'if (id=N)'.

Assuming id is 1 to 5, it will always do five comparisons and one
assignment. Up to four of those comparisons will be unnecessary.

> 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
> }

This will do up to 5 comparisons, and one assignment. There are no
unnecessary ones. Although, if you /know/ that id will be 1 to 5, then
that last 'elseif (id=5)' can just be 'else'. Now it will only do up to
4 comparisons.

If you additionally know that some values of id are more common that
others, you can arrange to test for those values first.

Of course, you can also write the whole thing as one assignment with
'id' as a parameter as others have shown. But since you've already gone
to the trouble of writing this out...

--
Bartc

Jim Lee_0

1/21/2016 8:44:00 AM

0

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.

Öö Tiib

1/21/2016 4:53:00 PM

0

On Thursday, 21 January 2016 10:44:19 UTC+2, Jim Lee_0 wrote:
> 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.

It does within defined limits but does not behave same with input outside
of defined limits.

>
> >
> >> 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.

Most programmers typically prefer rather noisy "bad thing"
in situations when preconditions are not met since reasons of
those are cheaper to discover, to track down and to fix.

> 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.

Perhaps both codes would benefit from some precondition checking
instead.

assert(0<id && id < 6);