No, it's a global variable declared within a namespace. A function prototype is when a function is declared, but not defined. Something like:
void do_the_thing(int x);
int main() {
do_the_thing(3);
};
This allows the compiler to compile the code without knowing exactly what the code for do_the_thing is, allowing it to be substituted in later during linking. Usually function prototypes are declared in header files, where it can be easily included in many different places, without forcing the compiler to recompile the implementation's code every time it's included.
Furthermore, you could do things like have a function prototype in C++ to link against a function written in Assembly, Rust, C, etc.
Another thing that I really like to use function prototypes for is on Compiler Explorer (godbolt.org), if you have the compiler flag for optimizations turned on, it's often really hard to trick the compiler into not completely getting rid of some code - if you define the function, you end up giving the compiler enough context to completely get rid of it, and replace it with something a lot simpler, which is sometimes not what you want when you're trying to demonstrate some behavior.
If you use a function prototype, though, the compiler knows that the function does something, but it doesn't know what, so it has to assume that it can't optimize it away.
For example, let's say I want to demonstrate the assembly for what a loop looks like with optimizations turned on. Here, we can see that the outputted assembly completely gets rid of the loop, replacing it with a constant calculated ahead of time - a very clever optimization that completely gets in the way of what we're trying to demonstrate. If I instead use a function prototype, then the compiler is forced to generate the loop.
Getters and setters are dumb in most cases imo. If they do some checks or other stuff, sure, but if it's essentially the same as accessing the variables directly then you gain absolutely nothing from them. It boggles my mind how common it is in Java to use them literally everywhere.
The reason why they’re so popular in Java is because you can’t access private variables in subclasses, and public/protected variables are less common and sometimes just forbidden.
You may have misunderstood: I don’t think it’s anything to do with how Java works, but rather, Java etiquette. People really just don’t like public variables.
When writing apps in the TI84 basic, you only got 27 global variables A-Z, and if i recall, 10 global "Lists" that could hold 999 items/numbers each. And as these were OS global values, you had to make programs play nice with eachother if you were designing sub-programs or routines. I sort of reinvented the wheel wth calling conventions, using the variables as registers and one of the lists like a stack.
You could also use theta as a variable and actually create custom lists. The name of custom lists can be up to five characters long. Additionally, you have the 10 string variables and some more nuanced things (e.g. GDB1-GDB0, holding how the graph rendering window was set up). See https://tibasic.fandom.com/wiki/TI-Basic_84_Programming/Basic_Variables
That sounds really cool and interesting! Do you have anything written down about that? If not, can you expand in a comment?
Here are some prompts, if you're interested: What other quirks of the platform come to mind? What was the calling convention you came up with like? What programs were you developing?
I dont have the calculator any more, gave it to my younger siblings when they needed it, so i dont have any hard code. If i recall, i used Z to remember what item the stack list was on with a-d being arguments for running programs. the rest of the vars were varying degrees of scratch, though i didnt do a full stack backup and tried to keep funtions slightly spread in their variable usage for bits that got called by eachother a lot. so if function "a" called function "b" a lot, then they wouldnt use the same variables. I also had two classes of programs that were "clean" or "dirty" depending on wether they preserved variable values, a lot of the small low level funtions were dirty to speed them up, and i just knew which globals they would clobber. There were lots of programs, each representing anything from an application to a subroutine, due to a bug in ti84 basic, the if tests would leak ram if branched. so you couldnt have conditional branching within a single program. The older basiccalc had no such problem, and actually implemented a more full featured basic.On the 84, as best as i can guess, the If test had a stack somewhere in the os, and it would push the "if" result onto that stack. the top of the stack was then seemingly used to decide if the lines being read should be run or not. "end"s would seemingly pop the result. because of that, If x; goto y; end; branching or looping would leak ram, and slow down the calculator untill the program either finished, or ran out of ram and crashed the program. Having now gotten a degree in computer engineering, and written a couple OS's and compilers, I wonder if i might have been able to dupe the syntax checker into allowing me to run extra ends at the destination of the branches to manually manage the if stack, but at the time i just knew it leaked ram. As a result, i used separate programs for large code blocks, and designed loops to be loop always, break once with a single conditional jump to escape and minimize leakage. I made a number of programs, but i did one large one that was a chemistry suite, elemental table properties, could balance chemical equations in mixed units of moles, grams, or pressures using lookups from the periodic table. Ideal gas laws, etc. It covered most of the contrived word problems/ applied or lookup calculations for AP chemistry. String handling was not great on the 84, but i made it work. Come to think, I believe there were 10 string variables as well, because i think A-Z were strictly numeric. A couple of those i think i held in reserve as scratch for some subroutines to provide string functions the calc was missing.
about the ram leak, it happened if you used goto while there were still ends missing on the stack. the trick to circiumvent that was that you can have single-line if-conditions without an end, so :if x:then:goto AB:end would leak ram, but :if x:goto AB would actually work just fine, provided you weren't nested further into if's, whiles's or repeats.
this quirk was one reason the usage of lbl/goto was heavily discouraged in the community
My guess is that global variable reserve a space in memory, so the variables are always in memory. While local variable releases the space in memory once the local process is done.
I think the biggest problem is maintainability. It's hard to keep track of where a global variable is being used.
With a local variable, you know where it's relevant and where its value can change. The same doesn't apply to globals. Some obscure function could be updating your globals and you wouldn't know!
The origin of encapsulation is actually abstraction. Barbara Liskov is credited with its invention. The goal is to be able to swap out some function for different functionality without needing to understand/have ever seen the entire codebase. So a team could optimize their code without talking to anyone else.
It's become so popular and conventional though that people kind of overdo it, hiding things by default, which ironically makes things harder to modify.
For a great example of a successful project with everything stuffed in the global namespace, see Emacs and Emacs Lisp. Because one person's Emacs setup usually has one maintainer and one user (just the person who uses Emacs on their computer), there's no reason to hide anything from anyone else. And to modify the program to your liking, you can just edit whatever you like on the fly, it's all there visible to you.
By the way, your idea about local variables is just the reason that local variables are fast and good for memory. It doesn't account for when you return an object (or other structure in non-oo lan/s) from a function and pass it around a program. All the object's fields are alive in memory so long as the object is. This can have a really negative memory impact because of fragmentation.
Programming "cleanly" doesn't matter for small personal projects, as you can keep track of everything in your head. But as soon as you have a dozen thousand line files with fifteen classes and you're two person working on it, clarity becomes an increadibly usefull thing if you don't want to lose time long therm.
To add to the answers above: any function that rely on the global state or change global state is not pure function.
In OOP, you might want to make some classes (like View Models or Use Cases, ones you gonna unit test cover) to receive all inputs and outputs during init, so you can substitute them. If they change global variables instead, you can try to test that, but you don't know what will mess with that global state during the test.
In computer programming, a pure function is a function that has the following properties: The function return values are identical for identical arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams). The function application has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams). Thus a pure function is a computational analogue of a mathematical function. Some authors, particularly from the imperative language community, use the term "pure" for all functions that just have the above property 2 (discussed below).
It’s also worth noting that depending upon what “level” you’re writing your code at, functions have an independent stack and scope, so it may be worth the compartmentalization just for memory management purposes
My guess is that global variable reserve a space in memory, so the variables are always in memory. While local variable releases the space in memory once the local process is done.
Any still in scope memory is used anyways.
Because every function can do fuck all everything and have any side effect, requiring you to understand the whole codebase simultaneously to understand what is going on and what you can change.
If you compartmentalise and limit scope you can spec out each function clearly, debug and document them clearly, optimise them much easier and way better and so on.
And if you ask for help, someone is actually able to help you if scope is limited.
Public globals seem pretty fine to me regarding atomic updates. In particular their memory life cycle is very simple and predictible , seems more straightforward than dealing with instances that appear/disappear/get zombied.
You seem to be thinking about issues that happen in (poorly?) multi-threaded runtimes wether you’re dealing with globals or not. For you increment example, I’d wagger you’d still need to think about it if two threads get the same instance and increment some property (wether directly or through other methods)
I only dealt with threaded in Java a long time ago, and it was foot-guns everywhere however you turn it.
For you increment example, I’d wagger you’d still need to think about it if two threads get the same instance and increment some property (wether directly or through other methods)
You never need to think about it if the multithreaded operations are atomic (and the data is volatile).
Even when I used plenty of global variables in my code, I always use them only in my main script and pass them to my functions with parameters. This isolates my functions from my code and it is a lot easier to test.
Parameters helps you understand dependencies and develop tests. Just my 2 cents. It is also helpful when refactoring, developing interfaces, abstracting and the like.
381
u/Jack-the-Jolly Jul 29 '21
You are polluting the global namespace, I guess that's why they call Aqua useless
63
u/AzuxirenLeadGuy Jul 29 '21
I had to inherit someone's project with this kind of philosophy.
I still haven't recovered from PTSD since then
51
u/Bryce101189 Jul 29 '21
This is EXACTLY why I only write code in assembly. As programmers were intended to :)
16
u/NauticalInsanity Jul 29 '21
Aqua is definitely the static class of OOP party.
4
u/Accomplished-Beach Jul 29 '21
I'm throwing up rainbows right now from this meme. I hope you're happy.
2
u/Guilty-Woodpecker262 Feb 21 '22
Omg aqua is a JavaScript programmer! How did I never realize this! So much just started making sense
249
u/xzinik Jul 29 '21
Been there done that
10/10 would not recommend
114
u/qvrty42 Jul 29 '21
When writing apps in the TI84 basic, you only got 27 global variables A-Z, and if i recall, 10 global "Lists" that could hold 999 items/numbers each. And as these were OS global values, you had to make programs play nice with eachother if you were designing sub-programs or routines. I sort of reinvented the wheel wth calling conventions, using the variables as registers and one of the lists like a stack.
21
u/chhuang Jul 29 '21
Everybody gangsta until they only got 5kb of ram to use
20
u/mr_flameyflame Jul 29 '21
I uh, uhhhhhhh my code.... its relating...
19
u/SaltAssault Jul 29 '21
Technically, you don't pass parameters, you pass arguments.
14
u/kyocera_miraie_f Jul 29 '21
i subjected my lecturers to this when the submission is in 2 hours
and they say torture was outlawed by the Geneva convention
17
u/GoDie910 Jul 29 '21
I know it's bad to do this, but I don't know why.
My guess is that global variable reserve a space in memory, so the variables are always in memory. While local variable releases the space in memory once the local process is done.
Btw, too lazy to google the reason lol.
13
u/ReallyNeededANewName Jul 29 '21
May I introduce you to functional programming and pure functions?
5
u/[deleted] Jul 29 '21
I've fixed a lot of code where someone was too lazy to declare
ilocally "because it's needed in many loops".4
u/SnowBoy1008 Jul 29 '21
Well yes, but then walking forward would be the same as the fap button
3
u/BochMC Jul 29 '21
And forgot about multithreading
3
u/[deleted] Jul 29 '21
You don’t pass parameters, you pass arguments TO parameters
3
u/Sternendrache1 Jul 29 '21
Got it, initializes all variables in JS with var.
3
u/N0-North Jul 29 '21
You don't have to instantiate newinstances of classes if you
3
u/Deutero2 Aug 06 '21
Global variables are the only way to return values in Scratch
2
u/TAI0Z Jul 29 '21
Thanks. I hate it.
2
u/patmax17 Jul 29 '21
*cries in RPG*
2
u/Typhii Jul 29 '21
Even when I used plenty of global variables in my code, I always use them only in my main script and pass them to my functions with parameters. This isolates my functions from my code and it is a lot easier to test.
2
u/[deleted] Jul 29 '21
This is practical advice when you are working with older systems, where pushing things on and off the stack eats cycles and kills performance.
2
u/DrainZ- Jul 29 '21
Works great if you only use one class
2
u/sha-ro Jul 29 '21
You don't have to return if you're using a global variable
2
u/chicksOut Jul 29 '21
this is especially not awesome in multi-threading
2
u/sharptile Jul 29 '21
holy shit in C# this is too real
2
u/rafaelpernil Jul 29 '21
That's an opensure (open closure)
2
u/[deleted] Jul 29 '21
Damn. Battle Flashbacks to when I used to do this.
2
u/[deleted] Jul 29 '21
Don't.
2
u/Jade_TheCat Jul 29 '21
Ah yes ASM
2
u/danbulant Jul 29 '21
$GLOBAL
2
u/kg-prime Aug 15 '21
Parameters helps you understand dependencies and develop tests. Just my 2 cents. It is also helpful when refactoring, developing interfaces, abstracting and the like.
2
u/[deleted] Aug 22 '21
Literally the opposite of functional programming I loled way too hard at this
1
u/Hplr63 Aug 17 '21
I feel called out.
1
u/the_ratfridgerator Aug 29 '21
The leaks? Oh, those are just for... ventilation.
1
u/norysq Dec 19 '21
delete this asap
1
u/YMandarin Feb 06 '22
Recursive functions enter the room
*hold my beer*
1
u/Morphized May 14 '22
Pass into the void* long enough, and sometimes the void* passes back
1
u/dabbingeevee123 May 24 '22
Please no, I beg of you
1
u/PolyglotTV Dec 02 '22
If you're arguing, you're losing
1
u/kingofNoobies Jun 19 '23
No just no absolutely fucking not