Null Object Pattern - Design Patterns

Поделиться
HTML-код
  • Опубликовано: 24 окт 2024

Комментарии • 115

  • @WebDevSimplified
    @WebDevSimplified  5 лет назад +32

    Don't forget to checkout the GitHub repository for extra examples and explanations of all the code in this video. github.com/WebDevSimplified/Design-Patterns

    • @321sas
      @321sas 2 года назад

      Me: Okk... but I’m not gonna

  • @AlexTechie
    @AlexTechie 5 лет назад +25

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

    • @WebDevSimplified
      @WebDevSimplified  5 лет назад +10

      I'm glad you liked it. I love design patterns so I will be making lots more videos as long as you all still enjoy them.

  • @moonshelter9137
    @moonshelter9137 4 года назад +8

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

  • @naruto5437
    @naruto5437 4 года назад +1

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

  • @burreifort
    @burreifort 2 года назад +13

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

    • @raves_r3177
      @raves_r3177 10 месяцев назад +1

      separation of concerns. you should not initialize anything inside the user class. you can, but it is not a good/clean practice.

  • @NoahNobody
    @NoahNobody 3 года назад +2

    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.

  • @GuRuGeorge03
    @GuRuGeorge03 3 года назад +2

    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

    • @pedroamaralcouto
      @pedroamaralcouto 2 года назад

      I think GuestUser as being a NullUser would be a good idea. The Guest user is a user that doesn't have a username, an account, a session, etc. It's empty.
      But making it the Administrator doesn't make sense, and it's very risky. The administrator user is not like a NullUser. It has data about a user. It's not empty. And it has access to functions that other users shouldn't access. And many times when you make an abstract class instead of an interface, you might be calling for trouble.
      (added)
      Instead of making classes for different user roles and inheriting code, think about how, for instance, UNIX and several CMS do it: use roles or groups to determine what a user can do. For instance: administrators, editors, publishers, readers, etc. For each role, you have a list of available function. Relate users and roles, like a relational database. By the way, that makes it possible to configure the roles and the user roles in a DB.

  • @Forshen
    @Forshen Год назад +3

    I know this is a old video. But here is an easier way
    user.find(user => user.id === id) ?? new User(-1, 'Guest')
    Now the function still returns the same type.

  • @pedroamaralcouto
    @pedroamaralcouto 2 года назад

    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.

  • @blank-vw2sb
    @blank-vw2sb 3 года назад +1

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

  • @sangaamosi2681
    @sangaamosi2681 2 года назад

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

  • @fadimoussa8382
    @fadimoussa8382 3 года назад +25

    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).

    • @dareknight2008
      @dareknight2008 2 года назад +5

      May be the getNullUser can be a static function inside the User class itself.

    • @unique_god
      @unique_god 2 года назад +1

      we should use singleton to return the same object reference

    • @thatsalot3577
      @thatsalot3577 2 года назад

      @@unique_god why not just use a simple object literal 😑 with a name, id and hasAccess() function.

  • @bohdanartemenko
    @bohdanartemenko 4 года назад +2

    Thank you, your teaching skill is amazing

  • @agustinmatthey2338
    @agustinmatthey2338 5 лет назад +6

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

    • @WebDevSimplified
      @WebDevSimplified  5 лет назад +1

      Thanks!

    •  4 года назад +1

      It's almost as if he is simplifying web dev....😲

  • @Lordnoashi
    @Lordnoashi 5 лет назад +2

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

    • @WebDevSimplified
      @WebDevSimplified  5 лет назад +2

      I plan to make more. I have been busy with other ideas but plan to keep this series going.

  • @randymartin9040
    @randymartin9040 Месяц назад

    Very very good explanation thank you!

  • @usmaness
    @usmaness 5 лет назад

    Please, keep this series going!

    • @WebDevSimplified
      @WebDevSimplified  5 лет назад +1

      Thanks. It has been on a bit of a break recently, but I do want to try and revive it sometime. I need to just dive into it and get started again.

  • @gurudatt-shahane
    @gurudatt-shahane Год назад

    Really amazing explanation, thank you very much 🙏

  • @SonGoku-lc1sb
    @SonGoku-lc1sb 2 года назад

    Nice explaination. I guess you forgot to save your edited code before running it. :D

  • @julianatlas5172
    @julianatlas5172 4 года назад +4

    I like the idea but don't you have to maintain 2 copies of basically the same class now? if you make a change to User you have to do it to NullUser also

    • @deansprivatearchive
      @deansprivatearchive 3 года назад +1

      Yeah there should be a way to do this with extends somehow in getUser

  • @parvindersingh3641
    @parvindersingh3641 Год назад

    When we use the find function and the id is not present in the array we will get the undefined instead of null.
    const user = users.find(user => user.id === id)
    for example, in the above case if we try to find the is which is not present in the array we will get the value undefined.

  • @RobertWildling
    @RobertWildling 4 года назад +3

    Is there a specific reason that you so not user the triple equal operator? Double equals perform type conversion... You'd actually would have to check against "undefined", not "null".

    • @jmalkvideos
      @jmalkvideos 3 года назад

      I believe “value == null” is a way of checking if “value” is null or undefined. It’s using type coercion to make the code shorter. Some people prefer that brevity, others prefer to be explicit and say “value === null || value === undefined”. The result is the same either way.

  • @quangthanh090990
    @quangthanh090990 4 года назад

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

  • @Victor_Marius
    @Victor_Marius 3 года назад +1

    Wouldn't be easier to have default arguments in the User constructor and if user not found just return new User()? Or to have an object linked to the User class and use as User.Null?

  • @penggao2615
    @penggao2615 2 года назад

    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

  • @ArunKumar-yd8qi
    @ArunKumar-yd8qi 4 года назад +1

    Nice explanation

  • @eddiejaoude
    @eddiejaoude 5 лет назад +1

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

    • @WebDevSimplified
      @WebDevSimplified  5 лет назад +1

      I use the adapter pattern all the time. Anytime I want to consume an external library or API being able to wrap it makes your code so much safer in the case of future changes with the library or API. It also makes it much easier to swap libraries later which is usually a huge pain.

    • @muhammadumair7861
      @muhammadumair7861 4 года назад +1

      @@WebDevSimplified Hey Kyle , great stuff . Please do make a video on Adapter Pattern too with the example you mention.It will be really helpful :)

    • @1LifeStudio
      @1LifeStudio 2 года назад

      @@WebDevSimplified Please do make video on adapter pattern also bro. It will really help. Thanks for such content.

  • @giorgiobarchiesi5003
    @giorgiobarchiesi5003 Год назад

    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…

  • @kareng9484
    @kareng9484 2 года назад

    great explanation

  • @doniaelfouly4142
    @doniaelfouly4142 2 года назад

    Awesome explanation, thanks 🙏

  • @deansprivatearchive
    @deansprivatearchive 3 года назад +1

    I'm thinking about you saying don't use else and here you're using it

  • @RawBert
    @RawBert 3 года назад

    note it is not common to prefix null objects by null, this is because in other languages this wouldn’t work, a function that returns a null obj is more common

  • @avanishkumar1689
    @avanishkumar1689 Год назад

    good explaination

  • @barakatosalon
    @barakatosalon 5 лет назад +3

    Very good video, I was wondering why not try to return new user(-1,´Guest´) directly avoiding to add an extra class !

    • @WebDevSimplified
      @WebDevSimplified  5 лет назад +4

      The purpose of this class is to have a predictable user object that is always the same. If you use your method you need to ensure everytime you want a null user you return that exact constructor which is hard to ensure. It also allows you to check if the user is a null user by having the class

    • @barakatosalon
      @barakatosalon 5 лет назад

      @@WebDevSimplified thank you very much for the feedback !

  • @paulbilbao4833
    @paulbilbao4833 3 года назад

    So well explained !! Thanks

  • @Sankaritarina89
    @Sankaritarina89 2 года назад

    Hey love your channel, keep it up!

  • @serispeaks
    @serispeaks Год назад

    Bro this is awesome!!

  • @lazymacs2823
    @lazymacs2823 3 года назад

    Why not just assign a default value to each property of the object in the constructor instead of creating a new null class? I'm just curious.

  • @hugofilipeseleiro
    @hugofilipeseleiro 4 года назад +1

    Thank you !!!

  • @mohammadebrahim4038
    @mohammadebrahim4038 2 года назад

    thanx 4 the video. but what if im using C#.. what would be the return type of function getUser?

  • @shaynerushton214
    @shaynerushton214 2 года назад

    If you can create a default constructor that uses "Guest" if no user was passed, why not do that? CS student asking a legit question. I could be missing a bunch because I am still very new. So why would we not use a default constructor in place of a Null design pattern?

  • @sergeisychov9810
    @sergeisychov9810 Год назад

    Thanks!

  • @choohanye9314
    @choohanye9314 2 года назад

    Correct me if I am wrong, but seems like it did not solve the null check required for name? (shown in 3:51) If we have a user with null name this will still cause an error. If it is impossible to have such a user, then this condition check was not required in the first place and the Null Object Pattern did not actually help on this.
    Great vid otherwise though!

    • @pedroamaralcouto
      @pedroamaralcouto 2 года назад

      It's possible to add a setter that makes sure the "name" is never null.
      Anyway, the database table might have constraints, like a NOT NULL for the name.
      In principle, the name should not be changed when outside a form submission. You'd only need to check if the name is null in the validation, before creating a user or updating it.
      And the name of the NullUser is "Guest": 7:29

  • @wilhelmschilling3105
    @wilhelmschilling3105 4 года назад +1

    thank you!

  • @alibozkurt-i5r
    @alibozkurt-i5r 11 месяцев назад

    thanks

  • @amjadshadid90
    @amjadshadid90 3 года назад

    thanks a lot

  • @andriitynok3897
    @andriitynok3897 4 года назад

    I don't understand why you couldn't just create a separate user with id -1 and name guest and use it in getUser function whenever user isn't found in users array

  • @oida10000
    @oida10000 3 года назад

    Would it be adivisible to make NullUser a subclass of User to having to write less code, just calling super with my default values and being able to use it things like an array of users or will this bring more trouble than it is worth?

  • @bashardlaleh2110
    @bashardlaleh2110 3 года назад

    cool thanks

  • @aryankumar87771
    @aryankumar87771 2 года назад

    is there a video on Factory Design Pattern ?

  • @moroofadeyemi3042
    @moroofadeyemi3042 2 года назад

    Why did we use the double equal check instead of the triple equal check?
    Also the (typeof user) is returning undefined and not null

    • @asiandurian11
      @asiandurian11 2 года назад

      == checks if the truth values are equal.
      null has the same truth value as undefined i.e. (null == undefined).
      === checks if the truth values are equal and if the data types are equal.
      null is a distinct data type from undefined i.e. (null !=== undefined).
      (obj == undefined) is the same as (obj == null) but the latter is more common.
      These are both identical to (obj === undefined || obj === null), but this looks messy.
      In this scenario, (user === undefined) would work just as well as (user == null) but not (user === undefined), since it returns an undefined as you said.
      However, this is bad practice, because a function like this could return null, and therefore (user === undefined) would return false when user is null.
      We don't care about the distinction of undefined and null in this case, so we can easily check if it is either by using (user == null).

  • @asgharibraheem8938
    @asgharibraheem8938 2 года назад

    I have question here, is there code repeatability rule violated here because of null check class again repeated with other values

  • @kavinduvindikasomadasa352
    @kavinduvindikasomadasa352 3 года назад

    can't we have default params inside the User constructor instead of managing another class ?

  • @explorercoder
    @explorercoder 7 месяцев назад

    find returns undefined if it does not finds a match, then why are you checking for null

  • @rabbyhossain6150
    @rabbyhossain6150 2 года назад

    Since, you have two maintain 2 different classes for the smae Object. Is there any better way to handle this situation?

  • @Vibestr
    @Vibestr 4 года назад

    Doesnt null have to be set in order for a variable to equal null

  • @keshavpahari4156
    @keshavpahari4156 3 года назад

    Could make video on custom react component based custom library like material ui

  • @fadiliabdeljalil770
    @fadiliabdeljalil770 4 года назад +1

    Thank you very much, kind sir!

  • @aakashuniyal6806
    @aakashuniyal6806 3 года назад

    We could also just put default values for your variables and have your User class be responsible to return a user with default values , not seeing how this simplifies things

  • @chinthanagunasekara7469
    @chinthanagunasekara7469 2 года назад

    in here getuser function return two different objects (user and NullUser). insted of creating seperate class what is we can use different constructor to set default values

  • @pradeepmaurya5780
    @pradeepmaurya5780 3 месяца назад

    For printUser(3), getUser() function isn't returning null. In my case, it's returning undefined. Did anyone else found this issue ?

  • @Paul-ve1gp
    @Paul-ve1gp 2 года назад

    on line 40, why do you need to check if the user has the method hasAccess itself?

  • @JoeWong81
    @JoeWong81 5 лет назад

    THANKS BRO

  • @parasarora5869
    @parasarora5869 5 лет назад

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

  • @VishalSharma-rn7mt
    @VishalSharma-rn7mt 8 месяцев назад

    Awesome

  • @Sebaaeza
    @Sebaaeza 5 лет назад

    After implementing null object pattern, can't you just check if (!user) return new NullUser() ? Since find() return undefined if it doesn't find

  • @germanduterte7110
    @germanduterte7110 4 года назад

    I Wish i know design pattern before

  • @kat7275
    @kat7275 5 лет назад +1

    good stuff!

  • @luchopinilla7171
    @luchopinilla7171 11 месяцев назад

    is this the same pattern as "Naked objects pattern"?

  • @Admiral_Ducky
    @Admiral_Ducky 4 года назад

    I'm blown away by quality and clarity of your tutorials! Sir, I bet you IQ is 195 or higher!

    • @sppanday_
      @sppanday_ 4 года назад +2

      and yours might be 0 or lower

    • @Admiral_Ducky
      @Admiral_Ducky 4 года назад

      @@sppanday_ I complimented him in my comment. But you asshole thought I mocked him, because you live in a place that everyone treat you like shit and no one gives you any attention so you think everybody is like those who live with you in that shithole that you call home. I hate your guts, and I hope I never hear from you asshole.

    • @Admiral_Ducky
      @Admiral_Ducky 4 года назад

      @@sppanday_ no surprise india is a stinking shithole!

    • @sppanday_
      @sppanday_ 4 года назад +1

      @@Admiral_Ducky And, no surprise that you are a fucking idiot.

  • @antwanwimberly1729
    @antwanwimberly1729 11 месяцев назад

    USA 🇺🇸

  • @rolikaseventysix
    @rolikaseventysix 5 лет назад

    Downvotes? Who and why...

    • @mrgees100peas
      @mrgees100peas 5 лет назад +2

      When these users signed in they received a "you are not allowed" message

  • @ChrisFotosMusic
    @ChrisFotosMusic 4 года назад

    man idk if theres any money in web development :/ kyle is an expert but he still cant afford doorknobs for his closet doors

  • @abhisheksah1867
    @abhisheksah1867 Год назад

    just use TS XD

  • @ahmedboutaraa8771
    @ahmedboutaraa8771 3 года назад

    notNull comment

  • @ahmedboutaraa8771
    @ahmedboutaraa8771 3 года назад

    ///

  • @mahdiwebdevelopment2338
    @mahdiwebdevelopment2338 4 года назад +1

    This pattern is really unnecessary.