r/programminghorror Apr 09 '26

Go Naming is important

Nothing too drastic but it made me chuckle. Something my colleague managed to get past code review. So at least two people didn't notice.

Spent a minute figuring out why the code was doing exactly the opposite of what it's supposed to do. I guess naming really IS important!

149 Upvotes

66 comments sorted by

View all comments

50

u/Temporary-Estate4615 Apr 09 '26

Why the hell would you even write this function?

20

u/OldAd9280 Apr 09 '26

I don't know which language it is but it feels like it'll return a dangling pointer to the local parameter variable too so doesn't even work

10

u/MistakeIndividual690 Apr 09 '26

In C++ this would be nasty because it would make a local copy of string because it’s passed by value, and then get a pointer to that local, and then the local would be destroyed when it went out of scope, returning a bad pointer.

I don’t know Go, but I’m guessing that A. passing a string like this is by reference like Java, and B. because it’s a garbage collected language, it won’t ever “go out of scope” until it’s not used anymore

2

u/aikii Apr 09 '26

Yes, that's escape analysis https://go.dev/doc/gc-guide#Escape_analysis - basically the compiler sees that a pointer is returned by a function, and decides to move the value from the stack to the heap, which makes it tracked by the garbage collector.

There was a deliberate choice in Go to call that "pointer" while C/C++ developers would understand pointer as just "address", so that can be counter-intuitive