144 points

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.

permalink
report
reply
85 points

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.

permalink
report
parent
reply
44 points

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

permalink
report
parent
reply
16 points

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

permalink
report
parent
reply
20 points

amen

Plus, there’s always the temptation to do it the shitty way and “fix it later”

double amen

permalink
report
parent
reply
21 points

// TODO: Fix later

permalink
report
parent
reply
8 points

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)

permalink
report
parent
reply
7 points

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

permalink
report
parent
reply
4 points

What industry do you work in?

permalink
report
parent
reply
3 points

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

permalink
report
parent
reply
1 point

and thats why we are reading a book about clean code at my apprenticeship

permalink
report
parent
reply
59 points

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

permalink
report
parent
reply
13 points

Pointers hard!! LOL

permalink
report
parent
reply
10 points

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

permalink
report
parent
reply
26 points

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.

permalink
report
parent
reply
19 points

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?

permalink
report
parent
reply
1 point

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.

permalink
report
parent
reply
5 points

But those would be constants, not variables.

permalink
report
parent
reply
1 point

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.

permalink
report
parent
reply
2 points

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.

permalink
report
parent
reply
2 points

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

permalink
report
parent
reply
1 point
*

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.

I also had to work with a system where configurations for user space were done as libraries setting global constants. And then we changed it so everything had to be hastily redone so that suddenly every client didn’t have the same config.

permalink
report
parent
reply
86 points

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

permalink
report
reply
48 points

Just call it “state management” and nobody will even care.

permalink
report
parent
reply
40 points

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

permalink
report
parent
reply
38 points

As a hobby coder: “mmmhm, mmmm, mmhm… I know some of these words!”

permalink
report
parent
reply
37 points
*

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

permalink
report
parent
reply
22 points

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

permalink
report
parent
reply
2 points

Yeah yeah let’s put all the eggs in one basket

permalink
report
parent
reply
13 points

So you saying, just the tip?

permalink
report
parent
reply
9 points
*

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

permalink
report
parent
reply
8 points

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.

permalink
report
parent
reply
7 points

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

permalink
report
parent
reply
2 points

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

permalink
report
parent
reply
1 point
*
Deleted by creator
permalink
report
parent
reply
3 points

That’s not immutable nor a singleton

permalink
report
parent
reply
4 points

That makes it inherently multithread compatible!

permalink
report
parent
reply
47 points

Obligatory, mutable global variables are evil.

permalink
report
reply
32 points

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

permalink
report
parent
reply
15 points

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

permalink
report
parent
reply
18 points

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

permalink
report
parent
reply
11 points

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.

permalink
report
parent
reply
5 points
*

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.

permalink
report
parent
reply
10 points

As opposed to immutable variables

*confused screaming*

permalink
report
parent
reply
5 points

Or mutable constants…

permalink
report
parent
reply
3 points
*
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…

permalink
report
parent
reply
2 points

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.

permalink
report
parent
reply
28 points

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.

permalink
report
reply
4 points

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

permalink
report
parent
reply
2 points

Are the legends really true?

permalink
report
parent
reply
4 points

Is that you… Windows 95?

permalink
report
parent
reply
2 points

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

permalink
report
parent
reply
27 points

Singletons:

Me: O_O;

permalink
report
reply

Programmer Humor

!programmer_humor@programming.dev

Create post

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

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics

Community stats

  • 7.2K

    Monthly active users

  • 954

    Posts

  • 36K

    Comments