Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
Komag
Level 9

How important to use "As Dynamic" or "As Boolean"?

How important is it in my coding in all my function headings to add the "As Integer" or "As String" or whatever it's going to be?

So far I've NEVER used this in dozens and dozens of functions, and I've never had a problem (that I'm aware of).

- Is it just for my own benefit/clarity?
- Or is it more useful and important than that?
- Does it force type, such as boxing an intrinsic value? (Am I even saying that right?!)
- Is this something that would help eliminate Type mismatch errors, or could it lead to more of them?
0 Kudos
3 Replies
Rek
Level 7

Re: How important to use "As Dynamic" or "As Boolean"?

"Komag" wrote:
How important is it in my coding in all my function headings to add the "As Integer" or "As String" or whatever it's going to be?

So far I've NEVER used this in dozens and dozens of functions, and I've never had a problem (that I'm aware of).

- Is it just for my own benefit/clarity?
- Or is it more useful and important than that?
- Does it force type, such as boxing an intrinsic value? (Am I even saying that right?!)
- Is this something that would help eliminate Type mismatch errors, or could it lead to more of them?


Since brightscript is loosely typed, it shouldn't ever be strictly necessary to include the return types. However, it is good practice to include them where you can since it makes it much easier to identify bugs as you develop. For example: returning invalid instead of a real Integer when method is "as Integer" will crash -- making it clear that the incorrect type was returned from that specific method. Otherwise you could end up seeing the crash in some other code that relies on the poorly behaving function and have to trace it back to the source yourself.
0 Kudos
EnTerr
Level 8

Re: How important to use "As Dynamic" or "As Boolean"?

"Rek" wrote:
... For example: returning invalid instead of a real Integer when method is "as Integer" will crash -- making it clear that the incorrect type was returned from that specific method. Otherwise you could end up seeing the crash in some other code that relies on the poorly behaving function and have to trace it back to the source yourself.

Just to be clear, "crash" above is a metaphor for runtime error "Type Mismatch", not something else (like the famed player crash-reboots). You want it to happen at the earliest "checkpoint" because that way it is closer to where something bugged out and makes your life easier to discover/fix it. Function entrance and exit being the "check-points" here. Say you write
function celsius_to_fahrenheit(C as float) as float
Seems pretty clear, right - float enters, float exits - could not be anything else. And the executor will keep you honest - if something else tries to enter/exit, it will sound the alarm. Which is better than getting some weirdness happen (not even runtime error) five minutes later and thousands of lines away.

Yup, declaring types for some of function's parameters and return value is a Good Thing. Think of this feature as of guard rails (traffic barriers) on a road:
  • They don't have to be present but often are good idea.

  • Will they make you arrive faster, no.

  • In a way they "stand in your way" but that is not the way you want to take anyway

  • By stopping you they may save your life (and in the case of a central divider they prevent cluster-fractals from happening).
0 Kudos
Komag
Level 9

Re: How important to use "As Dynamic" or "As Boolean"?

I'm sold!

I've never had a problem (that I'm aware of).


Now I believe that I've almost certainly wasted time diagnosing problems that might have been more quickly pinned down had I been declaring types all along. Smiley Surprisedops:
0 Kudos