this post was submitted on 08 Oct 2023
1078 points (96.6% liked)

Programmer Humor

18970 readers
406 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
 
top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 144 points 10 months ago (7 children)

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.

[–] [email protected] 85 points 10 months ago (4 children)

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.

[–] [email protected] 44 points 10 months ago (1 children)

Not if you leave the project soon enough. It's like tech debt chicken.

[–] [email protected] 17 points 10 months ago (2 children)

Then, at your new job, you see garbage code and wonder what dumbass would put global variables everywhere

load more comments (2 replies)
[–] [email protected] 20 points 10 months ago (1 children)

amen

Plus, there's always the temptation to do it the shitty way and "fix it later"

double amen

[–] [email protected] 21 points 10 months ago (4 children)
[–] [email protected] 22 points 10 months ago (1 children)

In a 10 year old commit from someone who's left the company 5 years ago.

[–] [email protected] 8 points 10 months ago* (last edited 10 months ago) (3 children)

Bruh. I fixed software from the 90's.

Scientific software too. Which is way weirder.

😀

load more comments (3 replies)
load more comments (3 replies)
[–] [email protected] 8 points 10 months ago (4 children)

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)

[–] [email protected] 7 points 10 months ago (3 children)

For me most of the people who have written our most annoying tech debt left the company long time ago.

load more comments (3 replies)
load more comments (3 replies)
load more comments (1 replies)
[–] insomniac 60 points 10 months ago (2 children)

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

[–] [email protected] 13 points 10 months ago

Pointers hard!! LOL

[–] [email protected] 10 points 10 months ago

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

[–] [email protected] 26 points 10 months 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.

[–] [email protected] 19 points 10 months ago (6 children)

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)
load more comments (3 replies)
[–] [email protected] 87 points 10 months ago (9 children)

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

[–] [email protected] 48 points 10 months ago

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

[–] [email protected] 40 points 10 months ago (1 children)

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

[–] [email protected] 38 points 10 months ago (1 children)

As a hobby coder: "mmmhm, mmmm, mmhm... I know some of these words!"

[–] [email protected] 37 points 10 months ago* (last edited 10 months ago) (1 children)

Software dev is full of obscure keywords that describe otherwise pretty simple or basic concepts you stumble upon in practice naturally and that you probably already understand.

  • singleton: a class/object that is designed to be single use, i.e. only ever instantiated with a single instance. Typically used when you use class/objects more for flow control or to represent the state of the program itself, rather than using it to represent data
  • immutable: read-only, i.e. unchangeable
  • dependency injection: basically when you pass a function or object into another function object, thereby extending their effective functionality, typically for modular code and to separate concerns.

Here's one more of my favourite examples of such a keyword: memoization

[–] [email protected] 7 points 10 months ago

Ahh yes memoization, the complicated way to say "remember this, I might need it again"

[–] [email protected] 22 points 10 months ago (1 children)

Important to contain all your mess to one side of the room, makes it easier to manage

load more comments (1 replies)
[–] [email protected] 13 points 10 months ago

So you saying, just the tip?

[–] xmunk 10 points 10 months ago* (last edited 10 months ago) (2 children)

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

[–] [email protected] 8 points 10 months 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.

load more comments (1 replies)
load more comments (4 replies)
[–] [email protected] 47 points 10 months ago (3 children)

Obligatory, mutable global variables are evil.

[–] [email protected] 32 points 10 months ago (2 children)

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

[–] [email protected] 15 points 10 months ago (2 children)

There's no ISO standardized definition for variable. People use that word with all kinds of meaning.

[–] [email protected] 18 points 10 months ago (1 children)

Waaaait a minute... isn't it called a variable because the contents are, you know, variable?

load more comments (1 replies)
[–] [email protected] 11 points 10 months ago (3 children)

This is needlessly obtuse. The definition of the word is that it's non-constant. There isn't an ISO definition of the word no, but there are many reputable dictionaries out there that will serve as an alternative.

load more comments (3 replies)
load more comments (1 replies)
[–] [email protected] 10 points 10 months ago (2 children)

As opposed to immutable variables

*confused screaming*

load more comments (2 replies)
load more comments (1 replies)
[–] fsxylo 28 points 10 months ago

Singletons:

Me: O_O;

[–] [email protected] 28 points 10 months ago (4 children)

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.

load more comments (4 replies)
[–] [email protected] 22 points 10 months ago (1 children)

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

[–] darcy 12 points 10 months ago (1 children)

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

load more comments (1 replies)
[–] [email protected] 20 points 10 months ago (2 children)

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. 🤪

[–] [email protected] 17 points 10 months ago (3 children)

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?

[–] [email protected] 17 points 10 months ago* (last edited 10 months ago) (2 children)

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.

[–] [email protected] 23 points 10 months ago (1 children)

Ah I misread I thought it was specifically a programming course. I can expect this from a math prof.

[–] [email protected] 8 points 10 months ago* (last edited 10 months ago)

Yes, it was a course on finite deformation material models. And no, you do really, really not want to declare each and every variable in your material subroutine globally for the whole finite element program.

[–] [email protected] 8 points 10 months ago* (last edited 10 months ago) (2 children)

Lecturers at universities tend to have little to no industry experience at all.

load more comments (2 replies)
load more comments (2 replies)
[–] winterayars 19 points 10 months ago (1 children)

"But what if I put the whole program into a class and then made it a class wide variable?"

[–] [email protected] 7 points 10 months ago

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

[–] [email protected] 14 points 10 months 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.

[–] [email protected] 13 points 10 months ago* (last edited 10 months ago)

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

[–] [email protected] 8 points 10 months ago

Me putting everything in 'public:'

[–] [email protected] 7 points 10 months ago

Exhibitionist Devs be like.

load more comments
view more: next ›