Late response but just thought I'd share it anyway in case any new viewers read it: NullObject is really not useful in situations where you read values from that object (like in this case reading CurrentSpeed). Instead, you use it in places where you either only write to it or call methods (like SpeedUp).
In this example, Car should be changed to have the check moved into it by adding a method called IsSpeedExceeding(value) where value is maxSpeedDesired). In such a case, the example will work.
Isn’t there a problem here because NullCar.SpeedUp do nothing carToDrive.CurrentSpeed will always be 0 and (carToDrive.CurrentSpeed < maxSpeedDecired) always be true so we will get an infinite loop – or did I miss something? I know it has nothing with the Null Object pattern to do, but it is a risk that arises due to the way the pattern is used here .
I totally agree with Archangel Chi.. The video shows good techniques, but the pattern couldnt do more bad down the road.
It is WRONG to return null, emtpy objects, strings, whatever. In such case you should always throw an exception. That is what exceptions are for, otherwise you will end up with huge, painful, costly indiscrepancies in your data in the future.
Good video; except it may cause problems later. Mainly if you'd want to actually check if the object is null somewhere in the code. This means you'd have check the instance of or the type of the object - which is expensive.
It's a good pattern, in theory, but you have to be REALLY conscious of where you use it and what you're using it for; otherwise it will be a nightmare.
Also; remember - Singletons need a private constructor so they cannot be instantiated :)
This is a solution in search of a problem and the problem it finds is poor design - the solution it introduces is obfiscated, non-intuitive, "spooky" behavior. The car factory should never return a null (it should throw an exception if it's manufacturing parameters are invalid for its context then the condition can be dealt with at object creation) and it sure shouldn't return a phony object that pretends to be valid but does nothing - that produces unexpected behavior tough to debug later.
Correct me if i'm wrong, but didn't you just create an infinite loop? The while loop seems to never be able to complete if you pass in the NullCar. Otherwise, good explanation on the concept :)
yes you are right, he should have thrown exceptions in the overridden inherited methods in the NullCar class. That way the client also knows it is dealing with a null car when catching exceptions rather than indicating nothing.
Late response but just thought I'd share it anyway in case any new viewers read it: NullObject is really not useful in situations where you read values from that object (like in this case reading CurrentSpeed). Instead, you use it in places where you either only write to it or call methods (like SpeedUp).
In this example, Car should be changed to have the check moved into it by adding a method called IsSpeedExceeding(value) where value is maxSpeedDesired). In such a case, the example will work.
DanielGjorwell 2 months ago
This video is great. It shows exactly why null objects shouldn't be used, thanks :)
yellowblood 10 months ago
nice, but in your example, your while loop is an endless loop :D
when you get your nullcar, speedup() does nothing, so :
while (carToDrive.CurrentSpeed < maxSpeedDesired){ carToDrive.SpeedUp();
}
this is endless, it will never reach maxSpeedDesired
AyuNeko 1 year ago
Isn’t there a problem here because NullCar.SpeedUp do nothing carToDrive.CurrentSpeed will always be 0 and (carToDrive.CurrentSpeed < maxSpeedDecired) always be true so we will get an infinite loop – or did I miss something? I know it has nothing with the Null Object pattern to do, but it is a risk that arises due to the way the pattern is used here .
onesoftdk 1 year ago 5
Hi.. Thanks for the video. Please post more of these..
vinayakaca 1 year ago
I totally agree with Archangel Chi.. The video shows good techniques, but the pattern couldnt do more bad down the road.
It is WRONG to return null, emtpy objects, strings, whatever. In such case you should always throw an exception. That is what exceptions are for, otherwise you will end up with huge, painful, costly indiscrepancies in your data in the future.
hrehe1234 2 years ago
Good video; except it may cause problems later. Mainly if you'd want to actually check if the object is null somewhere in the code. This means you'd have check the instance of or the type of the object - which is expensive.
It's a good pattern, in theory, but you have to be REALLY conscious of where you use it and what you're using it for; otherwise it will be a nightmare.
Also; remember - Singletons need a private constructor so they cannot be instantiated :)
cchmbrln 2 years ago
This is a solution in search of a problem and the problem it finds is poor design - the solution it introduces is obfiscated, non-intuitive, "spooky" behavior. The car factory should never return a null (it should throw an exception if it's manufacturing parameters are invalid for its context then the condition can be dealt with at object creation) and it sure shouldn't return a phony object that pretends to be valid but does nothing - that produces unexpected behavior tough to debug later.
ArchangelChi 2 years ago
Hate his voice xD
62316e 2 years ago
Correct me if i'm wrong, but didn't you just create an infinite loop? The while loop seems to never be able to complete if you pass in the NullCar. Otherwise, good explanation on the concept :)
SouvenanceNet 3 years ago 3
yes you are right, he should have thrown exceptions in the overridden inherited methods in the NullCar class. That way the client also knows it is dealing with a null car when catching exceptions rather than indicating nothing.
mylotundinho 3 years ago
How about improving the image quality
al2suarez 3 years ago
Nice video.. nice way to explain by giving basic example... Thanks.. Can u come up with more deisgn patterns..?
AshikWani2008 3 years ago
you can explain stuff nice way.. try on some more difficult patterns to make dev community life easier. -thx
KPHANIPRASAD 3 years ago
Thanks for the video. I always wondered what the "Null Object" design pattern was. Post more videos! =)
HumbleRat328 3 years ago
Excelent stuff
berezip 4 years ago
Sorry for the poor quality on this -- it's my first attempt at posting a video. I'll see if I can't fix it somehow...
ChocolateForDogs 4 years ago