this post was submitted on 04 Nov 2023
91 points (85.8% liked)

Programming

17540 readers
82 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 1 year ago (4 children)

The curse of OOP (java style...).

I mean why do you need to write getter and setter methods. I have wondered at the beginning of university 10 years ago, and am still wondering why you would need something like that...

[–] [email protected] 7 points 1 year ago (1 children)

You let your ide generate simple getters and setters or utilize something that generates them during a compilation process.

Who ever writes them per hand needs to utilize their tooling better or needs better tooling.

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

Yeah but why do I have to use an IDE to generate getters and setters in the first place? It just adds up to more mental overhead, because my brain has to process this boilerplate somehow, even if my IDE can generate it (I know it's simple code, but it's even simpler to not have that boilerplate code at all).

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

Access control and offering a sound interface.

You don't need getters and setters if every attribute is public, but you might want to make sure attributes are accessed in a specific way or a change to an object has to trigger something, or the change has to wait until the object is done with something. Java just has tools to enforce a user of your objects to access its attributes through the methods you designed for that. It's a safeguard against unintended side effects, to only open up inner workings of a class as littles as necessary.

In a language without something like private attributes you'd have to account for far more ways someone might mutate the state of objects created by your code, it opens you up to far more possible mistakes.

[–] [email protected] 1 points 1 year ago (1 children)

I'm totally aware of the benefits of encapsulation, but the way java does it seems so unnecessarily boilerplatey (C# is better, functional programming makes encapsulation even simpler, but that's a different paradigm...)

I like how Rust approaches this via the module system and crates (you have pub for the public interface, pub(crate) for crate/lib wide access and no modifier for being only allowed to access in the current module and submodules of that module)

[–] [email protected] 2 points 1 year ago (1 children)

Not to aware of how c# works, or interested in defending java, especially ancient java versions, but what does it do better in that regard?

Only records for more or less pure data objects come to mind, but those are also in modern Java.

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

And it's so good that Kotlin adopted them too in their journey to fix Java.

load more comments (3 replies)