Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Case - Bad Style? 5

Status
Not open for further replies.

PScottC

MIS
Mar 16, 2003
1,285
US
I use this form from time to time instead of using complex "If...End If" statements. Is it inefficient? Bad style? The path to eternal damnation?

Code:
Dim strMyString

Const strOne = "String 1"
Const strTwo = "String 2"
Const strThree = "String 3"

strMyString = "Some string data from another process/function/sub/etc"

[blue]Select Case True[/blue]
	Case (InStr(strMyString, strOne) > 0), (InStr(strMyString, strTwo) > 0)
		' Do stuff

	Case (InStr(strMyString, strThree) > 0)
		' Do other stuff

End Select

It subtly distubs me, but I don't know why. Opinions?

PSC
[—] CCNP (R&S/Wireless) [•] CCSP [•] MCITP: Enterprise Admin [•] MCSE [—]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
For me, Select Case is more readable/maintenable than complex If ElseIf ...
 
I'm wondering specifically about "Select Case True". Does that make the evaluation of each case more resource intensive?

PSC
[—] CCNP (R&S/Wireless) [•] CCSP [•] MCITP: Enterprise Admin [•] MCSE [—]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Typically the form of a Select statement is...
Code:
Select Case <expression>
    Case <value>
End Select

I'm turning it on it's head with...
Code:
Select Case <value>
    Case <expression>
End Select

Am I shooting myself in the foot or just over analyzing?

PSC
[&mdash;] CCNP (R&S/Wireless) [&bull;] CCSP [&bull;] MCITP: Enterprise Admin [&bull;] MCSE [&mdash;]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
i agree with PHV

but you are on the right lines with the your fear about the evaluation. i guess that the select case would have to evaluate all of the expressions. in your example this shouldnt have too much of an impact, but if you have a more resource intensive expressions...and / or lots more Case statements then i would say for efficiency the If Else etc would be more efficient but wouldnt look as nice.

for me efficiency always wins over good looking code

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
mrmovie, why are you thinking the Case statement does more evaluations than the corresponding If/ElseIf ?
 
'i am guessing in the If\Then example before only one calculation of Instr is done, compare with the Select Case where three would be made.
PHV i guess you mean it all depends on the order in which the If/Then is presented or the order in which the select case is presented, so i am being unfair in the example i have given below. I concede, there *could* be no difference in calculations via each approach

If Instr(x, y) Then

Else
If Instr(a,b) Or Instr(c,d) Then

End If
End If

'compared to

Const strOne = "String 1"
Const strTwo = "String 2"
Const strThree = "String 3"

strMyString = "Some string data from another process/function/sub/etc"

Select Case True
Case (InStr(strMyString, strOne) > 0), (InStr(strMyString, strTwo) > 0)
' Do stuff

Case (InStr(strMyString, strThree) > 0)
' Do other stuff

End Select


I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Code:
x = 0
Select Case True
	Case (x = 0)
		Wscript.echo "Yes, x = 0"
	Case (5/x = 1)
		Wscript.echo "Should give an error"
End Select
The code above returns "Yes, x = 0", not a division by zero error, so that would imply that case statements are interpreted in order, not all at once
 
Style? and the inevitable "good" and "bad"? sound like philosophy in the bedroom. (I use the construction with zero disturbance.)

However, the construction would not carry through to the .dot framework. As in that case, the "case" statement demand a "Constant Expression" that can be unambigously evaluated at compile-time.
Hence you are not going to use that "Style" in dotnet and you would have you mind in peace.
 
Not for c#, switch-case. So, to be more substantive, not for .net c#. So you have more peace of mind. Sorry vb guys.
 
Great discussion everyone! I appreciate the brainpower you applied to the question.

I'm going to accept the answer as...
Code:
Select Case True
    Case (InStr(strMyString, strOne) > 0), (InStr(strMyString, strTwo) > 0)
        ' Do stuff
    Case (InStr(strMyString, strThree) > 0)
        ' Do other stuff
End Select
is equivalent to...
Code:
If (InStr(strMyString, strOne) > 0) Or (InStr(strMyString, strTwo) > 0) Then
    ' Do Stuff
ElseIf (InStr(strMyString, strThree) > 0) Then
    ' Do other stuff
End If
...because "Select...Case...End Select" statements are processed top to bottom, and processing stops when it encounters a case that matches the requirement. (At least in VBScript, and some of its VB relatives.)

The concensus is that style is for readability and not necessarily performance in this case, and that readability could vary with the code involved.

PSC
[&mdash;] CCNP (R&S/Wireless) [&bull;] CCSP [&bull;] MCITP: Enterprise Admin [&bull;] MCSE [&mdash;]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top