Using Cases

BlitzMax Forums/BlitzMax Tutorials/Using Cases

Hardcoal(Posted 2013) [#1]
Using Cases instead of If Then is much clearer for the Eyes.
I recommend using it more then If Then in general


Wiebo(Posted 2013) [#2]
Thanks man!


H&K(Posted 2013) [#3]
What about

Goto 5000+(50*f)



GfK(Posted 2013) [#4]
This isn't a tutorial. It's an opinion.


Brucey(Posted 2013) [#5]
It's funny you should mention that, because I changed a Case statement to an If/Then today… (yes, really)

And as GfK says, this isn't a tutorial.


Hardcoal(Posted 2013) [#6]
Yes, its an Advice. well there is no Advice section right?
It is joke for those who are experienced But it was only a highlight for the newbies.

a Small eXample of what I Meant


'If Then Way
  If Mid(Text, 1, 1) = " " Or Mid(Text, 1, 1) = "/" Or Mid(Text, 1, 1) = "\" Then
	
  End If

'Case Way
  Select Mid(Text, 1, 1)
       Case " ", "/", "\"

  End Select




(Next Time Ill put stuff like that in begginers Section)


TaskMaster(Posted 2013) [#7]
How about:

'If Then Way
 Local i:String=Text[..1]
 If i = " " Or i = "/" Or i = "\" Then
	
 End If

'Case Way
  Select Mid(Text, 1, 1)
       Case " ", "/", "\"

  End Select


But like others said, this is entirely preference. And, I am unsure if the code of one way or another comes out better or not. It also depends on what the statement is doing, if you have multiple sections of the Text you want to compare, etc...


TomToad(Posted 2013) [#8]
or
If " /\".find(Chr(Text[0]) Then

End If



Hardcoal(Posted 2013) [#9]
Didnt know that Tom. so many ways.. im sure many arnt aware of.
that why learning the basics is essential as well


zoqfotpik(Posted 2013) [#10]
Maybe a good rule of thumb is that for shorter clauses you should use if and larger ones you should use case.

If you want to write very small functions you can do without either.
function blah(x:int)
    return (x>5)
end


This way the truth value is encapsulated right in the return statement. You can chain a number of these statements

if foo(x) * bar(x) * baz(x) do such_and_such


If any of these is false the whole if clause returns as false.

But if you are reading someone else's code, unless the functions are named very clearly, you would probably want a case statement which trades off conciseness for readability.

There are literally an infinite number of ways to do things...

You could even build up a list of function pointers at runtime and make a function that takes a list as an argument, evaluates all the function pointers in turn and returns whether the entire list evaluates as true or false. That's a rather unusual construct these days that it's possible to get a huge amount of mileage out of.