Write BETTER Code! 7 Tips to Improve Your Programming Skills

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody what's going on I'm Andy I'm a self-taught software developer and in today's video I'm going to give you seven tips that you can start using right now to make your code better just to be clear these tips are from clean coding principles and they are really meant for people who are just starting out so people who are big beginners if you're intermediate or advanced these may be helpful for you maybe they just might but they're really not for people who are just starting out for people who are just starting out just to give you an idea of what clean coding means the very basics how could I could explain it in like two or three sentences is that basically code should be readable you want to increase the signal-to-noise ratio in your code so you want to make sure that there's not a lot of excess a lot of distraction in there and that's what we're gonna base all of our tips on today so let's just dive into it so number one you need to avoid abbreviating your variables so we have a good example here we have three variables that are declared and it's not super clear what they are maybe whoever was using this thought that a if I were if I abbreviate the word password to PWD and users to users that everyone else is gonna understand exactly what I meant when I was coding this but that's just not true even if you know what you're doing two weeks later you're going to forget what your intention was here and you can come back and be a little bit confused Bri VA ting variables can be appropriate but they have to be universally recognized so I only really recommend using abbreviations for things like URL HTTP things that are widely recognized more than that though if we go to use these abbreviated variables look like this is a for loop trying to read through this it's a little bit disjointed because you're going to have to stop and try to process some of the variable names it can lead you to taking a little bit more time than is necessary and that adds up in a large code base so instead what we should do is just descriptively name our variables so in this case we have email address password and user and then if we use that in the sent same exact context as we were using before with the abbreviated variables you can see that it's easier to read okay we're looping through a bunch of users if the users email address matches the email address and the user's any of the users password equals the password if we get those two conditions to match then we are going to log these the system that's pretty I think it's pretty straightforward and that makes sense so don't abbreviate your variables just descriptively name them the second concept that you really want to get down is function arguments so you don't want to have really longer than three function arguments so let's look at an example here and you see a function that has what is it six arguments that's way too many just looking at this here this function that's declared it's like there's just there's a lot you have to go through and if you see when this function is actually called later on the code you can see that we have to pass in a bunch of strings if your code editor is this big it's gonna go all the way the end of the screen it might get cut off depending how many arguments you have but not only that like I see here where the function is called you have one of the arguments is called active right that's okay so if I didn't know this code if I was just coming into the first time how do I know that this fourth argument what active even means I'd have to either hover over it and hope that they're the intellisense will tell me what the argument name is or the parameter name is or I just have to go back to the function and see what's even better is just to create a function like this in this case this is JavaScript that just takes one argument that's a user and when you actually go to call this function you're going to new up a new object that's going to have all of those parameter names inside of the object properties so you can see I have a username password email address status IP address etc this is very clear what's going on because now when the function is called I just look above it and I can see what the user IP address is these are statuses there's no ambiguity there it's just much more clear so I highly recommend keeping arguments to three or less and using strategies like this to lower your number of arguments that you're gonna use the next one is my personal favorite for people who are just starting out it's simplifying your conditional expressions so let's just look an example to make this dead simple so this happens a lot with people who are just starting out so they love going through if-else statement because when you learn if-else statements it's just like you want to use it all over the place it looks so fun and cool it's so much so programming right but in this case I created a function can person drink alcohol we pass in a person and it makes sense here if the person is less than 21 years old we won't return false otherwise we want to return true this is this makes perfect sense this is a great function it's modularized it's very clean that you know we took some logic from somewhere else and moved it in a well named function here's the thing we can make this code a lot simpler if we just return the conditional expression directly so if we say person age is greater than or equal to 21 then we've done the same exact thing without using what six lines of code now you think yourself well it's just six lines of code well if we can just do this all over the place in our code base if we have Ellis's all over the place and return one line your code base is going to be a lot easier to read a lot more pleasant to work with and you know if you have a really complex code base this sort of thing adds up so anytime you can return conditionals directly you're going to want to do it the fourth tip that you also want to take to heart is declaring variables close to their usage so I actually found my Tetris game that I created a long time ago at this point was what had to be five years ago and you see that there are a lot of variables declared at the top of our file here now my file I think is anywhere from like five to six hundred lines long this is just a quick sample of it but I declared so many variables at the top and then started using them all the way down and this is not a good strategy because if you are debugging your application or even just working on some functionality somewhere you know deep down in your file and you have our your your your using variables that are clear way up at the top well that's just gonna be very frustrating because in this case I named variables completely terribly I've abbreviated like I said not to do but you're gonna have to jump around and constantly look for the context where that variable was declared and it's just gonna cause you to lose track lose focus and again your brain you have to think about your brain like you want to lighten the load on your brain the coding can be very difficult as it is trying to manage complexity so reduce complexity by declaring variables close to where they're used and that will avoid a lot of problems of trying to deal with some headaches that can come with that the fifth tip that is kind of a sneaky problem for a lot of people is making sure that your functions do what they say and you want to avoid unattended consequences so let's look at an example it's probably easiest to understand so we have a function here called add numbers and you get that the function takes two arguments the first number and the second number now if you have a function called add numbers you're gonna expect that that function probably gonna add those two numbers together and maybe return it but in this case we're sending an email that doesn't make any sense maybe there's a use case for that but look you have a function name add numbers this email being sent out doesn't make sense they're they're not logically they don't logically make sense that's not an abstraction that makes sense so that's what I would consider an unintended consequence if you want a function that adds two numbers just add two numbers as you can see here this looks a lot better another good example here is a pen string like if you have function called pen string that takes two strings you're assuming that you're gonna pen those two strings together you're gonna put them together in this case whoever created it decided that it would make sense for them to add some white space in between the first string and the second string but truthfully that's not really an intended consequence because if you have a function named append string I'm gonna assume that you're just putting those babies together that's what I would expect from my years of programming and well yeah you could argue that yeah maybe if you're gonna pen two strings together they should have space that should be clear from the function name so either change the function name to say something like a pen string with white space in between or just change it to this and just change it to no white space in the middle just keep the function name as it's gonna be so you wanna make sure that if you write a function and its name says it's doing one thing then it should do that thing the sixth tip that I'm going to give you is making sure that your functions do one thing and one thing only they do really well and avoid long functions so when you have functions that are 25 50 75 hundred lines I've seen a thousand line functions that's when problems just start to pile up because first of all that function is not doing one thing it's doing a few things it's not doing one thing very well it's doing a couple things very poorly and and when you're going to function that is very long you're going to have to start managing complexity as I said before so your brain in order to like try to manage everything that's going on that function will have to keep things in its you know short-term memory and you're gonna have to just store so many things your short-term memory that you're not going to manage all of the things that are going on so I just create a function here that I think you might see in some code bases and look it's actually not that terribly long but just if you look through the function here there's a few things going on that don't necessarily show and abstracted into another function you have database going on you have values being inserted into a database you have emails being sent out so something like this just looking at it is hard to kind of parse through there's just a lot going on this could easily be refactored and simplified so that the code is more readable so that when you have a create user function it should just be pretty simple what's going on here there should be nothing you should be distracted by random things that are going on and you should be it should be able to flow through the function very easily so number one thing is avoid long functions make sure that when you write a function it does one thing and it does it really well that's really good modularization it's very clean kodesh that's a word and you're going to want to follow that and the seventh tip is my absolute favorite you're going to want to avoid zombie code okay so zombie code is code that is currently dead but could come alive if somebody decided to uncomment it so you can see here this would be an example of someone leaving zombie code in your code base zombie code is typically left by developers who wanted to leave a little snippet of code where they were working because they did couldn't trust that that code could be deleted right so they're working with something then I can I'm just going to comment this code out because I may come back and use it later but what's really scary is this code could come back to life and cause really bad harm to your code basically it could introduce bugs sneaky little bugs what's even worse is this is very distracting so we said before you want to have a high signal of noise ratio well commented code is distracting because somebody's gonna stop and they're gonna read it so it causes the reader to slow down so the reader is gonna save themselves also like is this something I should implement should I uncomment it should I put it in here really what zombie code comes down to is you should never put this into production obviously like you should never put this into your portfolio applications make sure you remove it to me it looks like bad form well if you really want to leave zombie coding what you should really do is use source control so use git to save your code or any code that you're afraid to delete for later on when you actually want to use it so use source code appropriately avoid zombie code and you will have code that's looking better and reading ease more easily alright so those are my seven tips that you can use right now to write better I hope you enjoyed the video go ahead and leave a like if you enjoyed it and subscribe to get more content anytime I put out new videos make sure you hit the bell icon and just let you know I just recently put out a PDF report on the top 5 programming languages learned in 2019 it's a really good report it spent a lot of time on it I think you're really going to enjoy that so go ahead and download that and and easter close comm forward-slash report other than that that's really all I got for today so thank you so much for watching and peace out guys
Info
Channel: Andy Sterkowitz
Views: 172,771
Rating: 4.9214287 out of 5
Keywords: how to write code, how to improve your code, writing computer code, clean code, learn to code, how to write clean code, clean code tricks, programming tips, coding, learn programming, learn to program, javascript, how to code, programming
Id: vfqvzgkYYF0
Channel Id: undefined
Length: 11min 25sec (685 seconds)
Published: Sun Jan 20 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.