Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / without global variables

Author
Message
edler
2
Years of Service
User Offline
Joined: 11th Jan 2018
Location:
Posted: 29th Mar 2018 08:07
Although i am a newcomer and still i am learning at a middle tutorial AppGameKit guide, do you know some tutorial to learn to good programming without global variables, with examples?

better in spanish, but i read much better that writing in english.

thanks!!

--
edler

AppGameKit forum in Spanish, yet!
PSY
Valued Member
4
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 29th Mar 2018 10:42 Edited at: 29th Mar 2018 10:43
People seem to be obsessed with that globals-are-evil-thing.
Using globals is not always bad style.

http://heather.cs.ucdavis.edu/~matloff/globals.html


PSY LABS Games
Coders don't die, they just gosub without return
puzzler2018
User Banned
Posted: 29th Mar 2018 12:51
Its either we pass variables to functions or use globals

So if 5 functions within your app requires the same 10 variables then use them as globals

if 5 functions requires 1 variable then pass it through the function

function( a as integer ).


Dybing
9
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 29th Mar 2018 16:04 Edited at: 29th Mar 2018 16:04
I tend to avoid globals, as it makes code easier to read. When passing to a function the structs and variables it'll operate on, you can at a glance see what the function is supposed to do just by reading its' signature - no need to skim the actual code of it.

That said, my code is riddled with globals - but after initial start-up they are nearly all immutable. IE, they're not changing - ever. For instance color-codes, layout positions, pre-set sprite and text IDs and so on. I do usually have one mutable global as well, a state struct that contains various data between views, but that is it. Everything else that is mutable is passed explicitly to and from functions. Reduces instances of interesting bugs significantly in my experience.
Ortu
DBPro Master
12
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 29th Mar 2018 18:16 Edited at: 29th Mar 2018 18:27
In truth, a lot of my data ends up being in the global scope, but it is highly organized into module structures and namespacing

You won't ever find anonymous or ambiguous globals such as 'positionX' but you will find things like 'Camera.baseData.positionX' within the control module's Camera global. I know at a glance exactly what data is being accessed and the effect that changing it will have. I also try to only work with a module's data within the module itself

Globals are not inherently bad, they just need a lot of organization and consistency to use without running into the pitfalls of ambiguity and name conflict.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
PHeMoX
2
Years of Service
User Offline
Joined: 9th Jan 2018
Location:
Posted: 29th Mar 2018 20:34
Honestly, it is more important to think about what the specific variables should do in terms of whether just a single function needs to know about it or multiple functions and whether or not some kind of hierarchy or order matters in such a case.

If you have 40 objects running the same function, you will definitely need local variables for whatever is more uniquely changed per object. Passing that on to a function is the way to go. Should you choose a global for that, you'd end up with a lot of copies which are all 'globals', but used only once. That's a bit silly. If you're going to use globals it's either because multiple functions need access to the variable, or passing it on through function calls isn't flexible enough. Other obvious problem of globals is how they affect all cases where they are used, not just in a single function.

I don't believe there's a real downside to using globals, except for how unnecessary usage means you're going to end up with a long list of variables that could have been used more efficiently. But I do think there's an advantage to using locals whenever you can.

Combined with type lists and arrays it's fairly possible to get a good organisation of variables going in AppGameKit 2.
hoyoyo80
4
Years of Service
User Offline
Joined: 11th May 2016
Location:
Posted: 29th Mar 2018 23:43
I chose to use global if the variable is frequently use only and shared in other function quite much.

Dont know if this is a gpod practice or not.
puzzler2018
User Banned
Posted: 29th Mar 2018 23:48 Edited at: 29th Mar 2018 23:54
global variables dont get destroyed

Local variables do

so if :-

global something[100000000] as integer
function hello()

