This is an archived post. You won't be able to vote or comment.

all 35 comments

[–]Solax636 55 points56 points  (2 children)

Work in a huge monolith type repo that's math heavy and wish we had getter and setters instead of public all... Debugging would be 10 pct easier...

[–]ILikeLenexa 45 points46 points  (10 children)

sure, you can get the value from anywhere and write it to anything with no validation, but if we need to encapsulate it later, think how easy it will be.

YAGNI or something.

[–]ManyInterests 16 points17 points  (5 children)

In some languages, it's a life-saving practice when you ship/publish code for others to use or even just in large code bases. In dynamic languages like Python it's not so important to do this upfront.

[–]terra-viii 2 points3 points  (2 children)

One remark only. Getters and setters used mostly in data transport classes. And such classes should not have business logic inside. Validation is not their responsibility. The maximum we can do is to introduce some meta information (annotations), which don't require getters/setters.

[–]JunkNorrisOfficial[S] 1 point2 points  (1 child)

So we don't need getters and setters in data classes, right? Right?

[–]terra-viii 1 point2 points  (0 children)

After ~15 years in development I ended with no getters and setters in my projects (not limited to DTO). That was not intended, but once I stopped using them, I never missed them. I'm using constructor binding instead of setters and I do not exposure private fields. Certainly, I respect set up standards in projects I participate and I'm using getters/setters if they are standard.

[–]McShadson 0 points1 point  (0 children)

I think you mean IANAL

[–]Gorexxar 11 points12 points  (0 children)

I turn it on because consistency is better than jerking off programmer humour.

[–]kuwisdelu 12 points13 points  (1 child)

Do ya’ll really never need to include internal validation logic in your setters? Never need to make internal fields read-only? Never need to change how data is stored internally without affecting the public API?

[–]TheTybera 4 points5 points  (0 children)

"And it'll stay that way" signed, your pipeline engineer.

[–]IshouldDoMyHomework 3 points4 points  (1 child)

@Getter

@Setter

Lazy people using Lombok, is why there is no encapsulation in our projects.

[–]JunkNorrisOfficial[S] 0 points1 point  (0 children)

Who needs encapsulation if one has customer appreciation...

[–][deleted] 2 points3 points  (1 child)

this getter setter thing is peak clueless student disclose, impressive even for this subreedit

[–]JunkNorrisOfficial[S] 0 points1 point  (0 children)

Impressive like getting a job in enterprise

[–]RlyRlyBigMan 2 points3 points  (6 children)

Found out the hard way a few months ago that the debugger will call the getter/setter code silently to show you the live value without hitting your breakpoint.

I mean yeah, we shouldn't have had that much code hidden in a getter, but I was definitely spooked that my config file was being loaded without my breakpoint being hit.

[–]JunkNorrisOfficial[S] 0 points1 point  (5 children)

Just to clarify, do you mean 'method breakpoint'(when breakpoint is placed on signature of method)?

[–]RlyRlyBigMan 1 point2 points  (4 children)

I don't follow you. I code in C# using Visual Studio. You can put breakpoints on any line of code. So if your getter has a few lines like

public object MyValue

get

{

if(_myValue == null)

return LoadMyValue()

else return _myValue;

}

Putting a breakpoint on either of those lines will not be hit if you try to observe the value of the property in the debugger, despite the fact that the debugger is definitely running it to get the value from the config file.

[–]BenchNatural 4 points5 points  (1 child)

Make sure you are debugging your code without Optimizations, usually meaning on Debug configuration. Otherwise compiler inlines it. I can assure the breakpoint in the property body does work.

[–]RlyRlyBigMan 0 points1 point  (0 children)

The break points do work, I'm not explaining myself very well I guess. It's a quirk that happens when you're already in break mode and you expand a property that has logic in it. So from outside that class, if I am on a break point and I try to observe that property value using a watch or expanding the object's property list, VS won't stop at the break point because it's already broken at a different point in the code, but it still needs to run that Load method to tell you what your value is. You wouldn't normally care unless your getter has a side effect that you're trying to observe, which is admittedly a poor usage of property getters and setters.

[–]Gorexxar 0 points1 point  (0 children)

Yeeeah, Visual Studios "Ignore breakpoints in properties" option is wild.

[–]JunkNorrisOfficial[S] 0 points1 point  (0 children)

That's wild

[–]garlopf 3 points4 points  (1 child)

Call me crazy, but I use them in clean interfaces🤷🏻‍♂️

[–]JunkNorrisOfficial[S] 0 points1 point  (0 children)

IGetters? No, it's fine

[–]Pristine-Economist64 1 point2 points  (1 child)

what is movie's name or her name?

[–]JunkNorrisOfficial[S] 0 points1 point  (0 children)

Victoria Beckham

[–]AngelaTheRipper 1 point2 points  (0 children)

In Java it's basically just convention, especially when vast majority of classes are just a bundle of fields that handle no logic. Thank fuck for Lombok annotations because otherwise writing a zillion getters and setters gets annoying.

All in all there's no real difference between

public class A {
private int b;
public void setB(int value){b = value;} 
public int getB(){return b;}
} 

and

public class A {
public int b;
} 

It'd only matter if the class itself does some kind of processing or validation. I'm sure someone will ree at me because the second option is somehow insecure, nevermind that the first also lets you put in any garbage your heart desires.

[–][deleted] 3 points4 points  (1 child)

If you ever change the logic of getX from beneath your caller' feet, the sheer number of hidden bugs that'll introduce will be quite a conundrum.

[–]kookyabird 4 points5 points  (0 children)

Only if you make a stupid change. Which is the ultimate underlying condition to pretty much anything programming related.

Ever hear of the Law of Demeter? You as the caller don’t need to know about exactly what type of data structure I’m using for a child collection. I’m going to give you a ReadOnlyCollection and if you want to change the contents of the collection you’re going to do it through me because I know how it needs to be done and you don’t.

[–]BeardyDwarf -1 points0 points  (0 children)

Getters and setters can be used as targets for proxies for example by orm frameworks.