3 ms·
private and protected are fantastic for narrowing down bugs, and controlling access. Certainly not a waste of time. In comparison, a looser language like javas
by axod 17y ago
private and protected are fantastic for narrowing down bugs, and controlling access.
Certainly not a waste of time. In comparison, a looser language like javascript where any code can call any code any time it likes, can be a complete pain to debug.
- jrockway 17y agoIt can call any code any time it likes, but it shouldn't. Why do you think you know what the Future User of your class wants to do better than that Future User? In theory, a good library will have a perfect public interface that anticipates every need that the future users will have. "protected" and "private" eliminate many classes of bugs in that case. But in the real world, they just eliminate many classes of features, because they prevent people from using imperfect libraries.
- axod 17y agoI hardly ever use external libraries so luckily don't have that problem, but I don't think it's a widespread issue :/ If you really want access to a private I'm sure you could work out a way. >> "It can call any code any time it likes, but it shouldn't" Sure but it's nice to have a lock on the door that says "I can say with 100% certainty that this code will not be called from anywhere else". That's a pretty useful feature to have.
- jrockway 17y agoNice for you, but not nice for anyone else.
- chromatic 17y agoThat's only a nice feature until you pull out the Decorator or Adapter patterns to get around some class marked `final` in a library somewhere you can't modify and for which you have to appease the type system because classes do not imply interfaces.