hide variables from sight

Get help using Construct 2

Post » Thu Sep 19, 2013 1:49 pm

@vee41

What's bad practice about it?spongehammer2013-09-19 13:49:37
B
48
S
16
G
9
Posts: 1,097
Reputation: 11,195

Post » Thu Sep 19, 2013 2:26 pm

[QUOTE=spongehammer]
What's bad practice about it?[/QUOTE]

You'll end up with massive amount of variables that you see all the time, instead of variables only being available in scope they are needed in. In general, using lots of global variables encourages coding style that gets messy quickly. That is how I was taught to program, avoiding global variables in situations when more efficient options are available. :)

EDIT: Here is what I was talking about.. Some of those points are relevant for C2 as well.vee412013-09-19 14:34:36
B
24
S
9
G
7
Posts: 756
Reputation: 7,302

Post » Thu Sep 19, 2013 2:39 pm

@spongehammer

This is cause he know any language like php, java .... There are globals very unsafe. AND BAD !

Here are a few reasons they you should them dont use in any language:
- Source code is easier to understand when the scope is limited to the used elements (Also in C2)
- A global variable can be get or set anytime (Makes them unsafe)
- You can easily forget to reset globals, this causes often to problems that are hard to find

Here are a few why you shouldnt use them in Javascript(Construct):
- They are slower to access in Javascript than locals because Construct/Javascript needs to check every scope
- They are often not declared near the point of use(hard to read code)
- IE automatically defines a bunch of global variables this could cause problems
- They persist for the life of the script
B
15
S
6
G
6
Posts: 512
Reputation: 5,555

Post » Thu Sep 19, 2013 3:35 pm

@vee41
@Darklinki

Ha thanks, never knew any of that
B
48
S
16
G
9
Posts: 1,097
Reputation: 11,195

Post » Thu Sep 19, 2013 4:24 pm

I agree with Vee41.

As a general practice I use myself.
* Each event sheet get's a Group labels after the event sheet.
* any variables that I need in the event sheet get put into the group
* all groups in the event sheet get put into the head group
* any access to the variable from an external event-sheet will often use a Function.Call("eventgroup.get[VAR]").

it's really really rare when I find the use of a true GLOBAL required. When I do it will often be a value like "DEBUG_MODE" and similar.

otherwise I think Darklinki does a good job of explaining the why. However to expand on that.

C2 uses a self closure. This enforces that it's impossible for the same var name at the room to conflict with any browser's own variable decleration. However the rest is right.

http://www.mindandcode.com/quick-thoughts/why-global-variables-have-slow-performance-in-javascript/

unlike many other languages that use a model of variable to memory address; which is direct. All variables in JS are stored in a table. JS actually traverses the table every time you need to access the variable. As a general rule of thumb for JS. If you need to use a variable data more than twice in any function. Then store the data locally in the function. then use it.

ie
A = 24734
function scope{
   print A + A * A / A
}
is bad do the fact A is used outside the scope and JS needs to traverse the data table where A is stored.

where as

function scope{
var b = A;
print b + b * b / b
}
is better because JS won't be traversing the variable scope table for very long. As b is the only variable and ensures quicker execution.

honestly though the example is so simple it wouldn't matter. But it adds up in heavier math computation needing to be done much mroe often... like graphics :D
B
90
S
18
G
9
Posts: 2,455
Reputation: 15,018

Previous

Return to How do I....?

Who is online

Users browsing this forum: David181, manujorgo, NECRONOMICRON, rbailey83 and 25 guests