How To Write Cleaner Code - A Practical Example

How To Write Cleaner Code - A Practical Example

Tech With Tim

3 года назад

85,158 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Tincu Stefan Lucian
Tincu Stefan Lucian - 26.07.2023 19:59

Learn to use PyCharm not visual studio because what you did in the 8 minutes of your code will be done automatically with it.

Ответить
Keith Flick
Keith Flick - 13.05.2023 00:31

comments are useful

Ответить
DP
DP - 20.03.2023 01:33

Instead of doing all this, why not just use a linter like flake8 / black which will fix a lot of the issues you fixed manually

Ответить
Javier Padilla
Javier Padilla - 21.02.2023 20:31

What program is he using to use python? For example, is it Spyder or what is it?

Ответить
VVhite VVabbit
VVhite VVabbit - 14.01.2023 22:05

Forgot to pass the new golf_ball variable when calling the find_angle fuction.

Ответить
Talking Birb
Talking Birb - 10.01.2023 14:56

I suppose all those missing white spaces and amount of empty lines between functions could be handled with something like autopep8, black or yapf

Ответить
Octobot
Octobot - 15.11.2022 17:54

Loved the video and the resources! Thanks for sharing

Ответить
A
A - 16.09.2022 12:26

Why not use an ENUM for colours?

Ответить
Roman Dang
Roman Dang - 06.08.2022 08:18

Thanks for an explaination Tim. I like a theme you using in your vscode. Can you share a name of theme Tim?

Ответить
Frits van Doorn
Frits van Doorn - 19.06.2022 09:20

Interesting. Thank you

Ответить
スペース
スペース - 14.08.2021 06:14

The return on line 33 wasn't changed to reflect the new variable names.

Ответить
L30N74
L30N74 - 12.08.2021 21:52

You forgot to replace the (0,0,0) at line 25 with the constant BLACK you created.

Ответить
Everson Mayer
Everson Mayer - 11.08.2021 22:45

More clean code videos!

Ответить
Paul
Paul - 09.08.2021 23:10

Sorry, but the first striking thing that this code needs is comments.

You can embellish your code all you like with spacing and snake casing, but comments will make it more readable and easy to understand (and refactor).

Ответить
Adam Bright
Adam Bright - 08.08.2021 14:42

This video gives me anxiety.

Ответить
Timothy Doerr
Timothy Doerr - 05.08.2021 20:53

Beginner here. Is commenting on the code would be seen as bad coding practice? And if not necessarily, what is considered good commenting?

Ответить
Indellable Memes
Indellable Memes - 04.08.2021 14:21

Snake case for functions? That looks a bit ugly.

Ответить
Awwab Asad
Awwab Asad - 27.07.2021 15:35

Hey this is great, your code is A++, but another thing to make your code even better is to add a little more comments throughout your exceptional code!😎😇👍✌️😸

Ответить
Trillionth Cousin of a Stone
Trillionth Cousin of a Stone - 25.07.2021 06:23

I will never leave camel and pascal case

Ответить
Eva Giacomello
Eva Giacomello - 21.07.2021 13:41

This is on a whole new level of programming asmr

Ответить
Adarsh Saurabh
Adarsh Saurabh - 20.07.2021 21:36

You just made it harder for a begineer to understand 😂

Ответить
LowRhyan567
LowRhyan567 - 20.07.2021 07:10

If you use Python then your code already isn't clean, because Python have a lot of useless things on their binaries making the code heavy with a very poor performance.

Ответить
Soup Noodles
Soup Noodles - 19.07.2021 17:35

For most of these you couda just ran this in pycharm and it would have listed out all the format errors ;-;

Ответить
Damian Zaręba
Damian Zaręba - 19.07.2021 16:28

It's better most of the times to use try/except, because of "easier to ask for forgivness than for permission" or something it was going around :)

Ответить
fredericoamigo
fredericoamigo - 19.07.2021 01:44

Super helpful vid!

Ответить
Michiel Boswijk
Michiel Boswijk - 16.07.2021 21:31

Might be fun to do a type of competition where you ask your viewers to refactor a piece of code like that.
Then we could all learn from the best out of those.

Ответить
micheal hall
micheal hall - 16.07.2021 05:47

More things

Always add comments to your methods functions

ball_path should be called path because you are repeating yourself when calling it

Win, pos, vec are not obvious you need to state what these are and how they are used

Your try block should have except Exception as e: print(e) so you know when an error happens

golf_ball could be just ball, as you don't have any other types of balls on the field.

