We originally sent the following 10 tips to our Friends List and we received requests to post it here for reference, so here you go ...
Ever wondered what you could learn from a code review?
Here are some things we picked up from code reviews that when addressed can make your code a lot cleaner:
Break long functions (methods) into multiple smaller ones - this will make your code more reusable and easier to test.
Remember each function should do only one thing. Example: a function that parses a csv file, builds up a result list and prints the results does 3 things and should be split accordingly.
Move magic numbers sprinkled in your code, to constants (at the top of your module) - again easier to reuse, more readable, less surprises later on.
Watch out for anything that you put in the global scope, localize variables (data) as much as possible and you'll have less unexpected side consequences.
black) - more consistent, (PEP8 compliant code) is easier to read and earns you more respect from fellow developers (also remember: "how you do the small things determines how you do the big things" - very true with software development).
This goes back to developers writing code not only for machines, but also (and more importantly) for other developers. E.g.: really long lines might annoy your colleagues that use vsplit to look at multiple code files at once.
try/except blocks narrow (ask yourself: "Are all those lines in between really going to throw this exception?!") and avoid bare exceptions or just using pass or reraising an exception without additional error handling code (e.g. at least log the error).
Leverage the Python language (Pythonic code) - for example replace a
try/finally with a
with statement, don't overly check conditions (leaping), just
try/except (ask for forgiveness). Here is a great article on this topic: Idiomatic Python: EAFP versus LBYL.
Another example is relying on Python's concept of truthiness (e.g. just do
if my_list instead of
if len(my_list) > 0).
Use the right data structure - if you check for membership in a big collection it's often better to use a
set over a
list which would be scanned sequentially and is therefor slower.
Leverage the Standard Library - you don't have to reinvent the wheel.
For example if you have a
collections.Counter object you don't need to use max on it, you can use its
most_common method. Counting values manually? You can use sum that receives an iterable. The
any builtins are wonderful. Or for more complex operations,
itertools is an excellent module.
if-elif-elif-elif-elif-else's are quite ugly and hard to maintain. You can beautifully refactor those using dictionaries (mappings) - less lines of code, easier to maintain.
Flat is better than nested (Zen of Python. Go send
import this to your printer now and keep it handy) - closely related to number 1., but worth emphasizing: if you have a
for in a
for, and the inner for has a bunch of nested ifs, it's time to rethink what you are trying to do. This code will be very hard to test and maintain in the future.
Hope that helps! What cool tips have you learned from going through code reviews? Comment below ...
Keep calm and code in Python!
Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.
"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H
"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B
"This is now my favourite first Python go-to reference." - Anthony L
"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S