jbrave
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2010
12:29 AM
(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.
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!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
3 REPLIES 3
renojim
Community Streaming Expert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2010
02:18 AM
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:
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
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.
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.
kbenson
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2010
09:33 AM
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:
There is a solution to your problem in BrightScript. Use colons to separate multiple statements on a single line.
Note that it's a colon, not a semicolon, like many other languages. Don't get them confused...
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!
Check out Reversi! in the channel store!
jbrave
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2010
09:36 PM
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!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!