Checking for null without checking for null in C#

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

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

  • @SavageDoesSomething
    @SavageDoesSomething 2 года назад +39

    Good morning Nick, it's everybody. Just checked the comments for null and returned true, submitting this ticket as a big fix right now

    • @johannesh7610
      @johannesh7610 2 года назад +14

      Greatest version of "first" I've ever seen

    • @alexclark6777
      @alexclark6777 2 года назад +7

      If you like these types of comments, and you want to see more...

  • @8-P
    @8-P 2 года назад +4

    I would love to see an in depth video about csprojand nuget packaging :)

  • @cyril113
    @cyril113 2 года назад +2

    This is great combined with NRT. A built in option to enforce non nullable types at runtime would be awesome!

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

    That's really how I wanted NRT to work out of the box. I am still not sure why they could not add a compiler flag, e.g. strict-enable, that does this.

  • @antonmartyniuk
    @antonmartyniuk 2 года назад +8

    I like nullable enabled, always use it in the new projects with combination with errors

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

      @Stysner It makes your code look much tidier, by not making you check against null values at all. And since you have control over your code and know how it is supposed to work and what to return, it's also less error prone.

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

    Cool! And I've also learned about assembly weaving.

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

    I think nullable reference types should have an additional option to add the null reference checks to the beginning of every method, unless an argument is nullable.
    For people that implement guards, we use it everywhere, and it's the exception when we don't. That's why the !! Operator was bad, because we would have had to litter our codebase with it.

  • @dharanish.
    @dharanish. 2 года назад

    Your channel is a great reference

  • @Sebastian----
    @Sebastian---- 2 года назад

    Nice. Fody helped me so much ;)

  •  2 года назад +9

    Fody is awesome, but it also might be confusing magic for some.

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

      Expanding our knowledge is always good. It is a very simply but powerful introduction to assembly weaving

  • @cn-ml
    @cn-ml 2 года назад +1

    Its an interesting approach, but i prefer nullable because it is just so elegant. It allows you to write your code in a way that null exceptions never happen so there is no need to check for null at all (which also removes guard statements from your code)

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

      The problem is that nullable alone can still have null values even if they are not nullable so keep in mind

  • @endofunk2174
    @endofunk2174 2 года назад +17

    Personally I prefer a functional approach to this -- essentially a much more explicit management of nulls with a monadic sum type like Maybe (Option), Result / Try, Either, ...
    Explicit and / or unmanaged returns of a null in a codebase is IMO a code smell; and I similarly would categorize the more terse !! operator as part of that problematic camp. Also in context of this video; I similarly don't find the NullGuard project to be a significant improvement over either the default state or the previously proposed !! operator.
    In addition I rather prefer the notion of earlier returns, as in, for example: Swift's guard statement (inverted if clause) to accommodate earlier throws... similarly the used of Kotlin's Elvis operator and C# ?? operator for a similar purpose to Swift's guard statement.

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

    Currently fixing 1000ths of warnings in our codebase of which most are null reference related (nullable reference types enabled ofcourse)

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

    I think Source Generator feature would be really useful in this scenario

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

    In C# system intern structs have a instance without being created. Does anyone know how I can make this with my own structs. I don't find any documentation about it

  • @doublebass120
    @doublebass120 2 года назад +24

    Great video, great library. I've used Fody's INotifyPropertyChanged library and it was magical.
    However, I can't help but wonder if there are any performance impacts whatsoever (e.g. using reflection at runtime to check for the NotNull attribute), or is this essentially a source generator that writes the same type of null checking that you would have written manually.

    • @nickchapsas
      @nickchapsas  2 года назад +27

      There is no reflection. It actually emits code in compile time and it acts as if you had the code in place. It’s very performant

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

      @@nickchapsas What code editor are you using? I don't recognize it.

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

      @@AdamsTaiwan It's called JetBrains Rider

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

    Hey Nick, how to create account on your website to get the course? There is only login btn. :(

  • @rafaelurrea2385
    @rafaelurrea2385 2 года назад +6

    "null... nullable.... NULL ABLE"

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

    I rather have nullable reference types than use a magical code weaver in this case. Why? Because having and using nulls should be a language level feature, a feature that is static, can be expressed in code and is expected, explicit. What if someone doesn't know that you have that weaver in your codebase? Good library, but seems like an effort made before something more standardized could be developed and included in the platform.

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

      This works on top of NRTs. It’s not one or the other. NRTs doesn’t mean that a non nullable type can’t ever be null. It’s complimentary rather than a replacement

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

      @@nickchapsas I see. So it basically throws null reference exceptions for you when you forget to check for nulls, even if you use NRTs. Seems so redundant to me. I still stand by my words before. It should be a language level feature to guarantee non-nullability, a static compile-time feature, not a runtime check. Alas, this is it seems the only viable current solution. But I still dislike having to handle exceptions that *** should not happen *** in the first place. 😩

  • @bscamba
    @bscamba 2 года назад +2

    When you say about turning the nullables into errors do you mean just enabling both nullable and treat warnings as errors in the csproj or is there another way?

    • @nickchapsas
      @nickchapsas  2 года назад +2

      Turning NRTs on and treating that specific warning as error yeah

    • @LeMustache
      @LeMustache 2 года назад +7

      You can treat all nullable related warning as errors by adding this to your project file.
      Nullable

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

      Usualy I create a Directory.Build.props file with CS8600;CS8602;CS8603;CS8604;CS8625;CS8618;CS8620;CS0219
      the props file is used for every project in the folder

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

      @@lucasteles42 Any reason for specifying the warnings explicitly?

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

      @@MiningForPies You missed my point but that's fine. I figured out the answer to my question by myself :)

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

    I just have nullable always on so this and that banbang was useless...

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

      It’s actually not. Even if you have nullable on, you can still get nulls in those values because NRTs are just a compilation thing, not a runtime thing, and it’s not guaranteed

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

    You don't need to pass nameof() to .ThrowIfNull() btw

    • @bass-tones
      @bass-tones 2 года назад +1

      Yep. It uses [CallerArgumentExpression] behind the scenes, which is also a useful thing to know about independently.

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

      Good point, I forgot it was using the caller argument expression internally

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

    Does "From Zero to Hero" consist of two components (DI and unit-testing) only?

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

      It’s the “Essentials bundle” that only has the two together because I consider them essential. The minimal APIs one is separate because it’s situational

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

      @@nickchapsas Thanks.

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

    Keep in mind that fody is not free and expects you to be a patron

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

      Although you can still use it for free, expect no help from them (even your PRs will get rejected) if you're not a patron.
      It sucks really.

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

      I mention it in the video

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

      @@guiorgy well I’m not using the project 😂

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

    Cannot imagine situation in which normal null check could become a problem. But instead, using such 'approaches' could make typical developer's life a nightmare. It's kind ..hey, why we are still using a door to get into a car? 21 century, lets use a window for that.
    One more...program writing is not a painting making. Your programs must be readable and understandable longer then a few months.

  • @darioabbece3948
    @darioabbece3948 2 года назад +2

    Checking for null without checking for null. So it has come the full circle

  • @tcl78
    @tcl78 2 года назад +2

    Honestly, adding Nuget packages for every little thing is, IMO, a bad programming habit that should not be encouraged because it might cause problems if not today sometime in the future.
    I'd much prefer to have the bang-bang operator straight into the language itself than adding a thousand Nuget packages in my project that might stop working or become not supported in the near or far future.
    In my 30 years career as a software developer (i begun programming in BASIC on an MSX, before DOS became largely used on home computers) i saw way too many "cool things" (or even whole programming languages) just disappear or being abandoned or superseded by the next cool thing and i know how many troubles this can cause when trying to improve, update or fix an old software that uses such packages/features.
    In my opinion you should ONLY use well established, well known, largely adopted and well supported packages/components and even then, do that sparingly. Every time you add a dependency you risk to find yourself in 5 or 10 years to have to update an old software you made (and barely remember) and find that the cool little package you have used is not available anymore for the new version of your IDE, language or framework... and, sometimes, you don't even remember what it was used for and what was its purpose.

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

      Fody is a established set of projects with a very well known developer behind them. The platform has 80 million downloads behind it

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

    It would be perfect if this doesn't create any additional files in project. I don't really mind this xml file, but if we accept that this is fine for a library to have such xml files, then we will be drown in useless configurations laying everywhere in a project and begin ultimately garbage.

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

    IL weaving feels like a dirty thing to do...

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

      The biggest problem with it is that it’s not extremely obvious so people looking at the code or debugging it might get really confused

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

    Sadly fody expects you to pay for it.. So it's opensourceish..

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

    I partially share your enthusiasm in having your code seem to look cleaner. At the same time this enthusiasm leads to a fallacy in showing how-its-done to inexperienced coders, since they tend to rely more on hidden magic, imho out of the same reasons that children like fairy tales. Magic elements, no matter their origin, have a tendency to becoming a pitfall. Try not to violate the principle of least astonishment. Have someone code with this library for a year as a beginner and he will 'know' that and 'only that' there is no null checking required or involved, no decision to be made on tell-don't-ask-coding, no input parameter checking and so on. Give this lib to a cobwoy coder and he will re-write his code for demonstrating even more elitist geekiness. Like Jeff Atwood mentioned, most developers have an unnatural knee-jerk tendency to rewrite for the sake of rewriting. Think about it like that: Try to write a book using any language without understanding rules and grammar, will you succeed? Well, it depends, because your story telling is more important and something quite different. You need pathos, ethos and logos, but you also need facts, definition, quality and relevance. Code is meant to be written for other coders to understand, not for the machines and not for your 'own' beauty standard. Omit essentials = omit fundamental understanding. Less code is not meant in a way to hide LoC.

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

      I mean I'm not using this and it's not something I would use myself but there might be someone that thinks this is good for their project

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

    It is not the most popular intent of videos where you show not popular nuget packages with not the best solutions.

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

    I need to disable nullable checking? No thanks, I prefer compilation-time checking instead of ArgumentNullExceptions.

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

      You don’t need to disable null checking. It works with it. NREs are just compilation bars but they can still get nulls. With this approach you can’t get any

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

    First

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

    I’m late to the party and I just discovered:
    if (thing?.NestedThing?.DeeperNestedThing is DeeperNestedClass thingIWant)…
    I had no idea it’s been there since 2015!