Your Digital Media Has Never Looked So Good

 
User avatar
Komag
Topic Author
Posts: 808
Joined: Fri Aug 22, 2014 3:42 am

Trying to learn "m", need conceptual help

Wed Apr 01, 2015 11:29 am

So for a while now I've had this nagging feeling that I REALLY should be using "m". So far I haven't used it, I pass all the variables I need around all over the place, and I'm getting kind of tired of it. I have a LOT of info that I've broken down into a few main associative arrays which I pass to various functions, and I can draw info from them and change any values whenever I want.

Question - If I somehow put everything into "m", would that use more memory somehow? Not sure how garbage collector would handle stuff.
Question - How do I even do that?

The Brightscript Language Reference says:
A BrightScript object is an roAssociativeArray which contains function pointers. When a member function is called "from" an AssociativeArray, the special variable "m" is set to point to that AssociativeArray. "m" is accessible inside the called function to access other data in the AssociativeArray object.
I don't understand that at all (almost). :?

I thought I read somewhere that there are times you can't use "m"
The other relevant quote seems to be:
BrightScript does not support global variables. Except, there is one hard-coded global variable "global" that is an interface to the global BrightScript Component. The global component contains all global library functions. There is also a global context that can be accessed via the GetGlobalAA(). If in function scope and that function is not a method in an object, "m." also references the global associative array accessed with GetGlobalAA().
That too is a mystery to me, mostly. :shock:

Question - If I use "m" for "everything", it would create an additional layer for me, such as "m.cAA.mainMenu.w[1]" vs "cAA.mainMenu.w[1]". Everything would have this extra layer. Would that slow things down, since there is another dictionary lookup happening, or am I totally understanding that wrong?

As you can see I feel a bit lost on this. :|
 
User avatar
squirreltown
Posts: 870
Joined: Sun Apr 21, 2013 2:20 pm

Re: Trying to learn "m", need conceptual help

Wed Apr 01, 2015 11:44 am

It's just an associative array, but you don't need to pass it around, its built into BS.

m.thing = 0

Now I can use m.thing anywhere in any function.

It's the same as doing

varr={ thing:0}

Sub myFunction( varr as object)

 varr.thing = varr.thing+1

End Sub


I think there are some who frown on using it, but I can't see why so they can weigh in themselves.
As it's just an array, I don't think memory applies any differently, m.bitmap still behaves as any bitmap would.
Kinetics Screensavers
 
User avatar
NewManLiving
Posts: 452
Joined: Fri Aug 02, 2013 6:14 pm

Re: Trying to learn "m", need conceptual help

Wed Apr 01, 2015 11:52 am

viewtopic.php?f=34&t=83642#p479321
Gave an example here. It might help
My Channels: 2D API Framework Presentation: https://owner.roku.com/add/2M9LCVC
Updated: 11-11-2015 - Completed Keyboard interface
The Joel Channel ( Final Beta )
 
User avatar
RokuMarkn
Roku Engineering
Posts: 1581
Joined: Mon Jun 09, 2008 9:20 am

Re: Trying to learn "m", need conceptual help

Wed Apr 01, 2015 12:03 pm

Be aware that that are (somewhat confusingly) two different meanings of m. If you are in a function called through an AA, then m refers to that AA. For example:
    aa = { func1: my_func1, data: 42 }
    aa.func1(5) ' call through an AA

function my_func1(x as Integer)
    ' x is 5, m.data is 42
end function


On the other hand if you are NOT in such a function, them m refers the the "global" AA:
  m.data = 43 ' sets the global AA
  func2(5) ' not called through an AA

function func2(x as Integer)
    ' x is 5, m.data is 43
end function


The first mechanism is how Brightscript implements object-oriented programming, and is something you should definitely use. The second mechanism (global variables) is evil and should be used with caution, if ever.

--Mark
 
MatroxRT
Posts: 27
Joined: Thu Mar 12, 2015 2:03 pm

Re: Trying to learn "m", need conceptual help

Wed Apr 01, 2015 12:59 pm

That is probably the best summary I have seen Mark. It might be worth adding it to section 4.11 of the Language Ref.

I was just describing this yesterday to a co-worker, and did not do it as gracefully as your post. :)
 
User avatar
TheEndless
** Valued Community Member **
Posts: 9231
Joined: Mon Oct 04, 2004 10:15 am
Location: US
Contact:

Re: Trying to learn "m", need conceptual help

Wed Apr 01, 2015 1:03 pm

Think of "m" as BrightScript's "this". It's a reference to the current object instance. If used outside of an object instance, it refers to the global object.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
 
User avatar
Komag
Topic Author
Posts: 808
Joined: Fri Aug 22, 2014 3:42 am

Re: Trying to learn "m", need conceptual help

Wed Apr 01, 2015 1:33 pm

These responses are helping, good.
RokuMarkn wrote:
The second mechanism (global variables) is evil and should be used with caution, if ever.
I like the phrasing! But can you shed any light on why it would be wise to not use it this way much?

- Suppose I used it a lot - do I run the risk of my game breaking with some future Roku firmware update?
- Does "m" used as global variables array lead to other stability problems or memory management issues if it grows too big?
- Is it just "bad programming" but otherwise safe and harmless?
 
