4 ms·
This essay happens to be specific case about "null" but the more generalized concept is the optimal design of any public-vs-private state and how it affects the
by jasode 8y ago
This essay happens to be specific case about "null" but the more generalized concept is the optimal design of any public-vs-private state and how it affects the balance of code at the client site vs embedded inside the class. Unfortunately, the author's email example isn't a good case study of the principle he's trying to demonstrate.
If you expose unnecessary state, your class is acting like a "dumb data struct" and you make client code more complicated and it proliferates needless and error-prone code duplication. (I made a previous comment about this.[1])
On the hand, if you hide information that makes sense for client code to know about, you increasingly make fatter and heavier class code with extraneous class methods that's idiosyncratic to an isolated piece of client code rather than being of general utility for many clients. Arguably in this case of "missing email", it may make more sense for the client code to handle an "if (customer.email == null) SendPostalMail()".
You have to determine whether you can reasonably hide knowledge in a class such that client code (including all future client code) never needs it. E.g. if some future code needs to behave differently such as creating a different type of web login screen depending on the existence of email address, it probably doesn't make sense to fatten up Customer class with another method such as "CreateWebLogin()" to generate HTML -- all to avoid exposing the null status of email address. This would be a code smell.
[1] https://news.ycombinator.com/item?id=11481194 https://news.ycombinator.com/item?id=11481194
- Serow225 8y agoGood thoughts! Do you have a blog? :)
- emsy 8y agoI totally agree, it basically is one of the few instances in programming where you really can't get around needing experience. At the time of writing the majority of top level comments were arguing against the pattern, but it's useful to at least know about alternatives instead of just declaring them as bad.
- Serow225 8y agoAgreed, in my experience this is the real difference between mediocre/OK/excellent programmers; the ability to choose the "right" level of encapsulation and abstraction at design time. It's definitely not always correlated with experience, although finding someone relatively junior on the good+ side of the scale is a real find because as you said the school of hard knocks tends to be the best teacher in this realm. IMHO Most anyone can learn to program at a reasonable level, but not everyone can do design well; see bootcamp graduates(and also "ten years of the same 1year of experience)…