Going all-in with nullable reference types

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey folks i know i'm late to the party but in this video and the post i'm writing i would like to go into some aspects of going all-in with nullable reference types so it's not something new it came out last year with csharp8 and.net core 3.1 but only now i've been able to really use it instead of just playing a bit with it so i've got some more insights on how we can leverage this to really help us get rid of those pesky null reference exceptions for a very very brief intro nullable reference types basically help us by like we do with noble value types for instance in nullable it shows that the value may be null so if we just use it without checking we can get an exception and also gives us that peace of mind that if a value is not marked as can be no then we should be able to use it without problems and without null checks everywhere so we have the two problems sometimes we don't check where we should and other times we check where it's not needed so this is what null reference types tries to [Music] introduce with c-sharp 8 i am not going to go into more details but i'm going to show how to enable it and some things i'm doing as i said going all in to take the best advantage because just enabling it's good but we can do more to really take advantage of this new feature in c sharp 8 and beyond so let's go with a very basic example let me open visual studio so we have this basic application with c sharp nine so top level programs and all that and what we are doing is preparing a string putting in a value a null value and then write writing to the console the length as expected this will [Music] blow up with another reference exception because we are using uh this is null of course this is a very basic example we see it's null but maybe we get the value from somewhere else will you will we always check do we need to so that's where noble reference types come in now let's start by enabling notable reference type so let's go to the cs proj when i find here and over here nullable enable there are more options which enable it partially but i won't really enable all the things so now if we go back to the program let's rebuild just because of reasons and now we see we have warnings here because we are putting a null value in something that is not supposed to be null and here because the compiler is checking not because of this string because otherwise it should be string integration mark but because it's doing flow analysis and detecting that it's being assigned null here so doing this solves this issue this issue remains because it may be null we need to check so this is what just enabling noble reference types does warnings gives us warnings if we go here we see we have warnings now warnings are good but for me it's just really not enough because we ignore warnings all the time so one thing that's really not new and not specific to nullable reference types is just treat warnings as errors so to avoid these kinds of things although not new not new not everyone does it and since we are talking about it let's just take a look at a few ways we can do that so one possible way is to use something called rule sets so new item rule set and it's doing stuff let's see viral studio okay and we can for instance go here look for no related errors let's see here this is it is 600 and this is 8602 so we could look here it's warning so error error we put these three it is 600 and one and two so this does nothing by itself so now we can go to the cs broch and we add a line here just to reference that rule set so this directory row set and now we go here and we have an error instead so with the rule set we can change the severity of the things so it were there were warnings we put errors so this is one possible way if we now build it it gives us error so it won't run and if i try to go to the console it should yeah build fail so no dice another option if you noticed while we were here we have this pop-up saying migrate the rule set to an editor config so we can write the editor config by hand or we can just click this so the editor config serves more purposes the other config for instance is where we can define like the code style to be used in the project so everyone in the team uses follows the same style and when you do out of format it will format in the way that we want we can even define the amount of spaces tabs steps like that and also we can use this so let's create an editor config save and it's here over here it deleted the rule set so we can also delete it from here for instance we now have the editor config which is respected because it is in this folder we could be on a previous folder but it needs to be like in the context so we can define more stuff but right now what we have defined is the severities so errors so we go here and it's still the same treating warnings as errors these specific ones if we for instance hide this one we shouldn't get this anymore yeah now this is a warning again this is an error so this is another option to achieve the same thing and apparently the recommended as we got that pop-up and there's even more let's say i delete the editor config so this is back to warning but i can go here and do warnings as errors and then i can say what i want so cs 8600 yes it is 601 yes it is 602 save go back here and should be error should be error now etc okay so all similar approaches using different file formats but well similar but something i wasn't aware which is great is that instead of listing all the errors here if we want to just enable errors for nullable reference types we can write later let's see delete this this should go back to warning and now i'll write nullable here and if you go here hopefully now etc so this is the best way i didn't know so i was using namely the rule set i didn't know about this but this is much better because with the rule set or the other approaches that we define all the the codes there are a lot of possibilities but like this we just say noble and we get errors so this is great now one thing to keep in mind for all good that number reverence types brings is that it's not a feature that has been there since forever and it's optional because it would be a breaking change to force this everywhere so it's optional and we need to be running the at least the sharp 8 and all of that so it gives us this peace of mind to get these kinds of errors but we still need to take care about the edges what do i mean by edges i mean for instance even if you put says that a string cannot be known if you get a request from an api there is no way to enforce that the the client sending the payload doesn't put there an o so for instance is it's json you put there whatever you want when it reaches our api we need to check it may be null so we need to do the normal validations besides that not only that but for instance if we create a library even if our library uses notable reference types it might be being used by an application that does not so we might say that we don't expect a null in one place but then it will be passed because the other application has doesn't have those kinds of enforcement so we need in those cases to still write guard clauses to check that uh an input is not null and some stuff like that so in those places where our code interacts with other code that we don't control fully we still need to do the required checks and assume it may be null even if we don't expect it to be null and the same for libraries we might use so let's say for example i created here a library on the side and i created a static class set named i don't care about nrts which returns us as a method get null that returns a string with no now this library doesn't have if we go here to the edit edit it's a dot-net sender too and it doesn't have notable reference types so it's oblivious in our program let's say i don't care about this anymore and we go here i don't care about nrt's get null dot length and there's no error even though over here we're returning null but because we are using a library that is not annotated with nullable reference types so we have no idea and it may just happened things like this so this is what we need to keep in mind we have the peace of mind as long as as it's within our code and enforcing things our way but every time we interact with external code unless we are sure that it's a library that we use and it's open source and we looked at it and it's using the level reference types if that's not the case we need to assume that it maybe not maybe it was done in c-sharp 7 or maybe the author doesn't like these new things don't know so we need to take care otherwise in our code we can replace a bunch of guard clauses and null checks because it's our code and if it's supposed to be null we should mark it if it's not supposed to be null then we should state that with it's a string it's not a string integration mark now for another subject related to this is when we let's imagine it's it's at first length it's not something we think about but as soon as we start to use it for real that's why when i was just playing i didn't really want to do a post because i didn't feel all the pains but now that i'm really working with these things some things become more apparent as you really use the future and it's sometimes because we now mark it as can be null or not we may treating the warnings as errors or even having warnings will be sometimes annoying because maybe in one place we know that it's not supposed to be null so we can use the exclamation mark operator but that feels a bit dirty to use too much so sometimes it's good uh my my most of the times i used it i put a comment saying why i used it so we know why it's being done and it's not just me being lazy uh but sometimes we can just it would be nice if we could avoid to spread exclamation marks everywhere so let's see an example we have here i'll paste here a class with an extension method that's like i give it it's an extension method over string and it returns the first three letters and now i'll create i'll use this thing so so basically as you see it's a string it returns a string a nullable string because we don't know if we can get the three first letters maybe the the string is not big enough to get the three first letters and in that case this would be marked as no but the thing is we know that when we return this is the the common try get pattern we see a lot like try pattern for instance int dot paint dot try parse it's a common pattern when it returns true it means it works it worked so we are sure that unless it's bad bad code but assuming it's correctly coded we are sure that the thing in here is correct and in this case we know that it should be should have a value so we could for instance we know that it should have a value so we can do this but it gets annoying to be spreading these kinds of things so there are other options let me go here open the browser browser where is it over here and there are attributes which we can use to help us with this there are more cases this tri-get is just an example but i'll focus on this one but there are a bunch more attributes but let's find dragat so we have over here and you notice that we have this attribute here not null when so let's go back to visual studio and let's put over here the attribute need to import something and it's done so the error is gone and what you can see is that the attribute says not know so this value will not be null when the return value as we can see in the signature is true so within this if we know that this is true unless someone did wrote wrongly but if we go here into the console the right line first three letters dot length over here we get the error because it may be null we're not sure but inside if we we know because it's there so in this case we're telling the compiler a way to check things so it's less annoying so every time we have to do this here but every time we use the method now we won't need to put no forgiving operator or pragmas or whatever so it simplifies things so and like i said there are more attributes this is just one example but i won't go into all of them which can help us in multiple situations so to help us make code that the clients can use so the well-behaving clients will get the best experience but the best behaving clients will have also a good experience but we will be able to double check so stuff like that so it's pretty cool take a look at those docks uh i won't go into all this should be enough to to explain why this is important and why it's helpful when we really start to use it and if we got these errors to to make coding with this feature less of a pain so when that does it for this video uh and i will be finishing the post that's around the same subject uh and i hope you folks enjoyed it and start to see the value of this noble reference types feature and i hope everyone starts using it because if it becomes standard it will be much better for i think everyone and maybe we'll start having less null reference exceptions those annoying things it would be great because those are one of the worst cases of exceptions because you are like okay so where did it happen it's really a pain so i hope this really picks up and everyone uses it again hope you enjoyed it and hope to see you in the next one see yes
Info
Channel: Coding Militia
Views: 263
Rating: 5 out of 5
Keywords: dotnet, csharp, c#, dotnetcore, coding, programming, nullreferenceexception, nullable reference types
Id: f1LTO-2f_N8
Channel Id: undefined
Length: 20min 1sec (1201 seconds)
Published: Mon Nov 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.