Code efficiency for if-then vs Select Case

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have several one line If - then statements (by one line, I mean "If x>1
then y=x+1") which I could recode as a select case structure. Is there trade
off point for execution/memory efficiency?

Code segment:
If EventID = 2 Or EventID = 20 Then Gala2003_2004 (EventID)
If EventID = 33 Then Gala2005 (EventID)
If EventID = 56 Then Gala2006 (EventID)
If EventID = 50 Or (EventID >= 57 And EventID <= 71) Then RHGT (EventID)
If EventID = 87 Then RHGT (EventID)
. . .


Thanks,
Jael
 
A Select Case statement would be better. The way it is written, each
condition has to be evaluated even if a previous condition tested true. With
the Select Case statements, once a true condition is found, the other
conditioins are not evaluated.

Also, (and this is a personal opinion), One Line If statements are a bad
habit. I have seen too many times when a developer could not understand why
their code didn't work. It is usually because they have put some line of
code in that is affected by the If. It is not a technical thing, it is a
human thing. Using a traditional If Then
Do Something
Else
End If

Is easier to read.

Select Case EventID
Case 2, 20
Gala2003_2004 (EventID)
Case 33
Gala2005 (EventID)
Case 56
Gala2006 (EventID)
Case 50, 57 to 71, 87
RHGT (EventID)
End Select
 
Jael said:
I have several one line If - then statements (by one line, I mean "If x>1
then y=x+1") which I could recode as a select case structure. Is there trade
off point for execution/memory efficiency?

Code segment:
If EventID = 2 Or EventID = 20 Then Gala2003_2004 (EventID)
If EventID = 33 Then Gala2005 (EventID)
If EventID = 56 Then Gala2006 (EventID)
If EventID = 50 Or (EventID >= 57 And EventID <= 71) Then RHGT (EventID)
If EventID = 87 Then RHGT (EventID)
. . .


That approach executes every If condition. For mutuall
exclusive conditions a Select Case would only evaluate the
conditions until it finds one that is true. While a human
will never notice the time difference, I strongly prefer the
Select Case because it it much easier to read and you can
specify the conditions much more concisely.
 
My thanks to you and Klatuu.

When I thought about it, it was obivious that the if-then was not as
execution efficient as the case structure. I use a lot of case structures
and you are quite correct - they are easier to read and easier to control.

Klatuu - I understand your concern re:one line if-then, but will probably
continue to use it until it bites me (standard human response...;=)

Again - thank you,
Jael
 
Back
Top