this post was submitted on 21 Mar 2024
620 points (94.9% liked)

Programmer Humor

19197 readers
1104 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
 
all 20 comments
sorted by: hot top controversial new old
[–] [email protected] 17 points 6 months ago (2 children)

What's the point of having friends when the whole point of private fields is to ensure that you don't break other parts when changing those?

[–] [email protected] 9 points 6 months ago

There's infinite ways to organize code. In C# or Rust where this isn't an option, you might use nested classes or traits hidden behind a module/namespace.

Good use cases are data structures with associated helper classes. For example, a collection/tree and an iterator/tree-walker for working with elements of the collection. Or for something like a smart memory allocator (an arena or slab allocator), you might use a friend-class to wrap elements returned from the allocator, representing their connection back to it (for freeing up when done or to manage the allocation structure in ie a heap or sorted tree).

[–] [email protected] 15 points 6 months ago (1 children)

It’s just another option, don’t gotta use it. Maybe you find yourself needing something like this, and the only other choice is making it public. At least with friend classes, you know which classes are friends so you can go look for any dependencies

[–] [email protected] 4 points 6 months ago (1 children)

It’s just another option, don’t gotta use it

It's not a choice of mine when I'm trying to read through / modify some legacy code base

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

Meh, that already comes with infinite problems, so what's one more?

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

Did I misunderstand something or is that an InCel-joke?

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

Did I misunderstand something or is that an c++InCel-joke?

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

That would be incel++ instead

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

Does C++ actually have something like that? That sounds like something made up for the joke?

[–] [email protected] 38 points 6 months ago (1 children)
[–] [email protected] 2 points 6 months ago* (last edited 6 months ago) (1 children)

Huh, do Java and other oop languages have them too and what are some good reasons to use them?

[–] [email protected] 3 points 6 months ago (1 children)

It allows for more fine grained access control and to implement afterthoughts.

Think having some private function that can break things if called improperly, but also allow you to avoid significant overhead when calling it the correct way. For example you could be avoiding input validation in a public wrapper for that function. If your friendly class already does it, or cannot produce invalid inputs, there is no need for that.

You could also implement logging after the fact, because your friendly logger object to read private members.

Arguably it's a questionable design decision tho, as you could do all of this in other ways and it basically breaks any guarantees private would usually give you.

[–] [email protected] 1 points 6 months ago

That was a problem I saw with it but I guess it's useful too. I like structs.

[–] [email protected] 162 points 6 months ago

"Oh, so we're actually not friends" Walks away, as she should

[–] [email protected] 54 points 6 months ago* (last edited 6 months ago) (2 children)

Ohhh gottem!

She is now legally obligated to sex this man.

edit: programmers code

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

lmao 'sex this man'. hahahhaha

[–] [email protected] 2 points 6 months ago

Yup, that'd be the joke

[–] [email protected] 25 points 6 months ago

Normies hate this trick!