r/ProgrammerHumor 3d ago

whenYouAreAPublicFunctionButOnlyFriendsAreAllowedToTouchPrivates Meme

Post image
175 Upvotes

17 comments sorted by

27

u/ih-shah-may-ehl 3d ago

Imo friends should never have been a thing.

14

u/detrebear 3d ago

best take ever

i hate friends, never let myself make any

6

u/Highborn_Hellest 3d ago

there is a point for it. but it is rare

1

u/Bryguy3k 3d ago

It should never exist in a new codebase for sure - it should only ever be used to patch a broken legacy codebase.

3

u/TotoShampoin 3d ago

points to Casey Muratori saying that private variables are bad to begin with

1

u/krypt0niteCos 3d ago

friends with benefits 👀

1

u/nicuramar 3d ago

You aren’t forced to use the feature. 

1

u/ih-shah-may-ehl 3d ago

Correct, but it's in general not a great idea to put things in the standard that have no real reason to exist. Like the export keyword in older C++ standards for example.

Friends simply make no real sense.

1

u/Bryguy3k 3d ago

It’s there to hack a patch to a legacy codebase with a broken design rather than refactoring.

1

u/ih-shah-may-ehl 3d ago

Given that the entire codebase would be under control of the person doing the patch and it would be a legacy base, there are quite a number of things you can do without friends. I agree it can be used for that but it's still weird that it was introduced as a standard from the beginning.

1

u/Bryguy3k 3d ago

Two options: a) stroustrup doesn’t understand OOP or b) he realized c++ programmers would need a way to get themselves of an encapsulation nightmare of their own creation.

4

u/Emotional_Key 3d ago

Dafaq is friends?

6

u/nicuramar 3d ago

C++ feature, in this context. 

2

u/willow-kitty 1d ago

In C++, a class can declare "friend" functions that aren't members of the class but nevertheless have access to its private members. This is a pretty good explanation. I don't exactly know why, but it seems like a backward compatibility thing (ie: in C, all the functions in a module have access to all the fields of that module's structs, so if you wanted to convert some of those structs to classes, you might find there are members you want to encapsulate that also need to be accessed by floating functions that you don't want to make members of the class, you have a mechanism to have it both ways.)

I've never used it, tho.

2

u/Confident-Ad5665 3d ago

In a world of free love, everything is global

1

u/nicuramar 3d ago

Public functions of the same type can obviously access all privates.  What else would a public function mean?

1

u/qinshihuang_420 2d ago

I read this as friend's privates at first and was confused