PDA

View Full Version : Select Case Vs IF statements in CallBacks



marcuslee
21-11-2009, 19:48
I'm trying to come up with some easy examples for either the help file, the tutorials, or just for my own understanding. We'll see where this goes.

What are the pros and cons of using either of the following two examples?



Select Case CBMSG
Case %WM_COMMAND
Select Case CBCTLMSG
Case %BN_CLICKED
Select Case CBCTL
Case %Btn
Control GET TEXT hDlg, %Txt1 To txt
Control SET TEXT hDlg, %Txt2, txt
End Select
End Select
End Select
And this one:


If CBMSG = %WM_COMMAND Then
If CBCTLMSG = %BN_CLICKED Then
Control GET TEXT hDlg, %Txt1 To txt
Control SET TEXT hDlg, %Txt2, txt
End If
End If


I see certain advantages with the IF example, especially for beginners. For an easy example with only one button, it would be easier to read than a multi-line Select Case. But, then, I have to think about speed. If you have a complicated CallBack with multiple controls, multiple messages, Select Case would appear to be faster. Am I on the right track?



Mark

Petr Schreiber
21-11-2009, 20:30
Hi Mark,

the SELECT CASE is good choice when you need to compare single value to multiple possible CASEs.
Here:


Select Case CBMSG
Case %WM_COMMAND
Select Case CBCTLMSG
Case %BN_CLICKED
Select Case CBCTL
Case %Btn
Control GET TEXT hDlg, %Txt1 To txt
Control SET TEXT hDlg, %Txt2, txt
End Select
End Select
End Select

... each of the SELECT CASE blocks has just one CASE to test. In this situation I would prefer to use IFs.

On the other side here:


SELECT CASE fileExtension
CASE "TXT"
...

CASE "INI"
...

CASE "XML"
...

CASE "DAT"
...

END SELECT

...the SELECT CASE is true winner. You read the test value once (file extension) and compare it to possibilities in "table". This has positive speed effect as well.

With IFs it would be like:


IF fileExtension = "TXT" THEN
...
ELSEIF fileExtension = "INI" THEN
...
ELSEIF fileExtension = "XML" THEN
...
ELSEIF fileExtension = "DAT" THEN
...
END IF

... which looks clumsy. Although we are testing values of single string variable (fileExtension), we repeat it in each condition.

But also do not forget you can easily combine SELECT CASE and IF approach for optimum code clarity and performance:


' -- Block for testing button clicks inside WM_COMMAND

If CBCTLMSG = %BN_CLICKED Then

Select Case CBCTL

Case %buttonOK
MsgBox CBHNDL, "Ok"

Case %buttonCancel
MsgBox CBHNDL, "Exiting program"
Dialog End CBHNDL

Case %buttonNew
...

Case %buttonOpen
...

Case %buttonSave
...

Case %buttonSaveAs
...

End Select

End If


So my personal rule is - when I test one value for multiple cases, I pick SELECT CASE otherwise IF.
Both constructions have their place and use.

I use SELECT CASE in callbacks extensively, because I usually process 3+ types of messages, and dialogs usually have more than single button. For this reason I start the main message and control tests with SELECT CASE, as I know I can easily extend them in future.

marcuslee
21-11-2009, 22:47
I use SELECT CASE in callbacks extensively, because I usually process 3+ types of messages, and dialogs usually have more than single button. For this reason I start the main message and control tests with SELECT CASE, as I know I can easily extend them in future.


Excellent reply, Petr!

But, there isn't anything ackward about using IF if all you are doing is something really simple and you know that there will be nothing added? It works, right?

I don't know what it is really, but I have always had a problem with Select Case. I understand the concept, but I guess because I learned IF THEN so early in my study of programming that I tend to gravitate to IF THEN statements rather than Select Case. It is the same with the C type construction of an IF statement. I have a hard time understanding it. I like the BASIC type construction because it is more easily read. That's why I will stick with BASIC.

However, I need to train myself to use more versatile statements like Select Case. I just wish I had more time to devote to studying thinBasic. Oh, if only there was more time! Wouldn't we all love that?



Mark

Charles Pegge
22-11-2009, 00:48
Hi Mark,

Select .. Case can be a bit more efficient than If .. ElseIf when it comes to compiling, but when you are developing the logical structure of a program, it is often easier to use IF statements - then optimise later converting to CASE statements once you have a stable structure.

But if your program is simple or has a limited lifespan or will require restructuring later - then no need for CASES IMO.

Charles