Null Object Pattern - Design Patterns

preview_player
Показать описание
Design patterns are one of the easiest and quickest ways to create clean manageable code whether you are starting from scratch or refactoring existing code. One of the easiest to implement design patterns is the Null Object Pattern. This pattern is all about handling the null keyword in a way that removes all of those nasty if (object == null) checks from your code. It also makes handling default values for null objects a breeze.
If you want to learn more about the Null Object Pattern make sure to checkout the repository linked below.

Design Pattern Playlist:

Design Pattern Repository:

Twitter:

GitHub:

CodePen:

#DesignPatterns #CleanCode #Programming
Рекомендации по теме
Комментарии
Автор

Thanks, I'm really glad that you did a follow up video to the original! Looking forward to more design pattern videos. 👍

AlexTechie
Автор

one of my fav channel ... you have knack of explaining a complex topic really well

naruto
Автор

I don't understand why you have just 500 likes, you explain so well! Keep it up!

moonshelter
Автор

Thanks a lot, this information was very important for me. I need more videos about different design patterns from you.

cumar
Автор

Thank you, your teaching skill is amazing

bohdanartemenko
Автор

Instead of the NullObject, you can initialize the properties with default values.

burreifort
Автор

Wow that's a nice idea!!
I'm a self taught dev.
Helping me code better!

blank-vwsb
Автор

Great videos. I learned another way to reduce checking null. many thanks. Quang from Viet Nam

quangthanh
Автор

Great video! You have a really nice and clear way of explaining things :)

agustinmatthey
Автор

Really amazing explanation, thank you very much 🙏

gurudatt-shahane
Автор

thanks kyle. There is a special place in heaven for people kinda you !

sangaamosi
Автор

That's a much cleaner way of implementing it. Currently, when I make a class, I check for the null or empty possibilities and assign them a default as soon as possible.

NoahNobody
Автор

Sometimes, if the returned object doesn't implement an interface, adding if guard clauses inside the "class" might be more convenient than making another "class". For instance, in C++ I have wrappers for the SDL2 library. When a property is null, the methods that would access it return immediately without null checks outside the "class".

7:58 Coding peeve: redundant "if ... else" clauses. The "return" ends the function execution.

Some might it makes the flow explicit, but I think it makes the code less readable and, like in a UML Use Case diagrams, I like to think the most important part of the code is the main success scenario. You look at the end of the code, before the "if ... return" to understand the main goal. You look at the "if ... return" to understand the exceptions and alternate flows. With that "if ... else" you're not making a distinction between the code with the main goal and other code.

The "if ... else ..." makes it look like the code inside the "if" scope is the most common branch (it's the first thing we read before the "else", "else" sounds like an afterthought and compilers and machines assume the code inside an "if" is more commonly executed). Or, at least, visually, they're at the same level.

PedroCouto
Автор

thanks for the great video😄 ... it is amazing👌

parasarora
Автор

Hi,


can you please create more video about design patterns please ?
These are very well explained with great examples.

Lordnoashi
Автор

To improve this even further, e.g. for a really large project the getUser function should be inside of some repository type class where all users are stored and the User and NullUser could be a Administrator and Guest class that both inherit from a superclass User (abstract class is probably best for this useCase). that way all functionality that both types must provide can be defined in the user class and anything specific to administrator or Guest can be done inside the respective classes :-) another way (if u hate inheritance) is to use interfaces and dependancy injection

GuRuGeorge
Автор

Hi Kyle, really nice video. I am a student who is learning web design now, I learned a lot from your tutorials. Btw I think I heard League of Legend background sound 8:39 from this video XD, do you play it too Lol

penggao
Автор

Great pattern and great explanation! My other favourite is the adapter pattern :)

eddiejaoude
Автор

It would probably be better to have a getNullUser function that returns a normal object with null values than to have to maintain yet another class when modifying the normal class (maybe as a part of the class itself).

fadimoussa
Автор

Thanks for the video! To add my five cents, I would derive class NullUser from class User. So the same pattern could be used in a more type-safe language, like typescript, for instance. But I wonder if this would violate the Lyskov substitution principle…

giorgiobarchiesi