DON’T Make These 5 Coding Mistakes…
Key Takeaways
The video discusses common coding mistakes in Python, providing guidance on how to identify and fix them, with a focus on best practices for software development.
Full Transcript
here are five dumb mistakes that wreck your code that unfortunately most of you are making not only will I show them to you but I'll also show you how to fix them in just a few seconds so stick around mistake number one is lack of consistency have a look at this code right here pause the video and take a guess at what I'm going to say is wrong with it some of them are obvious some of them not so much now if you haven't paused the video please go ahead and do that I'm going to run through a lot of the mistakes that you can see that might be a little bit subtle especially to a more beginner program let's start by having a look at the top of our code here where we have these two variables message one and message two now they seem Innocent but notice that we're using double quotation marks and single quotation marks now in a hobby project this is completely fine but when you get into larger code bases and working with other people you want to make sure you're always using the same type of quotation marks or the same type of variable names it doesn't really matter what you decide so long as you're always consistent inconsistency just makes really bad code it makes makes it hard to predict and it's something that we can easily avoid continuing let's look at this variable first of all notice that we have this user profile and we're using camel case now in Python you really should be using snake case like this anyways that's one inconsistency the other is with the different field names that we have inside of our dictionary notice that we have camel case we have snake case and then we have Pascal case we really should be using the same type throughout all of our different field names to make it more predictable on what the field names will be continuing we have a class here now notice in the class we have some inconsistency with our spacing we have no space separating these two methods but we have a space separating this one so what do we want to do simply add a space or add an indentation level quite simple okay let's go down here to our DB manager now this is completely fine we have a DB manager variable we call our connect method and then notice that we actually use the class to invoke this class method or this static method sorry and then we use the instance to invoke the static method again now this doesn't really make a lot of sense and again is an inconsistency we have a static method we can call it from either the class itself or from an instance in this case we're doing both one after another we really should just stick to one way I'd probably prefer to use the actual class itself so database manager but it doesn't matter what you want to do so long as you do it the same each time you do it continuing we have some variables X and Y now notice that we don't have a space between the equal sign we can really easily clean that up by adding it there next we have two constants one of which has a capital one of which is not in capitals so again doesn't matter what you decide to do here just do it the same every single time so the code is more predictable lastly we have a function here inside of the function we're returning two different types here we're returning an object here where we're returning a string this makes it more difficult to work with the return type and again it's not consistent or predictable now we get into mistake number two which is not registering for my free software development course which teaches you everything you need to get a software development job making 80k per year with no degree or experience required seriously this is completely free you can sign up for it from the link in the description it's about an hour long and breaks down everything you need to do completely from the beginning all of the things you need to learn the curriculum you need to follow and how you can break into the tech industry register from the link in the description completely for free seriously though that leads me into mistake number three which is this code here before I reveal take a second pause the video and see if you can tell me what's wrong with this code okay I assume that you've paused the video when we have a look at this code here we can see that we have hardcoded values now notice this is just a simple game if I actually were to run this code let's go here and run it python example 2. piy you can see that we can just move around and it's a little square on the screen okay a simple game in Python however the issue with this is that we have all of the different values in our game hardcoded in for example we are setting the width and the height of the game by just putting in 800 and 600 we're determining the position of the player by dividing the width which is 800 and the height which is 600 notice that we use these values consistently throughout the program 600 here 800 here we now have a value like 50 you might not actually know what that is right away but this is actually the width of the player or the size of the player you can see we use that down here when we're actually drawing the player we have a color this is actually white which we're filling the screen with again you might not know that because we don't have it stored in a variable we have another color which is the color of the player we've got a bunch of hardcoded values even things like five here which is the velocity of the player so notice that it's very difficult for me at first glance to understand what all of these values are and if I want to change anything like the width of the screen the height of the screen the speed of the player I've got to change this in multiple places now this is a very simple example where we can get away with this but imagine we had code that was thousands of lines long and we were consistently using these values well it would be an absolute nightmare to change them to find them and to understand what they're doing so mistake number three here is using hardcoded values let's go to the fixed version of this code and you'll see how much better it looks so here's the fixed version notice at the beginning of the program I Define all of my constant values which previously were hardcoded first of all I now know what all of these values actually are it's very easy for me to change them in one location and as I read through the code it immediately becomes a lot more readable and easier for me to understand I now know what the player color is the background color I know that I have my height minus my player size I know the velocity is this value right here I know I'm dividing the width by two the height by two I know this is the width and the height I have my FPS like my frames per second it's just much clearer easier to understand and obviously something you should do in your code now we move to mistake number four again pause the video see if you can tell me what the mistake is well looking at this code here we can see that we have a bunch of different kind of conditions or criteria right we have income is student has coupon has membership Etc we then have a very complicated if statement which aims to tell us if a student is El eligible sorry or not eligible for a certain offer criteria whatever it may be now I challenge you read this and tell me what the criteria actually is for a student to be eligible now you probably can do that but it's going to take you a minute or two you're going to have to really look through this code it's a bit difficult to actually understand the reason for that is we're using a very complex Boolean expression that has a bunch of ands a bunch of ores a bunch of chain conditionals and it's just very confusing now it's really easy for us to clean this up by simply taking some of these more complex conditions and putting them inside of subconditions or variables that store a part of the condition let me show you what I mean so now have a look at this code it achieves the exact same thing as the previous code except it's much easier to understand now we've been very verbos here just for the purpose of the example but you can see that what I've done is broken all of the different conditions into variables that describe what they are for example are we an adult well that's the age greater than 18 are we High income that's the income being greater than 60,000 are we medium income well that's the income between 40 and 60,000 are we an eligible student you get the idea now some of these seem obvious we're writing them in a way where it's very clear to understand and any anyone reading our code would have no doubt what it's doing now to go a step further we have some more complex conditions right like eligible income well the eligible income is if we have a high income or this specific condition is true so we're kind of taking this condition and breaking it into smaller and smaller pieces so it's really easy to combine it together and understand what it's doing eventually we get down to the eligible offer and we can now read exactly what the eligibility criteria is it's if we're an adult we have eligible income and we have an eligible coupon we can understand that in a matter of seconds versus minutes with the previous code so moral of the story here is always break your complex conditions into smaller subconditions and then combine those with the variables that you have now we move on to the next dumb mistake which is a super simple one and this is unused Imports guys just remove your unused Imports if you're not using something don't have it in the code it makes it very confusing when you have a lot of imports or functions or parts of the code that aren't being used whether it's a variable class function import doesn't matter remove it if it's not being used you can always add it back you can always go and look at the G history you can comment it out if you really need to but preferably just completely remove it in this case you can see we're not using system we're not using radians we're not using random we're not using time if I were reading this code I might think that we're doing some more complex stuff that we're really not doing because we have all these Imports that aren't being used fortunately the IDE is bluring them out just to give you a quick example if we go here and we see what it looks like without the unused Imports a lot cleaner a lot easier to understand and just better code moving on to our last example we have unnecessary nesting have a look at this code here which is something that many of you have probably produced in your life often times we want to write complex conditions or we have a lot of different things to check what we end up doing is getting this huge chain of if statements it could be for Loops it could be many other things it could be functions classes whatever we end up getting really deep levels of nesting now it's very difficult to read this code maintain this code and change anything inside of it and it's really just unnecessary we don't need to write code in this fashion you can see that we're checking if data well if we have if data then we need to check if the name is in the data well if the name is in the data we need to check if we have a name then we need to check if it's Alpha then we need to check if it's in the right length you get the idea many beginner programmers think this is the only way to write code but let me show you an alternative style that removes all of these levels of nesting that make your code really complex and hard to understand let's pop this open and notice that I have the exact same code that achieves the exact same thing except I have no levels of nesting or very few in this instance now the way we can achieve that is by changing the way in which we think about checking conditions rather than checking if everything is true in that kind of chained nested statement we instead check all of the things that could be false or would result in us not reaching the condition or the end goal and then returning or exiting from the function so this case we say if not data then we simply return right we say okay no data was provided return next we get the name if we don't have a name we say the name is empty and we return so notice I'm checking all of the things that could be wrong and then if all these things are not the case that means I actually have the valid condition and I can go ahead and print out the valid name so this is just the opposite way of doing things right so you're checking it in the reverse order this allows you to exit the function early whenever you run into some potential issue and you don't have to have these huge levels of nesting I know this is a simple example obviously there can be more complex cases but I just wanted to show you that there's a different way of writing your code that removes those levels of nesting and whenever you find yourself having two three four five levels of indentation or nesting think about how you're writing it and if you can write it in another way that will make it more clear concise and have less nesting anyways that's going to wrap up this video don't forget to sign up for my free software development introduction course from the link in the description I hope you guys enjoyed if you did leave a like subscribe and I will see you in the next [Music] one
Original Description
Register for my FREE introduction to software development course to learn how to land a developer job as fast as possible! https://techwithtim.net/dev
Most of you are making at least 1 of these 5 dumb coding mistakes. Today in this video I am going to show you what they are, why you should fix them, and how you can fix them.
🎓 Premium Courses 🎓
🏢 CourseCareers - https://techwithtim.net/dev
🔗 BlockchainExpert - https://algoexpert.io/blockchain (use code “tim”)
💻 ProgrammingExpert - https://programmingexpert.io/tim (use code “tim”)
🎓 Free Courses 🎓
📚 Introduction To Software Development: https://techwithtim.net/dev
⏳ Timestamps ⏳
00:00 | Introduction
00:10 | Mistake #1
02:57 | Mistake #2
03:38 | Mistake #3
05:57 | Mistake #4
08:11 | Mistake #5
09:04 | Mistake #6
🔗 Socials 🔗
📸 Instagram - https://www.instagram.com/tech_with_tim
🐦 Twitter - https://twitter.com/TechWithTimm
💬 Discord - https://discord.gg/twt
🤝 LinkedIn - https://www.linkedin.com/in/tim-ruscica-82631b179/
🌐 Website - https://techwithtim.net
💾 GitHub - https://github.com/techwithtim
🔗 Support 🔗
👕 Merch - https://teespring.com/stores/tech-with-tim-merch-shop
💵 Donations - https://www.paypal.com/donate?hosted_button_id=CU9FV329ADNT8
🙏 Patreon - https://www.patreon.com/techwithtim
🔖 Tags 🔖
- Python debugging tutorial
- Python coding errors
- Common Python mistakes
Hashtags
#techwithtim
#coding
#pythonprogramming
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: AI-Assisted Code Review
View skill →Related Reads
Chapters (7)
| Introduction
0:10
| Mistake #1
2:57
| Mistake #2
3:38
| Mistake #3
5:57
| Mistake #4
8:11
| Mistake #5
9:04
| Mistake #6
🎓
Tutor Explanation
DeepCamp AI