"One Line of Code Means Clean Code!" - Code Cop #002

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Nick and this is the second episode of my ongoing series LAPD or LinkedIn advice Police Department in this series we're taking a look at really really bad or questionable advice on LinkedIn which is very easy to find by the way most of it is totally bad and we're trying to make the most of it and provide good advice by fixing it correcting it or just giving some context as always none of this is a dig to the actual Creator themselves but rather the advice and for that reason the advice is anonymized this video is very interesting because it has an example of a clean code piece of advice that really isn't clean at all so it's weaponizing an acronym like keep it simple stupid to make a statement that is just incorrect if lack of content and you want to see more make you subscribe for more training check out my courses on domtrain.com now quick announcement before I move on we just launched a brand new course on Dom Trend called getting started with cleanarchitecturing.net and that course is offered by the excellent educator anikai Monday month you probably know him from YouTube link in the description if you don't already now amikai works for my Microsoft and he works on technology that powers tools like PowerPoint and office in general as well as teams so you know his code is used by millions or hundreds of millions of people every year and after all the research he did when he made this course he said them to quote him this is the best clean architecture course you're gonna find out there for dotnet Now to celebrate the Lots would like to offer the first 500 of you at 20 discount codes or check the link in the description and use the code you see on your screen right now at checkout to claim the 20 discount and two more things there will be a deep dive course coming on clean architecture so if you buy this course you're gonna get a special discount code for the Deep dive as well as long as you subscribe to our mail list so go on dumb train scroll all the way to the bottom and put your email down we're gonna cross check everything and we're gonna send you a code and if you also want to get started with DDD which can be paired with an architecture during checkout you can add that course as well for discounted price too alright enough with that now back to the video alright so let's take a look at the advice I want to talk about and this video will mostly Focus around this single piece of advice because it's very very important so this is what the advice looks like keep it simple stupid.net tip now keep it simple stupid is actually an acronym we use called kiss it's usually part of the whole solid yagmi which means you aren't gonna need it or dry do not repeat yourself and so on kiss is one of those acronyms that people use in the context of clean code so if your code is using dry yagni solid and kiss then it's perceived to be clean code now I have an issue with all those terms and the term clean code in general but that's the story for another video in this video I want to talk about this specific example given here now I should point out that these images do come with text and usually the text is completely useless all you're really gonna get from the text for this for example is that using case will allow you to write less error prone maintainable and Clean Cover that's basically it now before I move on I'd like to let you just take a look at these two pieces of code and write in the comments down below if you you really think that the second is simpler because in my opinion less doesn't mean simpler if anything I think that the second thing is way way harder to read it is not simpler it's actually worse let's take this example and put it into the ID just to have a full understanding of what's going on here so this is the code in the example this is supposed to be the bad example and this is supposed to be a good example now I find this very hard to read and in general I'm trying to keep in my code one dot per line as a rule because having chaining dots and that's actually a rule from the code calisthenics rule set which I don't agree with everything but this one does seem to work for me so having everything most here especially in a Constructor of the string which is not something you usually see can just add more cognitive load to understanding what's going on here this is very simple to me we just have a character array based on the input we reverse that array which is the character array of the string and then we return a new string based on that array as a source it is very very simple this is like we get an input which we reverse and then we use a two array but then it's like what exactly is going on here because it's just a lot I don't think that it really delivers in the promise if we actually detach this bit I would say that this would be better in fact I would just say a reversed characters and then you can have input dot reverse and I could use something like this which in my opinion would be more acceptable or you could just extract this by the way all of this would be optimized so you wouldn't have to worry about allocating multiple Fields these two approaches that one I have before and this one now would perform the same so you could even revert this and say VAR characters array and you can do something like this which at this point you're basically on the same thing you were before so for what good reason would you use one over the other now if you truly prefer this approach the one that has just a single line of code with everything smushed together please let me know because I want to understand the reasoning behind this choice because the meat is not clear at all this is something I would take a look at just scanning through the code and have to pause to understand exactly what's going on if I was to do this I wouldn't go with any of these approaches if the goal here would be to go with Simplicity in fact what I would say is public string and then reverse string Nick and I would go with a dead simple approach which I think is way similar than both of them and you can let me know in the comments if you think it's simple or not and that would just be return string Dot concat and then input dot reverse and that's it that that's that's the reverse yes two dots per line sure we could technically just extract this here and say reversed input and have it be concatenated but this is still way better than both this because it is smaller without sacrificing any maintainability or implicitly or understanding readability but it's also similar than this because you don't have to chain all of those calls to cast it from one thing to another now one of the very reasonable concerns you could have with all this is performance and how would performance play into this because when you're dealing with strings in C sharp and any language really strings are very very expensive so doing something like this and using an example like this in my opinion is also bad because Simplicity can have a very visible impact on performance so clean code isn't necessarily fast code then especially with strings and allocations it can be bad so how we can have a very very fast and memory efficient approach to this well I'll show you we're going to go and say public string reverse string and then fast and have the same input over here and then I can say return string dot create and I can use the delegate approach so I can say the length of this string will be input.length because I know the length of the incoming string and the reverse string will be of the same length then I'm gonna use the state as the input the initial buffer so it's going to be the input itself the string itself and then I'm going to have my characters and my state as parameters and all I'm going to say is take that state and copy it into that character set that I have over here and then that character set just reverse it now this is certainly more complicated but it's not too complicated to render it unusable but not only is it the fastest but also the most memory efficient out of all the other examples let's go ahead and add some benchmarks to see how fast it is so let's take a look at the Benchmark I have over here so I have this string reverse benchmarks I'm initializing the class where I wrote all of these different approaches and then I have a benchmark Mark with two different parameters a small string and a bit of a longer string to see how it scales and how memory scales with longer strings then all I'm doing is I'm just calling The Bad The Good The Nick which is my opinion of a very simple approach to do that exact thing and then the fast approach of doing that thing so I'll go in the program.cs and say Benchmark Runner dot run and then I'm going to say string reverse benchmarks and let's go ahead and turn this into a release mode build and run the Benchmark and see what we get in terms of performance both speed and memory alright so results are back and let's see what we have here so in terms of speed the original advice is actually in the first example four times and in the second example with the longest ring many many like 10 times faster than what the good advice on LinkedIn provided you with and there's also more memory efficient in fact it is the second most memory efficient thing after my very optimized approach and by the way my simple approach is slower and not very memory efficient but that wasn't the goal in the case conversation but as you can see the one using the string that create with the delegate is both the fastest by far its scale is great as you can see over here and it's the most memory efficient so in my opinion it is not too complex and if you are to do any of that you should be using this approach it's not hard to understand work with spans it's a great tool to have to your toolbox but never enough of you what do you think about all this and which one would you be using your own applications leave a comment down below and let me know well that's all I have for you thank you very much for watching and as always keep coding
Info
Channel: Nick Chapsas
Views: 53,114
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, .net 8, lapd, linkedin advice, linkedin advice police department, lapd nick, linkedin advice nick, linkedin advice dotnet, linkedin advice c#, clean code, bad clean code, code cop, code cop nick, when clean code leads to terrible advice, bad linkedin advice
Id: ZCwMIUsZEGM
Channel Id: undefined
Length: 9min 16sec (556 seconds)
Published: Wed Sep 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.