5 Tips for Writing Clean Python Code

21,844
0
Publicado 2024-06-14
In this video, I'll share 5 key things you should do to avoid writing crappy code. These tips, drawn from my own experience, will help you write clean code.

🔥 GitHub Repository: git.arjan.codes/2024/clean_code

👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis.

📨 The Friday Loop by ArjanCodes Newsletter: thefridayloop.com/

💻 ArjanCodes Blog: www.arjancodes.com/blog

✍🏻 Take a quiz on this topic: www.learntail.com/quiz/ikcbog

🎓 Courses:
The Software Designer Mindset: www.arjancodes.com/courses/tsdm
The Software Architect Mindset: www.arjancodes.com/courses/tsam
Next Level Python: Become a Python Expert: www.arjancodes.com/courses/nlp
The 30-Day Design Challenge: www.arjancodes.com/courses/30ddc

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

Social channels:
💬 Discord: discord.arjan.codes/
🐦 X: x.com/arjancodes
🌍 LinkedIn: www.linkedin.com/company/arjancodes
🕵 Facebook: www.facebook.com/arjancodes
📱 Instagram: www.instagram.com/arjancodes
♪ Tiktok: www.tiktok.com/@arjancodes

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Dale Hagglund
- Kit Hygh
- Alexander Milden
- Bean

🎥 Video edited by Mark Bacskai: www.instagram.com/bacskaimark

🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes

🔖 Chapters:
0:00 Intro
0:30 Example
3:57 #1 Meaningful Names
6:48 #2 Comment Wisely
8:38 #3 Functions/Modules Should Be Responsible For One Thing
13:49 #4 Error Handling as Logic
15:54 BONUS: Formatting
16:29 #5 Become a domain expert without becoming a domain expert
25:02 Final thoughts

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free co

Todos los comentarios (21)
  • I get that you are trying a more functional approach, but that invoice is beeeeeeegging to become an object. When you keep passing the same arguments to a lot of functions, this is a sign there's an object structure hidden in your code. You create a factory classmethod "create_from_stripe", a "send" method, a "book" method, and badabim-badabum, you're golden. That's actually the remaining tip Uncle Bob gives about clean code: keep the number function arguments to a minimum. The strategies he suggests are to rely more on data structures and on objects, which is, in my view, pretty much the same strategy.
  • @royw2
    When refactoring, I like a safety net. Before refactoring, run unit tests with coverage to verify the area I’m refactoring has unit tests, adding any that are missing. Then the process is test, refactor, test. Just want to know I’m not adding any errors…
  • Here is a man who appreciated the value of functional programming through experience.
  • @zion4d
    Raise errors is great! But what about handling?
  • @marnz550
    Good stuff, thanks Arjan. I have a feeling you only scratched the surface on error handling. In the example you gave, the code became neater but much more vulnerable. Now if one of your functions raises an exception, the whole code breaks. Would you plz explain the full cycle of your error handling logic?
  • @Melindrea
    You accidentally divided the application fee by 100 twice. Both in the InvoiceData (which is where it was originally) and in the get_application_fee function. I did like the changes you did in general. It makes a lot of sense =)
  • Arjan, you are soo good at what you‘re doing. Your voice is calm and relaxed. And you are able to transport your knowledge to your audience in the same manner, calm and relaxed. Learning new stuff suddenly becomes like a therapy. No more stress, no more struggle. Thank you very much. Please keep going.
  • @malteplath
    This is one of your best videos. You use your own production code, you explain your thought process clearly, and also hint at further improvements. This could be expanded into a whole course - especially the last tip.
  • Much better now, but it's still far from ideal. The last point about domain knowledge is crucial because otherwise you can hardly figure out the right data structures and function signatures. It's all about using expressive language and increasing information content while keeping the coupling low. I'd probably remove most of your comments and express them by function names. But that's just my personal taste. Also a huge issue with the code is what happens when a transaction completes partially. Maybe you wanna roll back the payment entirely when it's not persisted in your accounting system, etc. You should make this more explicit. But in any case, it's also important to not overengineer. When an occasional manual rollback is cheaper than programming bulletproof error handling, then keep the code as is. After all, the code is supposed to save money by automating a manual process.
  • @Whatthetrash
    Something that's never discussed in videos like this (although this is a good video, not throwing shade at all, Arjan) is the overhead of learning all these new APIs and until you really grok what they're doing, what problems they're solving and how they work -- there really is a wall of ignorance that's hard to overcome. After all, you don't know what you dont know. All I know is , "Use this to solve your problem" but beyond that -- it really does seem like an endless (turtles all the way down) problem of "How much of this new thing do I have to learn to solve my problem?" Is this basic, fundamental knowledge thst. I should know, or concrete implementation minutia that doesnt matter beyond this API. All of that is where I find myself way more often than not knowing how to use clear variable names and short functions. >_< Edit: Hey, you just mentioned it! Becoming a domain expert without becoming a domain expert. That's the rub right there.
  • @JonitoFischer
    Thank you Arjan for the videos you upload, they're very helpful and have a great quality, both in teaching and technical quality, I have learned a lot from you.
  • Been watching for a while and this is the best video yet. I’m going to link to it from our coding standards document, as it provides good examples.
  • @loic1665
    Very nice video, thanks! It reminds me of older videos like the ones about dependency I jextions or design patterns:) Also very nice to talk about domain knowledge. From my experience it is that who makes the difference at some point, even if you're technically good. It's only by knowing what you're working on and your clients' needs that you'll be able to make good decisions
  • @ilbene7922
    Very interesting, I would like to see a video about how to read code and analyze it in order to catch all these mistakes. In this video you show the problem and tthe solution but now how do you catch the problem
  • @metal571
    By the way, VSCode's Python extension now allows you to "extract function" after selecting a block of code, although it doesn't seem to add type hints along with it.
  • @mmilerngruppe
    15:40 but you make no exception logic, you simple ignore your new exceptions. 23:40 but your function does now not do what it names stands for.
  • @tbpotn
    I feel like most of the comments are useless. Stuff like #retrieve the customer from stripe and # determine the application fee. Or # retrieve payment intents. None of these add to the code that's below it. Also, i think compute_timestamp immediately violates naming descriptively. compute_cutoff_timestamp would be better imo. Those are the two things that i noticed most.