Slluxx's beginners guide to write better (python) code

Slluxx's beginners guide to write better (python) code
Photo by Hitesh Choudhary / Unsplash

Sometimes i get asked to review code by beginner dev's that doesn't quite work for some reason. These scripts or snippets usually have a lot in common:
They look bad.

A "good looking" code helps tremendously to increase readability and debuggability. It gives structure, a well-defined flow and just makes it easy to understand for you, as well as for others. You might know what it does right now but will you also when you come back to this project in a month or a year?

Keep your code as little indented as possible.
The more your code indents to the right the more you can be sure that it's against good practice. Obviously, you have to use indentations and sometimes its unavoidable to have some more. But there are several things you can do to keep your code "as left as possible".

As you can see, the following code is pretty messy. It was sent to me to take a look at because the developer had issues with it (which are not in this snippet and i really want to spare you the rest of the code).

Don't use nested if statements.
Do you know what it does? No? Well, me neither. But that's not important.
Let's try to untangle the nested if statements. In the example code it works like this: "if x then do", however you should rather break out of the code if something doesn't fit your needs instead of simply going on, which roughly translates to "if not x then stop". Applying this principle to the code, we get this:

This is so much better already, but it can be optimized even further. For example, "config.json" is being loaded 3 times. Apart from that you should not use json files as storage (due to race conditions and thus corrupted files), its more efficient to load it once and store it in a variable.

Try to avoid loops.
If we inspect the code closely, we can see that the code loops over the array with the variable name "week_services". On each iteration, the content of the current index is compared to the argument "service_name". Basically checking if it exists and continuing with it if it does. However, we can do this much simpler, completely without a loop whatsoever.

Create "codeblocks" with space between them.
Line 11 shows how you can check if an element exists (or does not exist) in an array. Since we have control over the array content (line 10), we can write it in lowercase letters. If we then convert the service_name variable to lowercase too, we can completely remove an entire if-block from the code!

As you can see, the code looks much cleaner, is much more readable and you can actually figure out what each block of code does without getting an aneurysm. Its less code to write, although we use slightly more lines due to the spaces in-between the blocks.

Thats it! These simple tricks help you to write better code. Of course, there is more to learn and to optimize, but you are good to go if you remember to implement your new know-how.