Timeline for Explanation on how "Tell, Don't Ask" is considered good OO
Current License: CC BY-SA 4.0
19 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Feb 29 at 21:02 | history | edited | DeadMG | CC BY-SA 4.0 |
added 95 characters in body
|
Feb 29 at 20:56 | history | edited | DeadMG | CC BY-SA 4.0 |
added 95 characters in body
|
Feb 29 at 20:28 | history | edited | DeadMG | CC BY-SA 4.0 |
added 95 characters in body
|
Feb 29 at 20:27 | comment | added | DeadMG | Having gained a lot of experience in the last twelve years since I posted this answer, I can now say that the null object has some valid uses and some non-valid uses, so it is up to the developer to decide when it is appropriate. Generally I find that for data, null object is an anti-pattern, but for behaviour, it is more often a valid pattern. | |
Feb 28 at 13:41 | comment | added | Cort Ammon | @KonradRudolph This is true. Which is why I shied away from calling it an anti-pattern and instead said that it can easily be an anti-pattern. There are absolutely times where its useful, and there are times where null gets overused because it's so useful at its job. But applied in the wrong setting, the null object pattern turns sour quick. | |
Feb 28 at 13:32 | comment | added | Konrad Rudolph | @CortAmmon And your example fails because once you need to be able to tell the absence of something you can no longer effectively use a null object (there are some potential exceptions such as NaN/NA and their propagation rules in languages that support this). But for many problems you don’t need to be able to tell the absence of something. | |
Feb 28 at 13:29 | comment | added | Konrad Rudolph |
@CortAmmon The language object null is absolutely not the same as a null object, and the entire point of null objects (where they make sense) is to avoid having special handling for such objects. They are specifically designed such that they can be used in place of regular objects, and have “neutral” semantics (which obviously depends on the domain). If you need special handling for the null object, that’s a design flaw that defeats the purpose of having a null object in the first place.
|
|
Feb 28 at 13:13 | comment | added | Cort Ammon | ...the "empty string," and declare that it is special. If you receive an empty string for an address, it's impossible to tell whether there was no address (null object), or if the address was empty (perhaps the user did not fill it in). And there are absolutely real life situations where this is a great tradeoff, in exchange for not dealing with null pointer exceptions. But apply it religiously, and the story can get difficult quickly. | |
Feb 28 at 13:11 | comment | added | Cort Ammon | @KonradRudolph the Null Object pattern can easily be an anti-pattern because there is an existing solution already in the language: null. And, having language support, it has all sorts of advantages. For instance, it is easy for the Null Object pattern to result in a proliferation of null types, especially if you have a hierarchy of interfaces to support. If you take "tell, don't ask" as bible, it makes a lot of sense. But when you look at it, it takes away the algorithms opportunity to handle this case specially. In the case of strings, it forces us to take a valid string ... | |
Aug 1, 2014 at 6:38 | comment | added | candied_orange | By the way, if you've ever passed something an empty string "" you've used the null object pattern. | |
Aug 1, 2014 at 6:30 | comment | added | candied_orange | @KonradRudlph has a point. DeadMG, how is displaying, 'No street name on file' silent or failing? A null pointer exception would be better? How are you going to recover from that exception? The POINT of the null object pattern is to provide behavior that is constant with the system but rejects the premise of a question. It's the same thing the number 0 does in mathematics. If you hold up a fist and ask, "How many fingers am I holding up?" I use 0 as my way to say, "You aren't holding any fingers up" which is much better than saying, "You're question makes no sense. Please restart" | |
Jul 20, 2012 at 13:26 | comment | added | TMN |
Not sure I see your point with "the client can check_for_underheating without having to change SystemMonitor ". How is the client different from SystemMonitor at that point? Aren't you now dissipating your monitoring logic across multiple classes? I also don't see the problem with a monitor class that provides sensor information to other classes while reserving alarm functions to itself. The boost controller should be controlling boost w/o having to worry about raising an alarm if the temperature gets too high.
|
|
Jul 20, 2012 at 10:20 | comment | added | Max | @DeadMG I agree that the above example is a bad usage of the Null Object pattern, but there is a merit to using it. A few times I've used a 'no-op' implementation of some interface or other to avoid null-checking or having true 'null' permeating the system. | |
Jul 20, 2012 at 8:27 | comment | added | Konrad Rudolph | I’d argue that this entirely depends on the problem domain. | |
Jul 20, 2012 at 7:39 | comment | added | DeadMG | Pretty sure that nobody has any methods which are specified as "Does nothing". That makes calling them kinda pointless. That means that any object implementing Null Object breaks LSP, at the very least, and describes itself as implementing operations that it, in fact, does not. The user expects a value back. Their program's correctness depends on it. You simply bring more problems by pretending that it is a value when it is not. Have you ever tried to debug silently failing methods? It is impossible and nobody should ever have to suffer through that. | |
Jul 20, 2012 at 7:04 | comment | added | Konrad Rudolph | “Null object” isn’t what I’d call an anti-pattern, so I wonder what’s your reason for doing so? | |
Jul 20, 2012 at 1:58 | history | edited | DeadMG | CC BY-SA 3.0 |
added 277 characters in body; added 84 characters in body
|
Jul 20, 2012 at 1:49 | history | edited | DeadMG | CC BY-SA 3.0 |
added 317 characters in body
|
Jul 20, 2012 at 1:42 | history | answered | DeadMG | CC BY-SA 3.0 |