5 Tips for Writing Clean Python Code

preview_player
Показать описание

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.

🔖 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
Рекомендации по теме
Комментарии
Автор

Join my free Code Diagnosis workshop that teaches you a 3-factor framework using Python examples from production code ➡ arjan.codes/diagnosis

ArjanCodes
Автор

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.

markoobradovic
Автор

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…

royw
Автор

Here is a man who appreciated the value of functional programming through experience.

StavrosSachtouris
Автор

I get that you are trying a more functional approach, but that invoice is 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.

FernandoCordeiroDr
Автор

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?

marnz
Автор

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.

malteplath
Автор

Raise errors is great! But what about handling?

ziond
Автор

YOUR AUDIO REVERBERATIONS ARE UNIQUE AND UNCOMFORTABLE BTW !!!

mitz
Автор

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 =)

Melindrea
Автор

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.

Whatthetrash
Автор

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.

JonitoFischer
Автор

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.

marcotroster
Автор

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

ilbene
Автор

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.

ZachAttack
Автор

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.

metal
Автор

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

loic
Автор

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.

mmilerngruppe
Автор

What about error handling? As in how you deal with them in after you have raised them?

MoreCharactersThanNeeded
Автор

as always, thanks alot Arjan! I am still wondering how you would handle errors in each function. When there is a need of chaining a long list of task you have to do each method must raise some kinde of custom processing exception which should be handled in the main, or what is your approach? I´ve looked at taskflow and pypeln which should offer some kind of task processing framework. Maybe that would be nice for a next video to compare some workflow libs. What are you using?

timelschner