The problem with null

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 ธ.ค. 2024
  • Using null in code causes uncertainty and doubt, but we can't do without it.
    Right?

ความคิดเห็น •

  • @hejloldetermig3850
    @hejloldetermig3850 2 ปีที่แล้ว +60

    I would much rather prefer just checking whether the value is null. No matter which approach you implement, it's going to get harder to read and harder to maintain.

    • @Ffaarg7
      @Ffaarg7 2 ปีที่แล้ว +3

      It’s more systematic than that. Having to check for nulls probably means the business does not understand it’s domain or that it understands it’s domain but chooses to treat its entities as bags of fields to be queried or mutated. When you think specifically about the domain and the operations happening in the domain then you can make these bulls go away or at the least push them down into the infrastructure layer into a single place. Rest and resource controllers etc have made us think in terms of CRUD.

    • @hejloldetermig3850
      @hejloldetermig3850 2 ปีที่แล้ว +1

      @@Ffaarg7 I think building your infrastructure around dealing with nullable fields is a terrible idea. The author states that "Using null in code causes uncertainty and doubt". I couldn't disagree more with this statement. This comment is a perfect example, that the domain and the operations happening within the domain should not be translated directly into object infrastructure. From a domain- and object oriented perspective, it makes sense to use this "null pattern", but in reality it provides zero business value, as there is already a common consensus on how to use null properties in modern code. It's only going to increase costs of development and confuse any developer coming from the outside.

    • @Ffaarg7
      @Ffaarg7 2 ปีที่แล้ว

      @@hejloldetermig3850 Only if you hold onto the CRUD resource orientated mindset of development.

    • @adrianbrokman3702
      @adrianbrokman3702 ปีที่แล้ว +1

      Everything you are about to develop, except purpose specific algorithms are just CRUD. No matter how many layers of abstractions you will use, you still do CRUD.

  • @Vali615
    @Vali615 2 ปีที่แล้ว +14

    The "we want to show an empty string" part ~@4:05 is the main reason you SHOULD probably use NULL in PHP. Since the "show empty string" part is only the case in your current business flow/context. Maybe you want it to be 0000-00-00 00:00:00, maybe you want it to default to when your system was first online, maybe you don't want to show that "posted at" date if it was not "published" on some UI interfaces. So in that case, NULL could simply mean a value "not currently set". And since PHP has `??= `, it makes it really easy to set defaults.

  • @watamatafoyu
    @watamatafoyu 2 ปีที่แล้ว +5

    So, the alternative is to convert null to 0000-00-00 when running it through a date function? That's insanity. The problem isn't null, it's your chedder code.

  • @CottidaeSEA
    @CottidaeSEA 2 ปีที่แล้ว +11

    This is why optionals were made. To reduce that cognitive load, as then we'll be aware through typing alone that checking the presence of a value is necessary. The issue then is that an optional wrapper object can be null, but at that point it's just the fault of someone writing the code and not faulty logic.

  • @LeTrolli
    @LeTrolli 2 ปีที่แล้ว

    This is a perfect example how to turn a solution into a problem, add useless classes, an even more useless interface and lose the notion that a value may not be set.
    Great job ...

  • @is-sam
    @is-sam 2 ปีที่แล้ว +2

    I'm not convinced of this, I prefer to use null and performs all the checks because they are important and can change from a situation to another.

  • @ekfliu
    @ekfliu 2 ปีที่แล้ว +4

    If you are not checking for null that means it is not suppose to be null. Just because a program is not crashing or throws exception does NOT mean it is correct. Imagine seeing your production database with empty string every where, now good luck trying to figure out where that empty string is coming from in your 50,000+ lines of code with 100 developers. At least with null it would fail fast crash immediately throw a stack and tell which developer did a bad thing. That is much better than writing the supposed correct value of empty string in all your customers account. Good luck trying to restore the prod db from backup or figure out which customer account was screwed.

  • @bulent2435
    @bulent2435 2 ปีที่แล้ว +1

    I don't think null is something we should get rid of.

  • @CareyBaird
    @CareyBaird 2 ปีที่แล้ว +8

    I don't understand how you're saving cognitive load... you still have to think about how to deal with null in your app but now it's an empty string instead.
    And that takes away the flexibility. What if you want to show the null date as "no date" instead of an empty string?
    What if you have a conditional based on the null date?
    Feels like over-engineering to me.

  • @ThePhilosoft
    @ThePhilosoft 2 ปีที่แล้ว +3

    Just couple more steps and we arrive at NullObject / Maybe monad / Optional type

  • @r4ndsen
    @r4ndsen 2 ปีที่แล้ว +2

    the issue with your null objects is that you actively move business logic to them which may not always be desired.
    making the choice where it is needed is far cleaned and also less mental overhead, not more

  • @samuvisser
    @samuvisser 10 หลายเดือนก่อน

    Honestly now that we have optional types with the question mark, I don’t agree you always have to think about null. You can see right in the type definition whether you should check for null, or even if you forget your IDE will remind you. That works fine, really

  • @GGGGGGGGGG96
    @GGGGGGGGGG96 2 ปีที่แล้ว +1

    I don't like this NULL-pattern-approach, it is just a workaround for an invalid data-state. In this example every post should have a created-Date. In the end, the same problem exists for all kind of data, which comes from db. So you also could implement a missing case for everything (to avoid "strtolower expects a string, got null" and so on).

  • @SergejTihonov
    @SergejTihonov 2 ปีที่แล้ว +2

    Thank you for pointing out the null topic. Like to refresh the basics from time to time.
    As you mentioned in the video a good way to handle null is to use strict typing and accept only types you want to have.
    I understand the problem with cognitive load. In my opinion your selected example is too small to justify introduction of several classes. You presenting a solid concept but with the current example you increase the load too much.
    For smaller situations like this I go for a helper method or a service function. Laravel brings also an interesting approach with macros.
    Beside that I use guard classes as a concept to group some of my preconditions at the beginning of a method and can focus after that on my functionality.

  • @jamaicaigot9335
    @jamaicaigot9335 2 ปีที่แล้ว

    i cant find you on audea - can you post audio versions of your videos there? would love to listen to them! thanks again for the great content!

  • @klimenttoshkov
    @klimenttoshkov 2 ปีที่แล้ว +2

    Nice idea for the “null pattern”, thanks. Going to implement it

  • @marcvanderwielen
    @marcvanderwielen 2 ปีที่แล้ว

    How much overhead would we want to introduce? EA-Inspections, PHPStan and proper Unit tests are all we need (as much as I understand the congnitive load issue).

  • @BrunoGomes-su1bk
    @BrunoGomes-su1bk 2 ปีที่แล้ว

    The way you write OOP PHP makes me feel like I've been working on a different language for the last months... Do Anyone know a good free crash course or article or something on this "more complete" OOP PHP?

  • @tanmoykhan2618
    @tanmoykhan2618 2 ปีที่แล้ว

    Thank you for this! Super cool video! A+++

  • @arpansthapit6547
    @arpansthapit6547 2 ปีที่แล้ว +2

    Great video. A video on DDD would be awesome.

  • @tytydraco
    @tytydraco 2 ปีที่แล้ว +1

    What if there are no possible "default" values to use? For example, an object that has to be instantiated right away. Something like a File object.

  • @kion9138
    @kion9138 2 ปีที่แล้ว

    What about Doctrine and DraftPost + PublishedPost? Two tables?

  • @hasanmougharbel8030
    @hasanmougharbel8030 2 ปีที่แล้ว

    Hey there, glad to learn from you.
    SELECT TOP 3 * FROM Table;
    Would the first three selected records in that syntax display the latest records inserted into the table by default?
    So Top 3 records are the newest 3 records inserted into the table.
    Am I right?
    Thanks for taking care of this.

  • @naturemc2
    @naturemc2 2 ปีที่แล้ว

    This remind me of NULLPOINTER exception in Java. The most popular error in Object Oriented programming. Javascript actually make the null an object. Php also treat null a special type. Basically, you can't get rid of. It's itself a placeholder of empty object in memory. That's assembly stuff!!!

  • @Ffaarg7
    @Ffaarg7 2 ปีที่แล้ว

    Thank you. Very interesting. I believe the presence of nulls in domain interfaces is a sign that your domain needs fixing. This is especially true when you have lots of business partners who will be using your services. You try an accommodate them by adding data that belongs outside your domain inside your domain and then litter your domain with branches to take into account all the different use cases which now need to be be identified. If you go fully into ddd then your user interface is never a CRUD interface anyway.

  • @amaelftah
    @amaelftah 2 ปีที่แล้ว +1

    thanks a lot for this cool video ... i didn't think of nulls like that before :D
    not related to tech ... can we know your camera gear & lighting to shoot this great video

  • @greenya84
    @greenya84 2 ปีที่แล้ว +1

    This no problem with null.
    On your example, if a post can have optional publication date (as you said, if post not published, the date is not set), then you should expect it to be possibly null -- this is nothing wrong with that.
    On the other hand, if let say post title can never be null (by design), then you never check if its null -- you just use. In the moment when it get null, the code crashes which will indicate the is a bug (in code, if title suddenly was desided to possibly be null and old code is didnt support it OR bug in data source (like database).
    If you create a server handler, that is totally OK to fail with some http code 500, that it why those codes exists after all. If you create a client part (some app, or website, any UI for end user), you have to try catch large block of code (no need to test each thing with "if" branch) and catch the failure and do what is best at that point. By the way, at this point you also have to decide, what is more important: 1) show only valid data, if you cannot -- ask user to "try latter" or show explanation message -- never misinform user; 2) or you OK to show any data, like if publish date is not valid (null) and you show anything just so you get rid of null in your code :)

  • @ayushyembarwar
    @ayushyembarwar 2 ปีที่แล้ว

    I agree with your point about the cognitive load of always having to think about the null case, it's a constant hindrance

  • @danieltoth-nagy5097
    @danieltoth-nagy5097 2 ปีที่แล้ว

    This is very interesting and I've learned a lot from it!

  • @jacekkowalski1464
    @jacekkowalski1464 2 ปีที่แล้ว +1

    I hate nullable properties (and their respective getters and setters) that are nullable only before validation but after they should not. From business point of view they are no longer nullable after validation but technically (and for static analyzers) they still are.
    For now the only solution that I've found is to write separated model for validation and for my domain logic. But that creates a lot of boilerplate and duplicated code,as the only difference is this "?" removed from the declaration.

  • @smiley2827
    @smiley2827 2 ปีที่แล้ว

    Thank you for sharing your point of view and solution but, I would like to continue checking on null other then creating interfaces and new types 🤷‍♂️

  • @АндрейГалушко-е9о
    @АндрейГалушко-е9о 2 ปีที่แล้ว

    Спасибо

  • @ducodarling
    @ducodarling 2 ปีที่แล้ว

    How are you storing this data?
    Normalizing the (relational?) database, by creating two tables, will fix that. One table for drafts, one for published works. We should remember that there is a truth to data and nothing exists that doesn't exist; it either exists, or it's meta and can be normalized away.

    • @Ffaarg7
      @Ffaarg7 2 ปีที่แล้ว

      I don’t think you need 2 tables. But you could store the publishedAt in another table and avoid the null. And your domain model does not need a 1 to 1 correspondence with your db model.

  • @GonzaloMassa
    @GonzaloMassa 2 ปีที่แล้ว

    What about Booleans? Are you worried if they will be true or false? 😋

  • @krims15
    @krims15 2 ปีที่แล้ว

    In my opinion, null object pattern is outdated to modern languages and adds even more unneeded cognitive complexity. Its creating multiple types of "null" which is not actually null and you are always forced to think in that way. Modern PHP is so much more developer friendly when it comes to working with nulls.

  • @Cobblestoned100
    @Cobblestoned100 2 ปีที่แล้ว

    Seems like this pattern is most useful, when you use a type like PublishedPost alot in your codebase. If you use another type like that only a few times, writing the extra classes seems like more work than the nullchecks.
    As Aleksandr has pointed out, please look into Option monads. It does almost the same abstraction, but is a generic type, that can be used for all situations similar to this :)

  • @TengizAdamashvili
    @TengizAdamashvili 2 ปีที่แล้ว +1

    You came up with a problem and solved it, great job :D

    • @LeTrolli
      @LeTrolli 2 ปีที่แล้ว

      he came up with a solution and turned it into a problem

  • @dragosniculescu6307
    @dragosniculescu6307 2 ปีที่แล้ว

    Is good that null exists! For me null means unassigned (i know it sounds wierd since you can assign null) but is the simplest unassign check that can applied to any type

    • @grabas619
      @grabas619 2 ปีที่แล้ว

      Exactly. Null means the value is undefined and this example feels like dancing around an undefined value than addressing it for what it is

  • @TheEbencarljo
    @TheEbencarljo 2 ปีที่แล้ว

    Enriching content as always Thank you @brent

  • @jean70242
    @jean70242 2 ปีที่แล้ว +1

    How about storing a value other than NULL as the publication date? It would help alot with this mess.

  • @xy2i
    @xy2i 2 ปีที่แล้ว

    Rust does not have null

  • @Max_Jacoby
    @Max_Jacoby 2 ปีที่แล้ว

    Yep, null is a nuisance. In fact Microsoft changed their C# syntax recently so you could enable or disable null support for any type. For example "string" doesn't suppose to contain null but "string?" does. It so much easier now, I wonder why they didn't implement it natively in the first place instead of sugaring syntax.

    • @sohn7767
      @sohn7767 2 ปีที่แล้ว

      Started as a Java clone. Later copied kotlin’s null safety type system

  • @grabas619
    @grabas619 2 ปีที่แล้ว +1

    So instead of a simple null check I'll write multiple classes implementing some interface.
    What if the blog post had a property "updated_by" referencing User class? If the post was not edited by anyone this property should be null, right? So what would you do now? Create a UserNull object for User entity with an interface declaring every User method? This makes no sense, it's really not worth the effort. The boilerplate argument is thrown out of window because you are increasing the codebase substantially, adding unnecessary complexity to your code that your colleagues would really love you for and every change you would want to introduce in an User object you would have to change it in two different classes just because it's too much of a cognitive load to check if the value is defined... not to mention PHP has bunch of useful null operators (even more since PHP 8).
    No thank you.

  • @manuakasam
    @manuakasam 2 ปีที่แล้ว

    How to relax as a developer? Ignore null, let phpstan do it's job, and fix what needs fixing :D

  • @jelmerprins
    @jelmerprins 2 ปีที่แล้ว +2

    Hiding it to the database is something I do a lot in my symphony projects. It's super easy with custom DBAL types

  • @siddharthnahar4186
    @siddharthnahar4186 2 ปีที่แล้ว +2

    watching this at 404 views haha :)

  • @arturoordonez-hernandez8750
    @arturoordonez-hernandez8750 2 ปีที่แล้ว

    One way around writing "if" checks to guard against null reference errors is to write a Utility function that takes in an object and will either return the object itself or an empty string if the input object is null. Of course, this could turn into InvalidFormatExceptions if you're trying to convert or format an empty string, but at least the null exceptions would go away.

  • @ziarv
    @ziarv 2 ปีที่แล้ว

    👏

  • @AlekseyAnokhin
    @AlekseyAnokhin 2 ปีที่แล้ว

    I'm so used to using null's that I just don't bother about those check's, I gues.... But I found interesting the DDD approach, that you've mention

  • @kibbewater
    @kibbewater 2 ปีที่แล้ว +3

    so what all these "programming" channels on youtube is that every programming language is flawed, I'm shit, everyones shit and nobody should ever code. You guys are blowing up non-existant problems to get internet fame, either you are a coder or you're just not. either you're fit for the job or you are not. if simple stuff like this triggers you, then sorry.

    • @EIP674
      @EIP674 2 ปีที่แล้ว +3

      I think the only one triggered here is you 😄 I think trying to reflect critically on stuff you do every day will bring you forward even if you think it may not need thinking about.