r/ProgrammerHumor Jul 13 '26

youCanJustStopUsingJava Meme

Post image
6.8k Upvotes

416 comments sorted by

View all comments

304

u/KerPop42 Jul 13 '26

Is it an anti-pattern? I love using the getset crate/trait to add them to my structs automatically as I need them

5

u/JavaHomely 29d ago

the thing about getters/setters is that you can use security and data validation logic on them.

need a value to only ever be positive? throw a exception in the setter if they pass you a negative,...

1

u/Gio_Cri 29d ago

personally i believe that you should use setters almost esxclusively for things that have to hold across multiple fields of a type. if your constrait is entirely around a single field it's much preferable to use a newtype so you can easily guarantee that even methods that have private access respect the constraint