"Remove Braces From Your Code Now!" | Code Cop #004

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 to LinkedIn Twitter or even blogs and we try to find really really bad advice try to be sold as really good advice on how to write cleaner or better code LinkedIn specifically notorious about this type of advice and in this video I have probably one of the worst ones I've seen because even though LinkedIn will promote everything and people on LinkedIn will eat anything you throw at them this one got actually some backlash and this I do want to take a closer look because not only it's a terrible advice that some people will just believe but also the technique used to convince you that it's actually really good advice and you're actually wrong for not following it is atrocious so in this way you're going to see all that and more now I should point out like I do in every video this is not an attack to the Creator themselves in fact all of the creators in my videos on code cop are anonymized so we're just going to focus on the advice and try to make some sense of the advice and try to turn it into good advice if you like content and you want to see more make sure you subscribe for more training check out my courses on don't train.com okay so let me show you what I have here and I'm going to start by the text of this advice which is clean code tip six which means there's been five before that and probably more after that coming keep that in mind avoid unnecessary braces let's see what the person means often times extra braces reduce the readability of code wait until you see the code I go out of my way to delete extra braces wherever I find them my recommendation is that you do the same this is not an April full joke this is actually advice on the internet let's take a closer look on the code sample so the code in the post looks like this clean code TI avoid extra braces with short clean functions readability is often improved by dropping unnecessary braces let's take a look so factorize now don't try to understand what this method is doing just says factorized I'm not going to think too hard about it but the recommended approach here as you're going to see is first return an ey list and then have the factors in a list and then have a for Loop and then a for Loop within that and then add the devisors into the factors and then return those now this could be way easier if you just had an enumerable and you yield return so you wouldn't have to store the list and so on but that's fine that's besides the point the idea is that this is bad what's the good one this one this isn't a joke this is advice on the internet so this is what the author says is more readable than this now if I just saw this look I'm looking at this and I don't quite understand what it's doing this in my opinion is way worse and please leave a comment down below before I go any further into the video which one do you prefer do you prefer this one with the braces or this one without the braces I'm going to explain why I think this one is better but please let me know if I am wrong or I'm judging this in a wrong way let's take this example and put it in the ID to take a closer look now before I move on I'd like to let you know that we just launched the massive 6our course on Dom train called from Zero to Hero Test driven development in cop that course will take you from knowing nothing about testing in tdd and get you to a point where you fully understand the concept and you have all the technique and knowledge to apply it on any cop code base it is authored by GE Hera who has an excellent YouTube channel Link in the description but he's also someone who I work with personally in my previous company and I can 100% V for him he really really knows the topic and he actually changed the way I see the topic as well and the way I teach it I believe it's a must know for any developer and the first 500 of you can use discount code td20 and click the link in the description to claim it at checkout this do tend to go very very quickly and I don't renew them late so if you want to buy it then buy it now now back to the video now if I take those two code samples that's how they're going to look in the ID now like I said I could just turn this into n enumerable remove the factors remove this return and turn this into a yield return and do this the downside of this is your risking multiple numeration with whoever is consuming it Upstream but they can probably just deal with that themselves they developers they know how I numerable works so that's how I would write this if I had to write this but let's just revert it back into this now by going ahead and removing the braces on single line Loops in my opinion you make this way harder to read because these types of braces allow you to define a scope in a very obvious way you don't have to guess you don't have to think about it for example imagine if you had something that can be disposed let's say a logger Factory so I'm going to say new logger Factory over here right now a logger Factory can be disposed and you have a scope now you could just say using VAR and then assume where the scope is or you could say using VAR when it's not too obvious and have the scope in here do you need these braces maybe you don't but it makes it more obvious for the reader and the idea behind why I'm saying this is because it is actually very situational to know whether you need this or not in this case I think it does make sense because it is actually a complicated sort of for loop with the nested for Loop method and because the subject matter as well is sort of math related and logic related it's easier to visualize what the function is doing by having the scope clearly defined even if you can save four lines of Code by removing it this does not this looks like a blob to me and I don't like blobs in code in my opinion even if you have a single if check for example to see if something is I don't know let say if divisor is zero for example I wouldn't do this I would drop this into brackets because even though I had two lines the scope is way more obvious in my opinion this just makes it harder to follow so please leave a comment down below let me know which one you prefer and which one you would use and why especially if you prefer this one you think this one is cleaner please let me know why you prefer that especially as well in the context of cop because I do know other languages especially languages like python for example who are intend based are maybe more used to this type of programming which again I'm not a big fan of now the biggest issue I think with that piece of advice isn't the advice itself which I hate I'm not going to lie but it's actually the technique used to convince people that it is the approach they should follow because I just gave you a two minute or three minute explanation of why I think it is better to have this approach but what the author of that post said is this Uncle Bob is with me in this matter and then he just linked to an Uncle Bob post now I'm going to go ahead and say it clean code has some solid advice but a lot of it is opinionated and I disagree with it I'm not going to recommend everything in the clean code book in the same way I'm not going to recommend that everything that uh Bob Martin will recommend and the biggest issue with trying to convince someone about something especially on a subjective matter like this is say that this influential person said so because in the same way that anle Bob said this someone can say Nick said that Michael said this John said this and so on and it doesn't really matter you know who's very influential in the programming space Jonathan Blow now go ahead and follow every advice and any opinion that Jonathan Blow has to say and let's see how long you survive and I'm not saying you shouldn't follow anyone's advice I'm saying that you should think on why did that person give that advice and try to use that as an argument not that this person said it he is a link but this person said it because this this and this so the reason why I follow it is this this and this and don't even mention what influen you to make that decision it's like going into a company and you're a junior developer you have a solid idea and the principal says no and you say why no and the person says because I'm the principal and you just have to follow me that's not how it works you have to explain your point even if you at the point of authority that's not just for me to be like oh okay you're right but also for me to learn why you're right and your standpoint that's how we get better in any case I want to know from you in the comments please let me know and have a healthy discussion explaining why you prefer one over the other and how do you feel about this type of advice being sold on LinkedIn as the clean way of doing things 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: 53,740
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, uncle bob nick, nick uncle bob, nick chapsas uncle bob
Id: 4h8mKzptMzQ
Channel Id: undefined
Length: 8min 35sec (515 seconds)
Published: Wed Oct 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.