Cory Posted March 19, 2009 Report Share Posted March 19, 2009 Can someone tell me why it's desirable over IF conditions except that it's easier to read? Paul's recent post just reminded me of this question in the back of my mind. As I see it with my limited experience it only adds more lines of code. I read some things online about switch/case in general programming and the common answer was that it really doesn't make a difference anymore regarding performance but I take things like that with a grain of salt. So in terms of logical operations is there some sublime way it can be implemented that give it more capabilities or better performance? Quote Link to comment Share on other sites More sharing options...
paul Posted March 20, 2009 Report Share Posted March 20, 2009 Speaking personally... I abhor long complex IF statements, and I strongly dislike the lack of a true ElseIf. I can do this in many languages: If condition1 --do this ElseIf (or even Else if) condition2 --do that Else --do something else End If (or EndIf) Here it is clear that the ElseIf and Else statements are true alternatives to condition1. Whereas ME gives me: If condition1 --do this Else --If condition2 ----do that --Else ----do something else --End If End If (Why, oh why, does this forum software suppress leading spaces?) I therefore have an extra End If. and a misleading indentation for condition2, all of which makes reading it more difficult. And it's not at all clear that condition2 is a true alternative to condition1! You should never worrry about extra lines of code - readability is a prize really worth chasing. And performance is irrelevant unless you are repeating code hundreds, thousands or more times. I learnt this valuable lesson back in 1971 when I was just starting to learn how to program. We used a Burroughs COBOL compiler which supported 49 levels of nesting within an IF statement instead of the usual 3. I wrote this horrendous IF statement, which went to 15 or so levels, and took around 60 lines of code. The amazing thing is that it worked first time. Six months later I had to change it, and I could not understand the code, even though I had written it! I ended up recasting and rewriting, and it took longer than the original task! It's the same in publishing - use more, not less, white space. It makes the text easier to follow and prettier to look at. Quote Link to comment Share on other sites More sharing options...
stevecasper Posted March 20, 2009 Report Share Posted March 20, 2009 Speaking personally...I abhor long complex IF statements, and I strongly dislike the lack of a true ElseIf. I can do this in many languages: If condition1 --do this ElseIf (or even Else if) condition2 --do that Else --do something else End If (or EndIf) Here it is clear that the ElseIf and Else statements are true alternatives to condition1. Whereas ME gives me: If condition1 --do this Else --If condition2 ----do that --Else ----do something else --End If End If (Why, oh why, does this forum software suppress leading spaces?) I therefore have an extra End If. and a misleading indentation for condition2, all of which makes reading it more difficult. And it's not at all clear that condition2 is a true alternative to condition1! You should never worrry about extra lines of code - readability is a prize really worth chasing. And performance is irrelevant unless you are repeating code hundreds, thousands or more times. I learnt this valuable lesson back in 1971 when I was just starting to learn how to program. We used a Burroughs COBOL compiler which supported 49 levels of nesting within an IF statement instead of the usual 3. I wrote this horrendous IF statement, which went to 15 or so levels, and took around 60 lines of code. The amazing thing is that it worked first time. Six months later I had to change it, and I could not understand the code, even though I had written it! I ended up recasting and rewriting, and it took longer than the original task! It's the same in publishing - use more, not less, white space. It makes the text easier to follow and prettier to look at. I get lost all the time in some of my more complex If/Else statements. I was actually going to start a thread here soon asking for an explanation of how Case/Switch statements work (I tried playing with them in ME3, but could never get them to work). I admittedly have extremely limited programming experience, the majority of it in recent years has been dedicated solely to Macro Express. If Switch Case statements are clearer and easier to read than If/Else statements when the logic gets complex, I would love to have a better understanding of how to utilize them. Quote Link to comment Share on other sites More sharing options...
Cory Posted March 20, 2009 Author Report Share Posted March 20, 2009 I have no doubt that they can be easier to read but that wasn't my question. But I do agree that readability is very important and I do tend to do things a long winded way for that reason. I often have routines that would be more efficient to lump into one complex monster but as I write more and more complex code I find myself breaking things into simpler conceptual chunks even though it adds a lot of code and possible run time. I just wanted to know if there was any performance benefit because to me it looks like it would be the same since this is uncompiled script. I guess the answer is "No". One good thing I did find after writing is we don't have to use OR statements to combine. The downside to that is that I can only use the OR condition to combine. I seem to remember Case and Switch being two separate commands in something I learned many years ago. IOW instead of starting with a Switch and then having if/then like Case brackets we simply used Case or Switch then the conditions. I think Case broke as soon as a match was found and Switch made each comparison. And we could do Ors and Ands in line. So I guess I kind of am detracted by the chubby looking implementation here but that's better for new users I suppose. Paul: Try putting your 'spaces as indents' in a code or quote box. Quote Link to comment Share on other sites More sharing options...
paul Posted March 20, 2009 Report Share Posted March 20, 2009 Paul: Try putting your 'spaces as indents' in a code or quote box. I thought I'd already tried that a while back, but I'll give it another go. Thanks. Quote Link to comment Share on other sites More sharing options...
paul Posted March 20, 2009 Report Share Posted March 20, 2009 One good thing I did find after writing is we don't have to use OR statements to combine. The downside to that is that I can only use the OR condition to combine. I don't follow?? Switch and Case are part of the same structure. Switch starts the structure, while Case denotes the individual components. Suppose I have a variable that can contain 1 of 3 values, and I want to take a different action for each. Switch MyVar Case "a" action a End Case Case "b" action "b" End Case Case "c" action "c" End Case Default Case Error! End Case End Switch Quote Link to comment Share on other sites More sharing options...
Cory Posted March 21, 2009 Author Report Share Posted March 21, 2009 I was saying that if conditions were combined you don't have to use OR. Like this, to use your example: Switch MyVar Case "a" action X End Case Case "b" Case "c" action Y End Case Default Case Error! End Case End Switch I'm pretty sure that somewhere in my programming past Case and Switch were different commands. Case would stop comparisons when a match was found and Switch would continue. Or maybe I have that backward. Case MyVar >2 Do something <0 OR =1 Do something else EndCase It was very concise and didn't require breaks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.