"Stop Using if else if else In Your Code!" | Code Cop #005

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 I find questionable advice about programming and clean code tips and that sort of thing and I try to turn it into good advice now that sort of advice is everywhere but one of the best places to find that advice is LinkedIn because of how the algorithm will promote just anything irrelevant of how good it is so in this video I have one of these very bad types of advices that actually was recommended by one of you maximilan tagged me on this post and he said chaps another questionable clean code example which means that we're winning because I actually tend to see less and less of that bad advice as the series goes on and gets more popular so if you see questionable advice anywhere not just on LinkedIn tag me send it over and I will take a look at it and potentially make a video on it now as always with these videos the creator has been anonymized because is not about the Creator themselves but about the clean code advice so we're going to focus just on the advice if that of content and you want to see more make you subscribe for more training check out my courses on doet tr.com okay so let's take a look at the advice but before that I want to show you that people actually go ah this amazing advice that's great like it's not something that I just find that nobody has ever looked at the algorithm will promote those types of advices based on reactions any sort of reactions and most of them as you can see are positive in fact all of them are positive so those are not some obscure things that don't exist you see them or at least I see them all the time on my timeline now let's take a look at the advice itself now the text of the advice goes as follows cop clean code tip tenary operator the question mark and then the colum tenary operator is a short form of an if else conditions the tenary operator starts with a Boolean condition if the condition evaluates to true then it executes the first statement otherwise it goes to the second so this is all that there is to the context of the image I'm going to show you there's nothing else I'm hiding so so this is just sold as basically use the tenary operator when you can when you have an eil condition and let's take a look at the Cod nippet that is served to us as the that's what you should do yep okay so let's start with the first the the the bad type of code that you have or you have an if else if else if else if now if you know me from youy for a long time you know that I sort of hate this type of code and if this was me I would have written this in a completely different way in fact I'm going to show you later in the video how we can actually rewrite this in a better way but besides the point do not use the EAS operator to compare strings it's a very weird way to compare strings and it's not really a safe way to compare strings use something appropriate for comparing strings like string comparisons based on the culture the case normaliz like things that are built for comparing strings now besides the point you have an if else if else if else which you know is a type of code I hate and we're going to see how we can refactor this later in the video but you have code that is fairly easily readable now you could say why don't you just set the environment to the development environment that's because as you can see over here it is called prepro in the environment variable but the user wants to use PRP and PRD here so even the example is Flo because like what is the point of just having this short hand why can't you just use the name on the environment in the parameter but that's fine we can look past that and we can see that we have a perfectly readable fine dodgy looking code but there's nothing fundamentally wrong with it in my opinion actually the ease operator is the biggest problem with this piece of code I wouldn't do stram comparison this way in cop but what's the suggestion you use the tenary operator which means you go with environment is uat is it uat okay then use uat otherwise is environment prepro okay then use prepro oh and then it's like and you have to keep going now if you don't understand what's wrong with this piece of code I'll tell you it's it's kind of long okay that is not just the problem the problem is that it's very very very hard to read imagine that you want to know what happens just by looking at this you have to go check every previous condition before you go into the the one you're interested in so tenary operators are fine if it's for something very short and in fact if it was just about environment is something otherwise it's something else fine use it it is perfect but when you have multiple chain tenary operators that is how do you justify this as a clean code tip it is terrible the only clean thing you could justify about this here is that it's one line of code that doesn't even mean anything one line of code or less lines of code doesn't mean cleaner code in fact most times it means worse code because you're trying to smoos way too much in a very small space like at least you could new line on every else statement to make this look a bit better now just to give a bit of more context with everything and start playing around with the code I'm going to go into to the ID and I've written this example over here so we can play around with it so we start with this which by the way I wouldn't code it this way there's this weird space that for some reason the Creator added after every else if which if you're not going to use KY braces then do that now I'm assuming that they did this because it is in my opinion a bit harder to read this way so you could just use the um add braces refactoring sort of try and make it read a bit better but even then if you want to refactor this piece of code to what they had which is basically this then as you can see that that that is why why it's such a pointless change that even Riders my IDs refactorings don't have it as a suggestion usually you have these sort of shorthand suggestions but the one you're getting here is well the two ones that only make sense use a switch statement so now do something like this this which again I'm not a big fan of because I'm a bigger fan of the switch expression and look how much better this is how more expressive it is how more direct it is and way less bloated now all you have is say config development environment equal environment switch on the environment and then basically build a bit of a map here to say if it's this then this then this and if it's a default fall back into empty string in what Universe would this be worse than this I don't even know what's happening here why is it even there and in fact my ID Rider it recommends a refactoring to the switch expression from the tenary operator because it is better it looks better why would you shoot yourself on the foot by having these chain tenary operators don't don't do this never do this now this isn't the only way you can write this you can sort of have the same type of logic as well but with a map by map I mean that you can have a dictionary which sort of maps out your environment so you can say from uat to uat from pre-pro to PRP and from prod to PRD and then to get the environment appropriate to the one that's incoming you can do something like environment equals environment map get value or default if you find an environment give it back if you don't string or empty and then set that to the parameter so both of these approaches in my opinion are way better I would sort of wrap this into some sort of method I wouldn't just expose it to everything uh but I would just go ahead and use this in my code why would you use the tener operator it's just so so weird now as always I want to know your thoughts in the description down below if you have an alternative or a different way you would do this please leave a comment down below and let me know and if you genuinely prefer the tenary operator approach let me know as well why because I might be missing something as always if you find questionable clean code examples please tag me or send them over and I might make a video on them 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: 67,691
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, if else, don't use else, don't use if else, stop usign if else
Id: kZwIaEAdF0I
Channel Id: undefined
Length: 7min 55sec (475 seconds)
Published: Mon Oct 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.