The Right Way to Write if Statements in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Nick in this video I want to talk about which one of these should you use if you have a single line if statement what should you do how should you code it should you have it like this without curly braces should you have it like this in a single line should you use curly braces with the opening here or should you use this version instead there's been a lot of chatter about this lately and this was actually supposed to be a code cop video where I go through this piece of advice that I found online where it says don't use use this use this or don't use this use this but then recently there was a Reddit post it was also talking about this topic which got extremely popular it got 300 comments and there's tons of opinions here so in this video I want to see all the opinions I'm going to give you my own opinion too and let's see also in the comments down below which one you prefer write it down and let me know and let everyone else know which one are you using and why explain your logic behind the choice if you like of content and you want to see more make sure you subscribe for more training check out my course is on doet tr.com okay so let's start by the thing that was supposed to be the code Cup video first so you have the single line versus multi-line if statement even if you have a single check should you do this or should you do this so nothing about Curly brackets yet just should you use this or this and the text accompanying the advice was single versus multi-line every time I encounter single line statements in my code reviews my immediate reaction is a hard pass that's why multi-line coding isn't just prefer it's the only way way to go if you want my sign off basically don't do this do this have it as a multi-line let's see why each statement has its own line making it self-explanatory and easier to follow there's some truth to that I think with break points on individual lines tracking down bugs become less of a daunting task I think that largely depends on the ID you're using Rider makes it very easy to understand exactly what's going on but I digress anticipates the need for adding more logic without rewriting existing structure I don't quite understand this it doesn't really make sense maybe Chad GPT wrote it when're not coding in isolation multi-line code is considered code it says I care about the next developer who reads this and in a world of collaborative projects that's a golden rule that's not just about preference it's about Professional Standards multi-line code is a Walmark of a developer who values Clarity and maintainability and there's certainly some truth to that however I want to go beyond that because I think both of these approaches are really really bad in my opinion and I will explain why and I will explain why by going to the next point which has to do with the Reddit post I just showed you now very quickly before I move on I want to remind you that is Dome Train's first birthday and we're celebrating it with a discount that is at the level that we usually do on Black Friday so as good as it possibly get until the 30th of April you can use discount code birthday 40 to get 40% off any cost birthday 20 to get 20% off the already discounted bundles we have and you can use birthday 15 to get 15% off your first year of Dome train Pro the annual subscription that giv you access to all of our courses do not miss this opportunity invest in your future now back to the video so which one do you use do you have the Kelly bracket here after the each statement when you have a single small if statement or do you do it like this where you have the Kelly bracket opening in the next line and then here again that's app to personal preference however Microsoft tells you to use one of the two let's go to the ID and see all the options so what I have here is that same example and I have if success returns true if success same line true if success bracket here true and if success a new line and everything is on its own line Now spoiler alert the one Microsoft wants you to use is this one and that's the one they're using and there a few analyzes about this for example here we have ID o 1 one which says add braces so if you have statements like this or this one actually uh then add braces and we can actually see that here if I go here and I say refactor this and then use the refactorings available add braces is one of them and it will automatically format it to a multi-line version as well so there's two aspects here cuz you can just add braces and have it be like this this that's another option uh but then gets also very confusing maybe less confusing than this version for some people but that's also an option and then you also have this in code analysis which is c new line before open brace so the option concerns whether an open brace should be placed on the same line as the preceding code and if you use the refactoring you're going to see this so if you do it for everything then you're going to be sort of forced to have it like this and if you choose none then it's like this there's other analysis and guideline posts about this but generally Microsoft says do this and we can see that on Reddit as well where the top comment says Microsoft guidelines says two if I happen to stumble into a project using one which is this version over here I'll use one consistency is more important than person preference and I generally agree with this comment however there's another very interesting comment which I've actually seen being applied in my previous job when we had some new senior Engineers which is Microsoft guidelines says to if I happen to sound across into a project using one I'll apply a for matter to the whole solution and push a PR to fix the horror consistency is more important than their preference I've seen this happen I've seen senior Engineers join companies and just do that and apply their own formatting don't do this in general this comment encapsulates my opinion on the matter now do I prefer to yes and it's very funny that go actually has a compiler error if you try to do this which is the Microsoft way to remove any ambiguity about which one you should be using they say this is the one that we allow if you don't like it well you can't do anything about it your code won't compile now there's other opinions which I also agree with which is two I wasn't raising the bond very very valid comment and also another one which also applies to me is I start with Java and used one but I switched to two as far as I know it is by convention in cop so Java is more likely to look like this while C is more likely to look like this now why choose one over the other please if you have a preference pause the video now and type why you choose one over the other but I'll tell you why so first and foremost I think this is terrible and the reason why it's terrible is because it can lead to situations like this where you have stacked requests and you don't know that the scope is different because you have no braces to keep you in a specific scope which is also the exact same reason why generally I don't use using VAR stream equals new memory stream but I instead use this version with the parenthesis because I want to be explicit about the scope because there's a disposal aspect to this that I really want to have my hands on and be very careful with how far it can go and where it can get disposed implicit Scopes are fine but I generally prefer being explicit with my code because because it reduces cognitive load and it's easier to understand what's going on without having to assume much then this just doesn't read nice to me especially with complicated if statements it can look very weird and yeah you can say that the scope here is explicit but what if you have this for example it's the same problem as before just doesn't look nice in my opinion then for the other two I think it's just a matter of what we're used to you know we're used to having I in front of interface is in C and in the same way we do that then in the same way you use the second convention over the first one I don't necessarily think one reads better than the other you can write perfectly clean C code or Java code or any other language like I said go uses this as its default and no one has an issue with it so we're kind of more used to this version and we just train our rise to seek for it and just consistently use it more than anything else in general beyond the is just how I'm used to doing things I do prefer this one because it's like one thing at a time while having it like this is kind of two things cuz you have your if statement and you open the condition which you can say it's minor enough but I think there's just something to this version which makes it maybe a bit longer but very very explicit but now I want know from you which one do you prefer and why leave a comment down below and let me know well that's all I had for you for this video thank you very much for watching and as always keep coding
Info
Channel: Nick Chapsas
Views: 49,964
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, c# new features, c#, .net new features, .net core new features, .net 9 features, c# 13, .net 9, if statement, if statement c#, if statement .net, choose the right if statement
Id: QSOCXxQ-BQA
Channel Id: undefined
Length: 9min 4sec (544 seconds)
Published: Fri Apr 26 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.