10 Matching Annotations
- Jun 2023
-
stackoverflow.com stackoverflow.com
-
Conversely, I've never in 16+ years of professional development regretted marking a method protected instead of private for reasons related to API safety
-
I just wanted to tweak Java's BufferedReader to handle custom line delimiters. Thanks to private fields I have to clone the entire class rather than simply extending it and overriding readLine().
-
I'm not saying never mark methods private. I'm saying the better rule of thumb is to "make methods protected unless there's a good reason not to".
-
Marking methods protected by default is a mitigation for one of the major issues in modern SW development: failure of imagination.
-
Been disappointed, surprised or hurt by a library etc. that was overly permissive in it's extensibility? I have not.
-
The old wisdom "mark it private unless you have a good reason not to" made sense in days when it was written, before open source dominated the developer library space and VCS/dependency mgmt. became hyper collaborative thanks to Github, Maven, etc. Back then there was also money to be made by constraining the way(s) in which a library could be utilized. I spent probably the first 8 or 9 years of my career strictly adhering to this "best practice". Today, I believe it to be bad advice. Sometimes there's a reasonable argument to mark a method private, or a class final but it's exceedingly rare, and even then it's probably not improving anything.
Tags
- good point
- never say never
- surprising
- member visibility: protected vs. private
- rule of thumb
- can't predict the future
- what does this actually mean?
- extensibility
- not:
- disappointing
- reasonable defaults
- bad advice
- member visibility
- failure of imagination
- member visibility: make it protected unless you have a good reason not to
- learned from real-world experience
- annotation meta: may need new tag
- member visibility: make it public/protected by default so others can override/extend as needed
- member visibility: make it private unless you have a good reason not to
- software development
- please elaborate
- you can't know for sure
Annotators
URL
-
-
stackoverflow.com stackoverflow.com
-
Are protected members/fields really that bad? No. They are way, way worse. As soon as a member is more accessible than private, you are making guarantees to other classes about how that member will behave. Since a field is totally uncontrolled, putting it "out in the wild" opens your class and classes that inherit from or interact with your class to higher bug risk. There is no way to know when a field changes, no way to control who or what changes it. If now, or at some point in the future, any of your code ever depends on a field some certain value, you now have to add validity checks and fallback logic in case it's not the expected value - every place you use it. That's a huge amount of wasted effort when you could've just made it a damn property instead ;) The best way to share information with deriving classes is the read-only property: protected object MyProperty { get; } If you absolutely have to make it read/write, don't. If you really, really have to make it read-write, rethink your design. If you still need it to be read-write, apologize to your colleagues and don't do it again :) A lot of developers believe - and will tell you - that this is overly strict. And it's true that you can get by just fine without being this strict. But taking this approach will help you go from just getting by to remarkably robust software. You'll spend far less time fixing bugs.
In other words, make the member variable itself private, but can be abstracted (and access provided) via public methods/properties
-
As soon as you make a member not-private, you are stuck with it, forever and ever. It's your public interface now.
-
-
www.typescriptlang.org www.typescriptlang.org
-
Derived classes need to follow their base class contracts, but may choose to expose a subtype of base class with more capabilities. This includes making protected members public:
-
Member Visibility
-