3 ms·
And why use a RNG, when you can just return 3? http://xkcd.com/221/ http://xkcd.com/221/ The reason to use accessors is encapsulation. If the public field is p
by supermatt 13y ago
And why use a RNG, when you can just return 3? http://xkcd.com/221/ http://xkcd.com/221/
The reason to use accessors is encapsulation. If the public field is private to your package/application then by all means access it directly, but if you are exposing it publicly then expect your application to be broken by third parties.
By providing accessors, you are communicating to a developer a strict mechanism for interacting with your library/application.
What if myString (in your example) needed to have bounds or be formatted in a specific way (validation)? What if it does in the future?
Without strict application flow control enforced by accessors (i.e. allowing your objects fields to be hijacked at any time, from anywhere), you cannot be sure that your application will be consistent.
- simonh 13y agoBut how do we know that the 3 is random? Maybe you have some hidden three-ist agenda? I'm much more inclined to trust Randal on something like this than some random guy on Hacker News! Then again since you're some random guy....