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

Programmer Humor

19503 readers
1269 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
 
you are viewing a single comment's thread
view the rest of the comments
[–] lowleveldata@programming.dev 17 points 7 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?

[–] NightAuthor@lemmy.world 14 points 7 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

[–] lowleveldata@programming.dev 4 points 7 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

[–] owen@lemmy.ca 7 points 7 months ago

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

[–] CodexArcanum@lemmy.world 8 points 7 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).