3 ms·
Maybe I'm not seeing it, but if you want public access to a private variable, why not set it to public? Just saying. I code primarily in Java, and I can't real
by marginalia_nu 4y ago
Maybe I'm not seeing it, but if you want public access to a private variable, why not set it to public? Just saying.
I code primarily in Java, and I can't really say the last thing I wrote a getter or a setter, except possibly because I wanted a functional reference.
Accessor methods is part of a style of code Java that was unfashionably outdated 10 years ago.
- OkayPhysicist 4y agoThe language design failure that get/set methods are used to monkey patch around is the lack of computed properties. If you have an object with properties A and B, but later realize that B can be calculated from A, it would be nice to eliminate the class of "Inconsistent A and B Values" bugs from your software, by making B calculated from the value of A. The problem is, without computed properties, that involves replacing the Object.B expressions with Object.getB() expressions, breaking your API. So Java developers got in the habit of putting getters and setters on everything.
- marginalia_nu 4y agoI don't think that rationale makes a lick of sense in code written today. Besides the obvious violation of the YAGNI-principle and stinky mixture of data and logic, even if this scenario arises (which it very rarely does) any Java IDE worth its salt can replace a variable access with a method with a few mouse clicks. Now, in 1995, maybe...
- ivanche 4y agoThat is very broad generalization. I have a habit of never writing them unless I'm forced to do it.