Your Digital Media Has Never Looked So Good

User avatar
Roku Engineering
Posts: 1581
Joined: Mon Jun 09, 2008 9:20 am

Re: The quest to understand refcnt

Mon Nov 24, 2014 9:40 am

dev42 wrote:
Why are global variables evil?

Well I'm sure you can find hundreds of essays on this topic on the web. Basically global variables have unlimited scope, and can be accessed and modified anywhere. This makes code harder to analyze. For example:
a = 100
a = func3()

In this code, it's trivial to see that func1 uses the value of a but doesn't modify it, func2 neither uses nor modifies it, and func3 modifies it without using it. You also know that just before func3 is called, a still has the value 100.

Now look at it with globals:
m.a = 100

In this case, suppose the data access is the same, func1 uses m.a, func2 neither uses it nor modifies it, and func3 modifies it. You can't tell any of this by looking at the calling code. You'd have to go read every line of func1, func2 and func3 to determine what they do (if anything) with m.a. Limiting the scope of variables makes it easier to keep track of how data is used.

Global variables are a big topic and this is just one aspect of it. I'd suggest you read up on it if you're interested.

Topic Author
Posts: 82
Joined: Thu May 24, 2012 9:43 pm

Re: The quest to understand refcnt

Mon Nov 24, 2014 11:50 am

I appreciate the response and example. Yes, I do realize it is a "hot topic" (googleable) and your example does prove something. Yet, it doesn't solve my dilemma of needing access to data that the current function's "m" ( this pointer / scope ) doesn't provide.

To avoid the use of Global Vars (everywhere!), I've passed a reference to the parent object on to what I thought was the child object. In another thread TheEndless pointed out that I may be creating circular references by doing this. Since I don't get any "orphan" message on screen, I'm going to assume I'm not exactly doing this with child "objects" but with 2 & 3 hop "nested" functions. Either way, it seemed that this was a possibility the refcnt seemed higher than -- I thought -- it needed to be. But it was one way to avoid creating more and more global vars.

But in regard to the use of global variables, my current use is limited to (a) Setting them once early and then only getting them through-out the code or (b) as something that everybody can access / change / etc who's "value" doesn't matter as it is wiped out every-time through the loop. IOW, a bitmap. No need to Create it everytime it is needed and I'm still not sure -- in this case -- the benefit of not using a global variable.

Other than that, I've been attempting to separate function from form. So, I could change the display of the data without having to deal with anything in regard to the code that deals with the data. I'm not sure what this is called... good programming practices? :wink: or is there a buzzword, like something similar to MVC ( Model View Controller ) type thing?

Who is online

Users browsing this forum: No registered users and 8 guests