I want my code to be readable. an IF that isn’t terminated with an End if makes for difficult to read code. Yet Brightscript seems to complain if you have every if terminated, for instance:
if x=1 then return
will throw an error if you put an end if after it thus making the code hard to read at a glance…
which is driving me nuts trying to write a message loop that will handle lots of types of messages because it appears to require lots of nested if then else statements.
I wish there was a “Switch” or “Case” statement - easier to use for handling piles of different conditions
further, it seems that every example of this kind includes an if type(msg) to determine the type of message and then
another nested if if msg.GetMessage()=“blahblah” statement.
Why even bother checking the type and then the message when we can just check the message string itself?
Just blowing off steam, but would love to see some enlightening commentary on this.
If you want to always have an ‘end if’, then don’t put the return on the same line:
if x=1 then
return
end if
I’d say it isn’t always necessary to check the msg type if the port is only associated with one type of component, but it does make the code more readable. Also, if your port is associated with more than one component, like a Springboard screen and an audio player, then your code would crash if you received an roAudioPlayerEvent and you had ‘if msg.isScreenClosed() then’ since there is no isScreenClosed() event for an audio player. There are quite a lot of events where msg.GetMessage() returns an empty string. In fact, it’s probably something that is used less often than the other means of testing for the purpose of the message.
Single line if statements don’t require an end if, in the same vein as single line if statements in C/C++, PHP, etc. don’t require braces.
In C:
if (boolean)
statement;
not_part_of_if_statement();
There is a solution to your problem in BrightScript. Use colons to separate multiple statements on a single line.
if x=1 : x = x+1 : y = y-1 : end if
function myfunc() : return "test" : end function
if x=1 : print "1"
elseif x=2 : print "2"
elseif x=3 : print "3"
else : print "I dunno"
end if
Note that it’s a colon, not a semicolon, like many other languages. Don’t get them confused…