Just create a global object and stuff your variable in there. Now you have a global singleton and that’s not a purely bad practice :D
The temptation is always there
Submitted 1 year ago by Sgt_choke_n_stroke@lemmy.world to programmer_humor@programming.dev
https://lemmy.world/pictrs/image/9c65ec36-cd62-42d0-b2b0-fad2ceb5a014.jpeg
Comments
idunnololz@lemmy.world 1 year ago
Techmaster@lemm.ee 1 year ago
Just call it “state management” and nobody will even care.
magic_lobster_party@kbin.social 1 year ago
Not necessarily a bad practice if the singleton is immutable, and it’s provided via dependency injection for example.
wreckedcarzz@lemmy.world 1 year ago
As a hobby coder: “mmmhm, mmmm, mmhm… I know some of these words!”
Anonymousllama@lemmy.world 1 year ago
Important to contain all your mess to one side of the room, makes it easier to manage
shotgun_crab@lemmy.world 1 year ago
Yeah yeah let’s put all the eggs in one basket
mdk_@lemmy.world 1 year ago
So you saying, just the tip?
xmunk@sh.itjust.works 1 year ago
Real enterprise programmers know that everything should be on the stack… so they declare a List in main.
manapropos@lemmy.basedcount.com 1 year ago
Real enterprise programmers know you can get another job in the next year or two so fuck best practice
idunnololz@lemmy.world 1 year ago
But we might need to add more features in the future so it might not just be a list in a few years. Better encapsulate it in a few layers of abstractions just to be safe.
HurgletOfficial@lemmy.basedcount.com 1 year ago
I do this all the time in Python by creating a class like
class Core: foo = "bar"
ryannathans@aussie.zone 1 year ago
That’s not immutable nor a singleton
SquishyPandaDev@yiffit.net 1 year ago
Obligatory, mutable global variables are evil.
magic_lobster_party@kbin.social 1 year ago
The definition of a variable is that it’s mutable. If it’s immutable it’s constant.
marcos@lemmy.world 1 year ago
There’s no ISO standardized definition for variable. People use that word with all kinds of meaning.
Walnut356@programming.dev 1 year ago
I feel like it’s like pointers.
“Variable” refers to the label, i.e. a box that can contain anything (like *ptr is a pointer to [something we dont know anything about])
Immutable describes the contents, i.e. the stuff in the box cant change. (like int* ptr is a pointer to an int)
Rust makes it very obvious that there’s a difference between constants and immutable variables, mainly because constants must be compile time constants.
What do you call it when a variable cant change after its definition, but isnt guaranteed to be the same on each function call? It’s not a constant, the contents of that label are “changing”, but the label’s contents cant be modified inside the scope of that function. So it’s a variable, but immutable.
Eufalconimorph@discuss.tchncs.de 1 year ago
And more generally mutable aliasing references of any sort are evil. Doesn’t mean they’re not useful, just that you need magic protection spells (mutexes, semaphores, fancy lock-free algorithms, atomics, etc) to use them safely. Skip the spell or use she wrong one, and the demon escapes and destroys all you hold dear.
fsxylo@sh.itjust.works 1 year ago
Singletons:
Me: O_O;
KittyCat@lemmy.world 1 year ago
You can do better, define intergalactic variables that share the same memory location across multiple programs so you can seamlessly pass variables from one to the next.
Decompose@programming.dev 1 year ago
Is that you… Windows 95?
hansl@lemmy.world 1 year ago
The ONE TRUE CONSTANT; even with an infinite universe, the value is the same in all of them.
Fungah@lemmy.world 1 year ago
Are the legends really true?
Fungah@lemmy.world 1 year ago
But not sand memory. It’s coars ena drogjh and irritating and gets everywhere.
alphacyberranger@lemmy.world 1 year ago
Is it an orgy if multiple global variables are used in a multi threaded code?
darcy@sh.itjust.works 1 year ago
not if everyone uses a Mutex. stay (thread) safe
noli@programming.dev 1 year ago
Always practice safe stacks
Successful_Try543@feddit.de 1 year ago
I’ve once had a course involving programming and the lecturer rewrote the code, which we were usually using at our institute, making ALL variables global. - Yes, also each and every loop counter and iterator. 🤪
Chriszz@lemmy.world 1 year ago
There’s no way you teach a uni course and do this kind of thing unless to demonstrate poor practice/run time difference. Are you sure you were paying attention?
Successful_Try543@feddit.de 1 year ago
Yes. He really thought it was efficient and would avoid errors if literally all variables were defined in a single Matlab function he called at the beginning of the script. We students all thought: “Man, are you serious.” As we didn’t want to debug such a mess, in our code, we ignored what he was doing and kept using local variables.
rtxn@lemmy.world 1 year ago
I’ve seen two teachers do this, both of them mathematics professors who teach programming for the extra cash. One teaches C, the other Pascal.
wreckedcarzz@lemmy.world 1 year ago
Oh they were paying, way too much
DigitalPaperTrail@kbin.social 1 year ago
"I can make them worse"
winterayars@sh.itjust.works 1 year ago
“But what if I put the whole program into a class and then made it a class wide variable?”
MrMagnesium12@feddit.de 1 year ago
Sounds like the piece of legacy software I have do deal with.
fbmac@lemmy.fbmac.net 1 year ago
stephfinitely@lemmy.world 1 year ago
I am not a programmer who knows how to program. I know this because global variables are how I fix most the issue I run into, but are constantly told this wrong.
dingleberry@discuss.tchncs.de 1 year ago
Exhibitionist Devs be like.
dylanTheDeveloper@lemmy.world 1 year ago
Me putting everything in ‘public:’
pewgar_kbin@fedia.io 1 year ago
the meme is spreading
BilboBargains@lemmy.world 1 year ago
Our Father, who art in Microsoft HQ, hallowed be thy naming conventions,; thy architecture; thy will be done; on earth as it is in Linus Tech Tips. Give us this day our daily StackOverflow. And forgive us our 'sploits, as we forgive those who trespass against our user stories. And lead us not into temptation; but deliver us from a thicket of global variables. For thine is the irritating project manager, the power and the glory, for ever and ever. Or at least 7 years until obsolescence. Amen.
Blackmist@feddit.uk 1 year ago
Nothing wrong with global variables.
If anyone asks just say it’s the singleton pattern.
gandalf_der_12te@feddit.de 1 year ago
accurate
Sanctus@lemmy.world 1 year ago
Ironically, a guy named SingletonSean showed me how to avoid singletons.
CoWizard@kbin.social 1 year ago
UndercoverUlrikHD@programming.dev 1 year ago
Is it really tempting for people? They’ve given me too many headaches when I’ve had to reformat or add functionality to files.
Unless it’s a simple single use script that fit on the computer screen, I don’t feel like global variables would ever be tempting, unless it’s for constants.
PetDinosaurs@lemmy.world 1 year ago
Most people suck at software engineering.
Plus, there’s always the temptation to do it the shitty way and “fix it later” (which never happens).
You pay your technical debt. One way or another.
It’s way worse than any gangster.
squaresinger@feddit.de 1 year ago
Not if you leave the project soon enough. It’s like tech debt chicken.
rodolfo@lemmy.world 1 year ago
amen
double amen
FlickOfTheBean@lemmy.world 1 year ago
Rarely have I ever actually had consequences for my sins, which tends to be why I don’t go back and fix them…
If tech debt weight is felt in any way, it tends to get fixed. If it’s not felt, it’s just incredibly easy to forget and disregard.
(This is mostly me not learning my lesson well enough from my time being on Tech Debt: The Team. I do try and figure out the correct way to do things, but at the end of the day, I get paid to do what the boss wants as cheaply as possible, not what’s right :/ money dgaf about best practices until someone gets sued for malpractice, but on that logic, maybe the tech debt piper just hasn’t returned for payment from me yet… Only time will tell)
manapropos@lemmy.basedcount.com 1 year ago
If you’re smart you do it the quick and easy way and leave the company before it bites you in the ass. Only suckers stay with the same company for more than a few years
nogrub@lemmy.world 1 year ago
and thats why we are reading a book about clean code at my apprenticeship
insomniac@sh.itjust.works 1 year ago
This community makes more sense when you realize the majority of users are CS students.
Synthead@lemmy.world 1 year ago
Pointers hard!! LOL
Dave@lemmy.nz 1 year ago
Hey, don’t you group me in with people who have had a small amount of real training!
yiliu@informis.land 1 year ago
I.e. you did use them, but learned the hard way why you shouldn’t.
Very likely OP is a student, or entry-level programmer, and is avoiding them because they were told to, and just haven’t done enough refactoring & debugging or worked on large enough code bases to ‘get’ it yet.
BorgDrone@lemmy.one 1 year ago
I don’t get it either. Why would you ever feel the need for them to begin with?
magic_lobster_party@kbin.social 1 year ago
In software that’s already badly engineered. Either you do the work and refactor everything, or accept it’s probably not worth all the effort.
CapeWearingAeroplane@sopuli.xyz 1 year ago
Unironically: For in-house scripts and toolboxes where I want to set stuff like input directory, output directory etc. for the whole toolbox, and then just run the scripts. There are other easy solutions of course, but this makes it really quick and easy to just run the scripts when I need to.
ZILtoid1991@kbin.social 1 year ago
Depends on what you're doing. Functional programming has its own downsides, especially once you want to write interactive programs, which often depend on global states. Then you either have to rely on atoms, which defeat the purpose of the functional programming, or pass around the program state, which is janly and can be slow.
I personally go multi paradigm. Simpler stuffs are almost functional (did not opt for consting everything due to performance issues), GUI stuff is OOP, etc.
fluxion@lemmy.world 1 year ago
As with the sexual connotation here, the temptation is not rooted in long-term considerations like future maintainability
GTG3000@programming.dev 1 year ago
Well, if you’re writing something the user will be looking at and clicking on, you will probably want to have some sort of state management that is global.
Or if you’re writing something that seems really simple and it’s own thing at first but then SURPRISE it is part of the system and a bunch of other programmers have incorporated it into their stuff and the business analyst is inquiring if you could make it configurable and also add a bunch of functionality.