Always copy and paste very names to avoid mistakes Tim made

Always make a backup copy of the file before you refacter

Add comments to complex logic so you can just read why its happening rather than how it happening

run variable is uneeded as you are only using it once

Add comments at the top to what this program does, potential issue and future ideas and also add links to common documents like pygame docs

Your continue block was unneeded and will cause issues later when you have other mouse actions

Add type checks using assert as a sanity check to make sure everything is the excepted type of thing. Especially using tuple deconstruction you will have add errors if pos happened to have a length of three for example.

I typically write all of the function names in the document at the top of the page in a comment so I know what I'm dealing with when I open the project after a long time.

Pretend you are writing the code for a future more retarded version of yourself.

Ответить
Max Poppe
Max Poppe - 16.07.2021 03:56

even though you forgot like 5 things, you made your point clear and that's what this video is about, so great video

Maybe you could've pointed out that making comments is good practice, also maybe could've said that if you've a lot of and's that you can put them below each other instead of next to eachother and maybe try and avoid if statements, they're not efficiënt and also in a lot of situations not the cleanest solution.

Ответить
Muhammad Mustafa
Muhammad Mustafa - 16.07.2021 02:08

good stuff tim !

Ответить
ruben cid
ruben cid - 15.07.2021 00:34

One question: in the main loop, don't you need to see first events then movement and visualization?

Ответить
Paul O'Sullivan
Paul O'Sullivan - 14.07.2021 23:36

I am not so bothered by convention but I totally agree with being consistent whatever you decide to use. I would also prefer to have separate functions for the various code blocks personally but as long as others and of course you can understand your code when you have to revisit it to make changes months or even years later that's what matters most.

Ответить
R
R - 14.07.2021 19:29

thanks bro 👍🙂

Ответить
TCG vs OCG
TCG vs OCG - 14.07.2021 17:33

Well i just finish 12 hour video when is the new one?

Ответить
WGallo
WGallo - 14.07.2021 16:42

Do you think you could help me with a pycharm opencv project? im not a coder and the code is ready but I dont know how to put it together to run it...

Ответить
Cookie
Cookie - 14.07.2021 15:57

I'm currently working on a puzzle game, which users can grab the element pieces to fill in a grid.
Using the Tetris method to create grid and interface, so far so good, but I cant find any tutorial where I can grab an object (NOT an image) on screen.
How can I drag elements that is in pygame?

Ответить
Mark Madhukar
Mark Madhukar - 14.07.2021 14:44

Gr8 vid as usual : )

Ответить
Mayur S.
Mayur S. - 14.07.2021 11:13

If I have a single line after an if statement I write it as:

greeting = 1
if (greeting) : print("Hello")
else: print("Bye")

Is this a good practice?

Ответить
Itay Chechik
Itay Chechik - 14.07.2021 10:34

The try-except case is a really good one to demonstrate the importance of clean code - had you used a more specific exception (ZeroDivisionError), you would have easily known what the error could be there, without having to remember trigonometry! :)
In my opinion the point there might have been slightly missed - it isn't about sticking to best practices with an if-else statement, it's about making the code easier to handle, especially when returning to it after several years
Anyway great vid, love the uploads lately :)

Ответить
ARMSJARB C
ARMSJARB C - 14.07.2021 08:02

Keep up the good work Mr. Tim

Ответить
Harsh Prajapati
Harsh Prajapati - 14.07.2021 07:41

Loved this man! Thanks🙌

Ответить
Revolutionizer
Revolutionizer - 14.07.2021 07:15

fabulous

Ответить
Bina Prajapati
Bina Prajapati - 14.07.2021 04:59

And why using atom here?

Ответить
Soljarag5
Soljarag5 - 14.07.2021 04:47

I like camel case way better.... Not sure why python decided to use shake

Ответить
tarte empion
tarte empion - 14.07.2021 03:07

You should use atan2 in your find_angle method, instead of redefining it ;-)

Ответить
ItsAhmd
ItsAhmd - 14.07.2021 02:53

You're like telling me i have clean code, Thanks man Lol

Ответить
The0Koller
The0Koller - 14.07.2021 02:14

Wonderful video, I wonder if you can discuss a better way to address "if elif" and "if if" blocks

Ответить
BGxBlagiw
BGxBlagiw - 14.07.2021 01:55

Dude you are gold for this sphere very very well explained 👍👍 for the young guys don't worry your brain can't take too much unless you are giny who is rare, but who knows anyway gg Timaty 🙃✌

Ответить