922
top 50 comments
sorted by: hot top controversial new old
[-] UndercoverUlrikHD@programming.dev 123 points 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.

[-] insomniac@sh.itjust.works 59 points 1 year ago

This community makes more sense when you realize the majority of users are CS students.

[-] Synthead@lemmy.world 13 points 1 year ago

Pointers hard!! LOL

[-] Dave@lemmy.nz 10 points 1 year ago

Hey, don't you group me in with people who have had a small amount of real training!

[-] yiliu@informis.land 24 points 1 year ago

They've given me too many headaches...

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 19 points 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.

I don’t get it either. Why would you ever feel the need for them to begin with?

load more comments (6 replies)
[-] fluxion@lemmy.world 2 points 11 months ago

As with the sexual connotation here, the temptation is not rooted in long-term considerations like future maintainability

[-] ZILtoid1991@kbin.social 2 points 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.

load more comments (6 replies)
[-] idunnololz@lemmy.world 74 points 1 year ago

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

[-] Techmaster@lemm.ee 48 points 1 year ago

Just call it "state management" and nobody will even care.

[-] magic_lobster_party@kbin.social 35 points 1 year ago

Not necessarily a bad practice if the singleton is immutable, and it’s provided via dependency injection for example.

load more comments (2 replies)
[-] mdk_@lemmy.world 13 points 1 year ago

So you saying, just the tip?

[-] xmunk@sh.itjust.works 9 points 1 year ago* (last edited 1 year ago)

Real enterprise programmers know that everything should be on the stack... so they declare a List《void*》 in main.

[-] idunnololz@lemmy.world 8 points 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.

[-] manapropos@lemmy.basedcount.com 7 points 1 year ago

Real enterprise programmers know you can get another job in the next year or two so fuck best practice

[-] z3rOR0ne@lemmy.ml 2 points 1 year ago

Ah yes. Global Objects, AKA the thinnest of condoms, lol.

load more comments (3 replies)
[-] SquishyPandaDev@yiffit.net 32 points 1 year ago

Obligatory, mutable global variables are evil.

[-] magic_lobster_party@kbin.social 24 points 1 year ago

The definition of a variable is that it’s mutable. If it’s immutable it’s constant.

[-] Walnut356@programming.dev 5 points 1 year ago* (last edited 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 describes that the pointer points 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? (E.g. x is an array that's passed in, and we're just checking if element y exists)

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.

[-] QuazarOmega@lemy.lol 9 points 1 year ago

As opposed to immutable variables

*confused screaming*

[-] yiliu@informis.land 5 points 1 year ago
[-] Eufalconimorph@discuss.tchncs.de 3 points 1 year ago* (last edited 1 year ago)
int const golden = 1.618;
int* non_constant = (int*)&golden;
golden = 1.61803399;

Casts are totally not a danger that should require a comment explaining safety…

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.

[-] KittyCat@lemmy.world 28 points 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 4 points 1 year ago

Is that you... Windows 95?

[-] hansl@lemmy.world 4 points 1 year ago

The ONE TRUE CONSTANT; even with an infinite universe, the value is the same in all of them.

[-] Fungah@lemmy.world 2 points 1 year ago

Are the legends really true?

[-] Fungah@lemmy.world 2 points 1 year ago

But not sand memory. It's coars ena drogjh and irritating and gets everywhere.

[-] fsxylo@sh.itjust.works 27 points 1 year ago

Singletons:

Me: O_O;

[-] alphacyberranger@lemmy.world 22 points 1 year ago

Is it an orgy if multiple global variables are used in a multi threaded code?

[-] darcy@sh.itjust.works 12 points 1 year ago

not if everyone uses a Mutex. stay (thread) safe

[-] noli@programming.dev 3 points 1 year ago

Always practice safe stacks

[-] winterayars@sh.itjust.works 18 points 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 7 points 1 year ago

Sounds like the piece of legacy software I have do deal with.

[-] stephfinitely@lemmy.world 14 points 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.

[-] fbmac@lemmy.fbmac.net 13 points 1 year ago* (last edited 1 year ago)

I asked stable diffusion for a photo-realistic version of this image. This isn't what I had in mind

[-] dylanTheDeveloper@lemmy.world 8 points 1 year ago

Me putting everything in 'public:'

[-] dingleberry@discuss.tchncs.de 7 points 1 year ago

Exhibitionist Devs be like.

[-] BilboBargains@lemmy.world 6 points 1 year ago* (last edited 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.

[-] pewgar_kbin@fedia.io 6 points 1 year ago

the meme is spreading

[-] Blackmist@feddit.uk 4 points 1 year ago

Nothing wrong with global variables.

If anyone asks just say it's the singleton pattern.

[-] gandalf_der_12te@feddit.de 3 points 1 year ago
[-] ipkpjersi@lemmy.ml 2 points 1 year ago

Nowadays they call it global state lol

load more comments
view more: next ›
this post was submitted on 08 Oct 2023
922 points (96.4% liked)

Programmer Humor

19315 readers
94 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS