5 Nooby Coding Mistakes You NEED To Avoid
Key Takeaways
The video discusses common programming mistakes and how to avoid them, covering topics such as simplifying conditions, improving code readability, and using variables to simplify code, with tools like Brilliant offering interactive lessons in math, data analysis, programming, and AI
Full Transcript
please don't write code like this today I'm going to be showing you the most common programming mistakes that I see literally every single day at some point in everybody's programming Journey they've made these mistakes and this video is going to help you fix them very quickly so with that said let's get into it and learn about the common mistakes and how to fix them the first mistake I see time and time again has to do with conditions and if statements now a lot of times there's many different things you need to check in order for a condition to be true are to go into a certain block of code so I've given you a simple example here you can see that we're checking if the user is authenticated if they're an admin or a manager or they're an editor and they have their permission edit if their account status is active if they have a last login if their age is between 18 and 35 if their IP address is not in this range and you get the idea there's a bunch of different conditions and although this code is fine it will work it's not horrible there's a significantly better way to write it so let's go over to the fixed version and now look at this now it's a little bit longer sure but don't concern yourself with the length of the code what we're more concerned about is how readable is the code and how easy can we understand it longer code that's easier to understand is always better than shorter more complex code so if we go to the top here you can see that what we've done is we've just Associated each condition with a variable now some of them are a bit redundant like is authenticated for example we probably don't need a variable for that but for the other ones what this does is just make it a lot clearer on what we're actually checking so we're checking are they active have they had a valid last login are they of valid age is there a valid IP address and what this is doing is two things it's kind of automatically commenting the code for us because we're writing it like we would write normal English and we're simplifying some of the longer conditions into one variable now if we keep going you can see that we have some more complex conditions like for example has proper role in this case it makes even more sense we're saying the user. roll is in admin or manager or and then we check this so we've kind of taken this complex condition that might take a few seconds to understand if we're reading it and we've just wrapped it inside of a variable that defines exactly what it's checking same thing for has proper method for the request for example and then same thing is suspension compatible so then we go down here and you can see that we have all of these ands we've combined them together and now we can return true so if I want to see what we're checking in the if statement I can read this just like English are they authenticated do they have a proper role are they active do they have a last login Etc now this is a little bit Overkill how I've shown you here I'm just trying to demonstrate the point A lot of times you'll have multiple different conditions and rather than checking them all in the if statement you can make variables to simplify the condition and then combine those variables together to be able to read your code like English which is really what we're going for so this next example is a simple one but so many people make this mistake now look at this code pause the video and see if you can figure figure out a better way to write it now immediately what should stand out to you is that we've got this kind of staircase going down you can see that we're kind of going on a diagonal the reason for that is that we've got a lot of nested blocks we have if statements inside of if statements inside of if statements inside of if statements now for simple code like this it's okay we can still understand what's going on but if we keep going and add more and more ifs inside of each other it gets really complicated really fast we've got all of these levels of nesting which is what it's referred to which is just really difficult to read and especially to change or understand what's happening so you can see that what we want to do here is we want to go through all of the numbers that are passed to our function we want to make sure first of all they're not none they're greater than zero they're an even number and they're less than 100 so what we're looking for is all the numbers between 0o and 100 that are even and then we're going to add that to our results list okay that's fine but it took me a second to understand that because of all of these levels of nesting whenever you've got this the staircase p pattern it's probably a hint to you that you could simplify the code a lot and make it flat ideally when you're writing code you want the fewest levels of indentation possible because the more indented blocks you have again just the more confusing the code is and you've got all these staircase patterns going through which makes you kind of have to use the mouse and figure out what block is where and what's going on especially when you start adding you know else blocks here and printing other stuff and adding more indentation gets really confusing really really fast so let's look at the solution here which is this one so you can see immediately the code no longer has that staircase pattern and we've been able to reduce the indentation levels to almost flat now you can't always reduce every level of indentation but here you can see that we only ever have one if statement we don't have if statements inside of if statements inside of if statements inside of if statements so what we're doing here is we're using a pattern that's very common notice that rather than checking all of the things that I want to be true I'm actually going to check the things that could potentially be false and result in me not adding this number to the list this is really common reversing the direction of the condition to simplify your code so rather than saying okay is the number not none like is it a valid number I don't check that I check if the number is none now if the number is none immediately I know I'm not going to be adding this to my list so I can just continue I can go back up to the top of the for Loop and try the next number next thing down here I say okay let's check if the number is on let's check if the number is out of our range and if it's either of these so if it's out of the range or it's odd let's continue so I'm going through all of the things that could potentially eliminate this number from getting added to the list and I'm checking those first and then if these are not the case I can simply add the number to my list so if you look here you can see the code side by side right here I check if the number is not none here I'm checking if the number is none if it is none I can just continue and that means I can eliminate that level of nesting okay that's great next thing we check if the number is greater than zero if it's even and then if it's less than 100 so rather than doing that in three indented blocks I just write a few simple conditions here and I check okay is it out of the range or is it odd if it is we simply continue so all of this is kind of eliminating all of those numbers and then we get to the point where we know we have a valid number because we've already done all of these checks and they haven't been true meaning the number is valid so we can add it to the list I know it's a little bit confusing when you first see it but this is a really great way to remove those levels of index ation and it's very common especially in Loops that you're going to see things like the continue keywords and the break keywords to allow yourself to avoid having all of these levels of nesting so the next example is something that probably looks familiar to you I can almost guarantee you that at some point in your life you wrote code that looks like this check it out what we've got is a bunch of if statements or if and L if statements to check things like numbers so for example maybe you want to print out the day of the week and you have Days 1 through 7 so what you do is you say okay I'm going to write a function I'm going to check if the day is one if it's one we'll print Monday if it's two we'll print Tuesday and you keep going and you check all of the possible options and sometimes I've seen people have 50 if statements 100 if statements checking every possible number that you could have in fact even sometimes when I ask people to write programs to check if a number is odd or even they just write out all of the different numbers they can think of so numbers you know one to 100 and if it's one they return odd if it's two they return even if it's three they return odd and they write code like that it's totally fine it's a logical thing to do if you don't know any better but obviously there is a better way so let's check that out so here's the first way that you can fix this code now what I've done is I've created a dictionary so whenever you've got kind of values mapped to some other values like a one is Monday a two is Tuesday or something is equal to something else but you don't want to make all of these different variables or write a ton of different if statements what you can do is you can kind of map them using a dictionary so notice what I've done here is I've mapped one to Monday 2 to Tuesday 3 to Wednesday and I've written this dictionary that contains all of my keys associated with their values exactly what a dictionary is used for and then I can simply get out of the dictionary the key and it will give me the value so if I say days. week get and then the day number if this number exists as one of the keys inside of the dictionary it's going to return me the associated value so if I put a one here it's going to give me Monday if I put a six here it's going to give give me a Saturday and if this number for some reason doesn't exist as one of the keys in the dictionary we'll simply return invalid day number that's what happens when you use the do getet method now if you were to use the square brackets you'd get an index error or a key error if you tried to access something that doesn't exist that's why I'm showing you dot get so this is one way you can do these mappings now another way that you can do this is using an array especially if you're using numeric values so here's another method that I have we have this array Monday Tuesday Wednesday Thursday Friday Saturday Sunday and notice that every single value in the array kind of corresponds with an index right Monday's at index zero Tuesday's at index one Wednesday's at index 2 and what we can do now is we can check the day number so we can say okay if the day number is between 1 and 7even then what we're going to do is access inside of our array or inside of our list days of the week and then day number minus one now we've subtracted one so that we can associate day one with index zero day two with index one so we kind of map up you know the human readable number starting at one with the computer readable number starting at index zero so that's another way to do it doesn't matter whether you're using a dictionary or a list but these are things that you can Implement to avoid writing all of these different if statements so just see how much easier it is to understand here especially with the dictionary example because I can really quickly see okay four maps to Thursday five maps to Friday and if I want to add more I can just add entries to the dictionary rather than adding more and more and more if statements which just isn't the best way to do this now another reason why this is better is that rather than having to potentially check every single one of these conditions what I can do is just very quickly look up the value using this dictionary and what's known as constant time I can just immediately grab whatever is associated with the number whereas with the if statements if I'm looking for day seven for example I first need to check if the day is one if it's two if it's three if it's four if it's five if it's six and I have to keep going and check all of the different values which if you're in some kind of performance critical code is not going to be the most efficient efficient way to do this again if you keep adding values you got to check every single possible value until you eventually get to the one that you're looking for now the next example I have is something I was guilty of for many years when I was writing code and that is the Mega function now the mega function is just a function that does everything it's one function to conquer them all handles every single task and is very very confusing especially when you come back to it a year later so let's look at this code here now immediately if I ask you to tell me what this function does in 10 seconds are you able to do that maybe some of you are if you've seen something like this before but chances are it's going to take you at least a minute or two to read through this code and understand what's actually going on now the purpose of this code is not important the point is that it takes a long time to understand what it's doing and when you're writing code you want that code to be self-documenting you want it to express really clearly and really quickly what it's doing so that other people that look at it don't need to spend so much time and so much brain power trying to dissect it and figure out what's going on so if we read through it we can see that we're first making sure the order is a dictionary if it's not a dictionary that's an error we're also making sure that items is in the order and customer is in the order if it's not then that's an error because we need to make sure that we have items and we have the uh customer then we go down here and we're say okay we're going to grab the total price I think that's what that's doing I got to read through this and make sure we're going to make sure the price is uh in item and that quantity is in item as well before we do this then we're going to see if we need to apply any discount then we're going to make sure that we have a payment method and then we're going to process the payment using the provided payment method okay so I can understand that but again like it takes me a second I'm not 100% sure what's going on and I've got to read through every single line of code in order to figure this out now worst case if this function got bigger and bigger and I needed to go in here and change something like maybe change the way we're calculating the discount or the possible payment methods again it's going to take me a bit time to locate that code and it's going to be more prone to errors so let's look at the way that we can fix this so what we've done here is yes our code's got a little bit longer but we've separated it out into logical functions so each part of this function now we've separated into smaller subf functions so that it's easier to understand and to debunk so now we go back to the original process order function which is right here if I want to understand what the process order function is doing all I need to do is read these six or seven lines of codee so I say Okay first I'm validating the order that makes sense next I'm calculating the total price got it then I'm applying the discount okay makes sense then I'm processing the payment then I'm sending the confirmation email really straightforward all I did was just read the names of the functions that we're calling and now I immediately understand what's going on I don't care about all of the little details and exactly how things are being handled and if I do I can go look at those functions but the point is in the matter of what 10 seconds I now fully understand what the point of this function is because of the way that it's written and this is really the practice you want to get into now obviously we can go read these individual functions if we want and now if I want to change maybe how the discount is being applied well I know that I'm going to go to the apply discount function I don't need to look at all these other pieces of code that I'm just not concerned with same thing with process payment same thing with calculate total price so really what I've done is I've taken this function I've split it into the different things that it's doing those independent tasks and I've put those in their own functions and then kind of combined them in this one call so the lesson here is whenever you've got these Mega functions that are doing a ton of different things see if you can split them up into logical smaller units of code that not only makes the problem easier to solve but it makes your code more readable cleaner and just easier to understand again look at how clean this function is and how quickly I can understand it now the last major mistake that I see literally every single day is forgetting to check for edge cases now when we write code as a programmer a lot of times we're optimistic we just assume that whoever's using our code is a genius they're going to know exactly how to use it they're going to pass the correct input and everything's going to work in an ideal scenario anyone who's been writing code for a little bit of time now knows that's just not the case and a lot of times your code can break in really weird ways that you've never predicted so let's look at this code right here you can see that we have a calculate average function totally simple we grab our numbers we Loop through all the numbers we add these together and then we divide divide the total by the length of our numbers any normal person would read this and go okay yeah that makes sense you know we can get a result like 20 here if we calculate the average of these numbers but what happens if the length of my list is zero in fact we can run this code and notice that I actually get an error zero division error now the reason we got that error is because we divided by the length of numbers and well the length of numbers was zero so that's fine but what happens if we do something like three and then maybe we pass in the string hello and then we run this code again again and notice that now we get unsupported oper and type for plus equals int and string okay so there are errors that can occur and even though these seems silly when you're running code thousands hundreds of thousands of times for so many different inputs really anything can happen so when you write your functions and your code you want to be really robust and make sure you're checking for all of the possible things that could go wrong before you start executing it so let's look at an example where we fix that now again this is a little bit Overkill it's more robust this function is going to work to calculate the average of any numbers now and it's going to handle any issues that could occur so you can see this first slide here what we're doing is making sure that numbers is actually a list now in Python we're working with a dynamically typed language that means that even if we add a type annotation like list here technically people can pass anything to this function that they want so it means we have to check things a little bit more than we would in a statically typed language like C++ or go or something along those lines so first I make sure numbers is actually a list if it's not not I'm going to raise an error and the reason why I'm raising an error here is because I want to tell whoever's using this function exactly what's wrong so that's another note here if there is an issue rather than just returning false or some weird ambiguous value actually raise your own custom exceptions so in this case I'm raising a type error and I'm telling them hey you you know you called this with the wrong value fix it so now they know how to fix their code great next we check if the length of numbers is zero if it is we'll just return zero because well if there's no numbers we can assume that the average is zero we could also raise a value error if we wanted to do that depends on what we want the function to be next what we do is we validate all of our numbers this is one way to do it there's many different ways we can validate this and we're going to collect num for number in numbers if is instance num is int and Float so what we're doing is removing all of the invalid instances here so anything that's non-int or a float and just keeping all of the numbers this way even if you pass a string for example but you have some other valid numbers well that's still going to work and we'll just calculate the sum or sorry the average of the valid numbers you give us so we go here and now again we make sure that the length of valid numbers is not equal to zero if it is equal to zero we simply return zero because well that's the average of zero numbers then we go here we calculate the sum we divide it by the length of our valid numbers and now this function is bulletproof it works with pretty much anything that we pass it and if we pass it some invalid input it tells us exactly what's wrong and how we can fix it so there you go those are the major mistakes and how how you fix them and I want to give a special shout out to anyone who's stuck around until the end here because I know if you've made it all the way through the video then you're someone who values education and constantly improving now if that's you and I'm pretty sure that it is I want to share with you a fantastic resource that you can take advantage of to continue improving those skills now that's brilliant the sponsor of this video brilliant is where you learn by doing with thousands of lessons in math data analysis programming and AI they adopt a first PR principles approach ensuring you understand the why behind each concept every lesson is interactive engaging you in Hands-On problem solving which is proven to be six times more effective than simply watching lectures the content is developed by top-notch Educators researchers and professionals from renowned institutions like MIT Caltech and Google brilliant emphasizes enhancing your critical thinking abilities through active problem solving rather than memorization as you learn specific subjects you're simultaneously training your mind to think more effectively consistent daily learning is crucial and Brilliant makes it effortless with their bite-sized lessons allowing you to acquire meaningful knowledge in just a few minutes each day perfect for replacing idle screen time additionally brilliant offers a comprehensive range of computer science and python courses as well as extensive AI workshops guiding you from a complete beginner to an expert through practical Hands-On lessons try everything brilliant has to offer for free for a full 30 Days by visiting brilliant.org techwithtim or click the link in the description you'll also get 20% off an annual premium subscription [Music]
Original Description
👉 To try everything Brilliant has to offer for free for a full 30 days, visit https://brilliant.org/TechWithTim . You'll also get a 20% discount on a premium subscription.
In this video, I will be showing you the 5 most common programming mistakes that I've seen made literally every single day. Every programmer has made these same mistakes at point in their programming journey and this video is going to help you fix them very quickly and ensure that you don't write code like this.
If you want to land a developer job check out my program with CourseCareers: https://techwithtim.net/dev
⏳ Timestamps ⏳
00:00 | Mistake 1
02:52 | Mistake 2
06:40 | Mistake 3
10:37 | Mistake 4
14:20 | Mistake 5
Hashtags
#coding #mistakes #programming
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Agentic Coding
View skill →Related Reads
📰
📰
📰
📰
I built a VS Code extension that reads your code aloud and narrates git diffs with AI
Dev.to · Abhishek Sharma
I Built an AI-Powered LeetCode Auto Solver Chrome Extension (Using JavaScript + LLM APIs)
Medium · AI
I Built an AI-Powered LeetCode Auto Solver Chrome Extension (Using JavaScript + LLM APIs)
Medium · JavaScript
Docker Model Runner: Run Local AI Models Like Containers
Dev.to · Anuj Tyagi
Chapters (5)
| Mistake 1
2:52
| Mistake 2
6:40
| Mistake 3
10:37
| Mistake 4
14:20
| Mistake 5
🎓
Tutor Explanation
DeepCamp AI