print (something[random(1,10000000)]

endfunction


or

function something()
something as integer[10000000]

print (random(1,1000000)

endfunction


Which is better -

globals dont automatically deleted from memory
local functional ones do when the functions are finished


I think....

Edit
That was bad example, i wouldnt have arrays in local functions - cause these need to be generated every function call

pass types as

function ( r as ref _r)

r been used within the function and _r been the type defined
Golelorn
3
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 30th Mar 2018 19:37
Using globals can cause tracking down an obscure bug a complete pain to almost impossible.

You could be working on one area, and due to a global variable changing that you failed to anticipate, and you haven't worked with in months, you could spend hours if not more tracking it down. I believe this is why the advice given from veterans is to limit the use of globals.
Richard_6
3
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 2nd Apr 2018 11:23
I like to use glabals only for types. I never create isolated global variables, specially inside functions. So here is what I try to follow:
- if you're inside a function, use always local variables.
- if you're outside a function, structure all your variables inside types and make your types globals. I didn't have good experices passing all types by argument, since the code gets bloated, difficult to handle and even slow if you're inside a main loop. Some types can be useful to be passed by reference though.
GarBenjamin
AGK Developer
3
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 2nd Apr 2018 15:15 Edited at: 2nd Apr 2018 16:56
I focus on wrapping up globals to a degree (like for main objects) during development but will still have many loose globals just through the course of development. Every few dev sessions I focus on clean-up and refactoring code in general which includes wrapping up globals. And then I do it again at the end.

This approach lets me focus on getting things done to make progress rapidly while still having a somewhat solid codebase. And through the occasional code clean-ups along the way and at the end that gets its own focus. I find it works well because I rarely take the time to design out anything these days like I used to years ago. So things evolve and improve over time. By delaying focus on code structure... when I do focus on that aspect I am dealing with more stable final versions which is a benefit saving overhead time.

Really if the globals are organized with types or even naming conventions that is the important thing I think. I mean we have to remember what the purpose of focusing on such things is. Basically we want to make the code clear, bug free, easily maintained and ideally no more complex than it needs to be (i.e. simple code is generally "better" code easier to understand easier to enhance and easier to debug).

I'd say a good rule of thumb is if you notice the use of globals becoming a problem.... "Where did I define that? Which variable tells me...?", etc then it is time to focus on structure and organizing your use of globals so development becomes easier and faster.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
Phaelax
DBPro Master
17
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 2nd Apr 2018 18:03
One idea for using globals that I saw someone else do which I really liked, is to make a UDT with global variables.



This method prevents any typos from causing issues if you don't have the explicit option set. Plus, it makes it easier to track global variables in your program. You could prefix globals with something like g_, which is what I always did in the past.



Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
GarBenjamin
AGK Developer
3
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 2nd Apr 2018 20:14 Edited at: 2nd Apr 2018 20:20
@Phaelax I agree. I saw that same post by someone around here talking about wrapping up their globals into a single type.

I adopted that at a modular level in my AppGameKit development so all globals declared in a given code file (for me that is nearly always a manager of some sort) are wrapped up inside a type for that module. So then I know a certain manager's data is in managernamedata or whatever and that helps me to be sure I enforce encapsulation so only that manager is directly accessing and changing that data.

And I suppose that is something worth mentioning... I do that with object specific types as well. Meaning I always have a player type and the PlayerManager directly accesses the data in that type. Other managers (separate code files) call methods in PlayerManager such as PlayerGetXpos and PlayerApplyDamage instead of directly accessing the data themselves. It's just that now I have extended this to wrapping up all other "loose" globals in a given manager to be encapsulated the same way.

I think it is important for us to keep in mind these concepts can be implemented by choice regardless of the language being OOP or not and using globals or not.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
Phaelax
DBPro Master
17
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 2nd Apr 2018 21:36
I'm willing to bet the majority of us that tend to take a modular approach, even if not necessary, likely have a background in OOP.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Richard_6
3
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 3rd Apr 2018 11:31 Edited at: 3rd Apr 2018 11:36
Another useful stuff that helps structuring the code is to return a whole UDT from a function.

It's not usual, but after seeing an example showed by Markus, I've been incorporating in my code and it's really handy when you want to return more than one varaible from a function. It basically returns an object.

This is a very simple example:

Phaelax
DBPro Master
17
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Apr 2018 19:11
Personally, I'd pass the UDT as a reference rather than return it. Just my preference.

Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Login to post a reply

Server time is: 2020-09-24 23:13:51
Your offset time is: 2020-09-24 23:13:51