5 Simple Tips for Writing Clean Python Code
How do you write CLEAN Python code? Following this simple five-step process will ensure that your code works properly, and make the most of what Python has to offer. Ready to learn how to write CLEAN Python code? Let's get started! All of these tips are inspired by versi Javascript original made by Ryanmcdermott adapted to Python with a broader explanation.
Want to know how to write Python Clean code? The trick is to follow this simple five-step process, you will be sure that the code works well, is easy to read, and maximizes what Python has to offer. Are you ready to learn how? Let's get started!
Always use easy-to-remember variable names
Often when programming, we don't stop thinking about the names of descriptive variables that are easy to read, pronounce, and remember. This is a bad practice because of the time we will save at the time TIDAK Thinking of the name of a descriptive variable, we'll spend it in the future trying to understand what that variable is supposed to do.
Now let me know, which one better describes the function of a variable? ymdstror currentDate?
It's better to spend a few extra seconds when declaring a variable and choosing a good name for it, rather than wasting two times (or three times) more time in the future trying to understand what it does.
Always create an easy-to-search variable name
One thing that every programmer should know is that in our programming career, we will spend most of our time reading code and trying to understand it rather than writing it. That's why this simple tip is based on assigning names to easily searchable variables.
If we stop to read two code quotes (the good one and the bad one), we'll realize that it's much easier to quickly understand what a good code quote does before the bad one.
If we stop to read two code quotes (the good one and the bad one), we'll realize that it's much easier to quickly understand what a good code quote does before the bad one.
Always use a very descriptive name
When you send a function as an argument to another function, the only thing that causes it is that we can't follow the code correctly and we have trouble understanding the function.
With this example, we can see how we reduce regex dependencies by naming subpatterns. This makes the code easier to read and understand.
Don't use if-else too often
Many times when we write in "automatic" mode, we don't think much about the future and that at some point we may have to change that code (and before that, read and understand it), so we abuse it if-else from time to time. The more if-else, the harder it is to understand the code.
See how clean an example is. Fewer lines written, easier to understand.
Never add more context than necessary
This small (but effective) tip is based on "not repeating yourself". If the name of any class or object you have, already describes something or provides clear information about what it does, it's not good to repeat it yourself on behalf of the variable.
We can see that in this way, we don't repeat it because it's too repetitive and doesn't really add value to us. Remember that the simpler the better.