"Stop Using null, Use default Instead in C#" | Code Cop #010

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Nick and welcome to another episode of Code cop the series where we go over advice usually given on LinkedIn Twitter or blogs that is usually terrible and we try to correct it and learn from it in this video we're going to take a look at a very interesting piece of advice that many people seem to also agree with but I fundamentally disagree with and this video will explain why now as always the post we're going to see in this video is anonymize this is not about who said it it is about the advice itself if you like of content and you want to see more make sure you subscribe for more training check out my courses on doet tr.com okay so let's take a look at the advice itself so in terms of the code that's the snippet that the author posted so you have two methods over here and the only difference is that this one when you don't have any entities after this call is returning null while in this one you're returning default now in this context this is presented to be the bad thing that you shouldn't do the bad practice while this one where you returning default is the good practice now I think this advice is terrible and the code you will produce by doing this will be equally terrible I think default is a terrible keyword for this context and let's see why but before we see why let's take a look at the text that this author also wrote to understand what they mean with this type of image so C has a handy keyword called default that essentially does what you normally do manually for reference types it returns null for Value types like integers it returns zero and booleans it returns false this means you no longer need to explicitly return the default value types yourself you can basically use default to do that the key takeway is you can use the default keyword instead of returning null explicitly and as you can see some people love this advice they found it helpful and the linking algorithm will go ahead and just promote this post now like I said I hate this type of advice and to see why I do let's hop into my ID I have a very similar code sample over here which basically has the exact same code just a little bit different because I didn't want to necessarily add a mapper in the equation as well but it's basically the exact same thing as we have in the infographic so in this case we have the previous code we have null and the advice is return default now why do I hate this well I hate this because it makes me now have to look into two places to understand what my code is doing that's the biggest issue there's also another massive one we're going to see that in a second but first why do I say that well when you say return null you can stop reading here because you know that the code at this point will return null you know that the type you're returning into is a nullable type however if I change that to return default even though this value is calculated at compile time I now have to not only look here but I also have to raise my eyesight and look here as well to see what I'm returning and imagine that this code is this method is long I'm not saying methods should be long but if this method happens to be fairly long then I have return default here and I don't know what I'm returning because the type is all the way on the Declaration of the method so don't use the default keyword that way if you're using it with generics then it does make sense to use because sort of it's the only way to use it if you want the default value but default doesn't mean null because default value is relative to the type you're returning for example in this case I'm returning an iron numerable of Bank details response and that default value if I computed in a constant form is null because this type is nullable however if this is not nullable in this case this will still be null but is violating the null reference types feature however if this is an integer for example default in this case doesn't change but now I have to mentally know o integer is a value type and value types default value for integers is zero which how am I supposed to know that in the same way that if you have a string for example and you go with default okay what is the default value of a string is it null because it's a reference type sure but some people might assume it is an empty string which is not it's actually as you can see over here null and then you go into other categories for example if I have a struct over here and I use a struct and I use the default keyword what's the default of a struct well a struct is a value type so well there's a default type of that struct and by the way I should point out at this point that this is the default literal feature I can turn it into a default declaration or like specify the type over here which would solve the problem of having this all the way at the bottom without knowing what the type is but the refactoring recommended here is use default literal so that's why everyone is using this now again this is totally fine if you have no other option and you really want the default value which for example if you're using generics it might be the only way you can go forward with this but for most cases if what you want to do is return null then just return null and nothing else I don't understand why we have to make things complicated now I do want to point something else out as well if you have a method like this don't return null null is a bad thing to return when you're dealing with innumerables lists arrays or just an inumerable itself what you want to do instead is not return anable type but return an enumerable do empty of that type in this case Bank details response and that way whoever is processing that piece of code can either check whether you have any items or just iterate without needing to have needless null checks in their code that will lead to better code more resilient code more robust and clean code another thing I want to point out at this point is that if I change this to a a class instead of struct and I say a class I personally don't have much of an issue with the following if you have a property and you have like a string property over here and you say first name for example uh you might want to say that this is definitely not nullable this will be set by something so that is one of the approaches you can do this the other approach is to use a default exclamation mark now this in my opinion it's still not great and by the way null exclamation mark is also not great but it's definitely better than the example we saw because you can see the type and you can see the default value and if the type is a common type and you use default that is fine however again if you have a struct and you a default or something that is not a BCL type I highly recommend you don't do that and even if it is a BCL type the zero value of those types can be changed especially with how things change in cop 11 so it just makes things so so much more confusing if you see this in this context I don't think it is as bad as doing it here however in my opinion it is still pretty bad but now I want to know from you what do you think about this advice and how do you feel about returning null returning default returning inumerable dot empty or ar ra empty where possible leave a comment down below and let me know well that's all I had for you for video thank you very much for watching and as always keep coding
Info
Channel: Nick Chapsas
Views: 64,139
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, dotnet, .net, code cop, code cop nick chapsas, code cop nick, code cop default, code cop null, null vs default, null c#, default c#
Id: iEGaj1CNrl8
Channel Id: undefined
Length: 7min 6sec (426 seconds)
Published: Mon Jan 22 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.