User avatar
RokuMarkn
Roku Engineering
Posts: 1581
Joined: Mon Jun 09, 2008 9:20 am

Re: Trying to learn "m", need conceptual help

Wed Apr 01, 2015 1:40 pm

The latter. It's bad programming style, and avoiding globals is one of the reasons Object Oriented programming was invented. I discussed this previously here. Other than poor style and difficulty in maintaining the code, it's not going to break anything if you use globals.

--Mark
 
User avatar
Komag
Topic Author
Posts: 808
Joined: Fri Aug 22, 2014 3:42 am

Re: Trying to learn "m", need conceptual help

Wed Apr 01, 2015 2:27 pm

Thanks for the clarification.
I need to better understand object oriented programming then to make best use, it sounds like.

What about this question though:
Komag wrote:
Question - If I use "m" for "everything", it would create an additional layer for me, such as "m.cAA.mainMenu.w[1]" vs "cAA.mainMenu.w[1]". Everything would have this extra layer. Would that slow things down, since there is another dictionary lookup happening, or am I totally understanding that wrong?
 
User avatar
NewManLiving
Posts: 452
Joined: Fri Aug 02, 2013 6:14 pm

Re: Trying to learn "m", need conceptual help

Wed Apr 01, 2015 3:27 pm

It would all depend on how the compiler resolves references. If it's a good compiler then it will resolve the reference at compile time. It would be nice to have an official word.
My Channels: 2D API Framework Presentation: https://owner.roku.com/add/2M9LCVC
Updated: 11-11-2015 - Completed Keyboard interface
The Joel Channel ( Final Beta )
 
User avatar
NewManLiving
Posts: 452
Joined: Fri Aug 02, 2013 6:14 pm

Re: Trying to learn "m", need conceptual help

Wed Apr 01, 2015 3:35 pm

As a side note. If I have something like m.a.b.c in a loop or where I use it over a number of times I presently resolve it myself. l_var = m.a.b.c.
My Channels: 2D API Framework Presentation: https://owner.roku.com/add/2M9LCVC
Updated: 11-11-2015 - Completed Keyboard interface
The Joel Channel ( Final Beta )
 
User avatar
TheEndless
** Valued Community Member **
Posts: 9231
Joined: Mon Oct 04, 2004 10:15 am
Location: US
Contact:

Re: Trying to learn "m", need conceptual help

Wed Apr 01, 2015 4:05 pm

RokuMarkn wrote:
The latter. It's bad programming style, and avoiding globals is one of the reasons Object Oriented programming was invented. I discussed this previously here. Other than poor style and difficulty in maintaining the code, it's not going to break anything if you use globals.

--Mark

It may also be worth noting that using the global AA is the only way to implement global singletons...
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
 
destruk
Posts: 2720
Joined: Sat Dec 18, 2010 4:58 pm

Re: Trying to learn "m", need conceptual help

Wed Apr 01, 2015 8:12 pm

If it truly is bad practice, bad programming style, evil, bad idea, etc etc then why do all these languages KEEP ALLOWING it to be used? Maybe if you feel so strongly about it, remove it in a future firmware update. Until then, there are mahy reasons why you would want to use global variables - one of which is not having to call a function with numerous values passed to it all the time, every time it is called. I find it's easier to leave things like server urls in the top of the script, as global, to reference throughout the rest of the program execution, rather than having to type out each server url over and over every time I need to get something from the internet. So thanks for leaving this 'bad practice' as an option.
 
User avatar
Komag
Topic Author
Posts: 808
Joined: Fri Aug 22, 2014 3:42 am

Re: Trying to learn "m", need conceptual help

Thu Apr 02, 2015 3:49 am

I think having a few globals like server urls is not going to do any harm, but I can see the point that using a lot of variables that get changed and dynamically used throughout the whole program could make it harder to debug and keep track of things, as illustrated by RokuMarkn Here. I suppose it comes down to experience, preference, team policy if not working alone, etc.
 
User avatar
RokuMarkn
Roku Engineering
Posts: 1581
Joined: Mon Jun 09, 2008 9:20 am

Re: Trying to learn "m", need conceptual help

Thu Apr 02, 2015 6:38 am

There are situations where globals are appropriate, such as global constants or (in some cases) singletons like a Registry or FontRegistry. Experienced programmers already know that globals, like gotos, are dangerous. But in this forum there are readers with a wide range of experience, so it's hard to give guidance to inexperienced programmers without saying something that an experienced programmer, looking at a wider range of possibilities, may object to. But when a programmer with no experience with globals talks about moving all their data into globals, the appropriate response is "don't do that". Similarly if a new programmer recently discovered gotos and was considering replacing all his for loops with the more "powerful" goto, I'd say "bad idea". I did say globals should be used with caution, not that they should never be used. I've probably used at least one global in every BrightScript program I've written. I would still advise new programmers to mostly avoid globals, since they don't yet have the experience to see when they're being used inappropriately.

--Mark

Who is online

Users browsing this forum: No registered users and 7 guests