5 ms·
Just the idea of writing getters and setters that don't actually do anything at all, except to allow access to a 'private' variable, makes me want to tear my ha
by Shorel 4y ago
Just the idea of writing getters and setters that don't actually do anything at all, except to allow access to a 'private' variable, makes me want to tear my hair.
It's boilerplate for the sake of boilerplate.
- marginalia_nu 4y agoMaybe 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.
- ivanche 4y agoExactly! And the solution is don't write getters and setters unless you are absolutely forced to do so, and even then only for the minimum amount of fields.