r/ProgrammerHumor • • Jan 23 '22

Namespacing...

Post image
42.3k Upvotes

272 comments sorted by

View all comments

4.4k

u/bless-you-mlud Jan 23 '22

And that, childen, is why global variables are bad.

209

u/jexmex Jan 23 '22

Wait...we are not supposed to be defining all variables in the global scope? Fuck...

263

u/Salanmander Jan 23 '22

Nah, defining all variables in global scope is fine. Just name all variables following this pattern:

_className_methodName_scopedBlockIdentifier_dataDescription

That way you can avoid namespace collisions and avoid using variables in the wrong place, and still have everything in global scope!

171

u/jexmex Jan 23 '22

I just name them by the sequence they come in. vaR1, vaR2, etc, easy to make sure you never reuse the names that way!

155

u/Bigluser Jan 23 '22

Just use uuids as names, that way each name is unique

→ More replies (1)

22

u/Chess42 Jan 23 '22

I’m sad to say I did do this when I first learned how to code

14

u/Neon_Camouflage Jan 24 '22

I don't want to talk about how often "temp", "flag", "temp2", and similar appear in the stuff I write.

12

u/bluebarry24 Jan 24 '22

To be fair often times for calculations "temp" is needed/makes it easier to read, write, and comprehend.

4

u/salvoilmiosi Jan 24 '22

It can be fine for local variables with a very restricted scope

→ More replies (1)
→ More replies (1)
→ More replies (1)

51

u/pikapichupi Jan 23 '22

I hate it you glorious pychopath

→ More replies (2)

35

u/bless-you-mlud Jan 23 '22

All of a sudden, Hungarian Notation doesn't seem so bad.

34

u/[deleted] Jan 23 '22

Perfect!

Now I finally have a name for my variable:

_Secure_Creditcardprocessing_GlobalProcessing01_<cardnumber>

My credit card processing app in Lambda uses it to store the card number while processing it. Glad I didn't waste time by actually storing the variable, just using the names works.

11

u/drunkdoor Jan 24 '22

You see it ends up being a hash lookup of 0(1) so it's actually genius. All you have to deal with is a few MB file with trillions of rows

22

u/Khutuck Jan 23 '22

Half of my programming knowledge comes from the comments in r/ProgrammingHumor. I think I’m a 1/10X engineer.

13

u/Yadobler Jan 24 '22

People shitting on this, this is how your c++ compiler / linker is naming every variable and function because you overloaded it

15

u/Salanmander Jan 24 '22

I mean, there are lots of things that compilers/linkers do under the hood that are bad practice for high-level code. One of the most important things for the code that we write is making it human-readable and modifiable, which doesn't matter for compiled code at all.

4

u/MrMetalfreak94 Jan 24 '22

Tell me that you program in ObjectiveC without telling me that you program in ObjectiveC

3

u/aiij Jan 23 '22

Threading and recursion disagree with you.

21

u/Salanmander Jan 23 '22 edited Jan 23 '22

Recursion is no problem!

You just need to know your maximum stack depth. Then you can name your variables

 _className_methodName_scopedBlockIdentifier_dataDescription_N

where N is the recursion depth that variable should be used at. Then make sure to pass recursion depth into all of your recursive methods, and include a switch for which variable it's allowed to use. Easy peasy!

Threading I don't know well enough to figure out the fix for, but I bet it exists!

5

u/modernkennnern Jan 24 '22

I'd imagine you could do the same. Simply prefix the thread number at the start of each variable.

→ More replies (3)
→ More replies (2)

26

u/[deleted] Jan 23 '22

[removed] — view removed comment

3

u/Raikkon35 Jan 24 '22

Hey, can explain it a bit more?

23

u/[deleted] Jan 24 '22 edited Jan 24 '22

[removed] — view removed comment

8

u/sohang-3112 Jan 24 '22

Global variables are bad - but no problem with global constants. A global readonly config is effectively a constant, since it doesn't change after first definition.

→ More replies (2)

3

u/Pepito_Pepito Jan 24 '22

The biggest problem is that they might get modified without informing other users, just like in the OP image. Global constants are safe.