Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
jbrave
Channel Surfer

(rant) if then else weirdness & ideal message loops

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.
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
3 REPLIES 3
renojim
Community Streaming Expert

Re: (rant) if then else weirdness & ideal message loops

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.

-JT
Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
kbenson
Visitor

Re: (rant) if then else weirdness & ideal message loops

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...
-- GandK Labs
Check out Reversi! in the channel store!
0 Kudos
jbrave
Channel Surfer

Re: (rant) if then else weirdness & ideal message loops

Thanks, that is : much easier to read!
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.