DEV Community

Cover image for 10 must-know patterns for writing clean code with Python🐍

10 must-know patterns for writing clean code with Python🐍

Alex Omeyer on April 05, 2022

Python is one of the most elegant and clean programming languages, yet having a beautiful and clean syntax is not the same as writing clean code. ...
Collapse
 
jmccabe profile image
John McCabe • Edited

This is a pretty poor example of clear variable names (see Naming Conventions #2 above)

# Recommended
cities = [“UK”, “USA”, “UAE”]
    for city in cities:
        print(city)
Enter fullscreen mode Exit fullscreen mode

The variable name's purpose would be clearer if it were 'countries', not 'cities'!

Collapse
 
svschoenherr profile image
Sven Schönherr • Edited

For loop should also not be indented...

Collapse
 
a5537 profile image
A5537 • Edited

Maybe this indent decided to leave the print statement (where it is destined to be)

for x in c:
print(x)

# Recommended
cities = [“UK”, “USA”, “UAE”]
    for city in cities:
        print(city)
Enter fullscreen mode Exit fullscreen mode

and moved in with that for loop neighbor against all odds.

Seriously, indent really matters in Python. As well as the case (especially for keywords), so Pass instead of pass

# Recommended
def fetch_users(): 
    # do something
    Pass
Enter fullscreen mode Exit fullscreen mode

will trigger a NameError unless there is a variable named Pass.

And speaking of technical debt, these two kinds of bugs (wrong indentation and case incosistency) in poorly written Python code are sneaky enough to go unnoticed since they may not trigger any error messages in some cases.

Thread Thread
 
potasiak profile image
Sebastian Potasiak

Also, the quotation marks in the cities list are not " (\x22) but (\u201C) and (\u201D) unicode characters. It would raise SyntaxError.

Collapse
 
jmccabe profile image
John McCabe

There's always a possibility that's an issue with the page formatter for code. I know Confluence can be a bit funny about that if you paste into a code block.

Collapse
 
philman profile image
Phillip Jones

Whether on purpose or not, it is a clear example of why it's important to give clear names. If this was a bug, I could clearly find it just by looking at the code.

Collapse
 
jmccabe profile image
John McCabe

It's also a relatively good example of why compiled languages are so (much more) useful :-)

Collapse
 
koljaoh profile image
koljaoh

Is using flags really that bad? Especially for large complex functions? Not using them would mean that I need two (large) functions instead of having one simple switch. I would imagine this often leads to problems because stuff is fixed inside one function but not the other.

Collapse
 
potasiak profile image
Sebastian Potasiak

Yeah, there are cases when it should be done and the cases where it should not. The example in the article is an obvious example when it should not be used though.

The issue is with description of the case. It should've say something along the lines of "boolean flags in function arguments should not change the function's core logic, only its side effects".

Collapse
 
bluemont profile image
Bluemont

Agreed. It’s easily avoided in the article’s example, a one-line function. But flags can help us write DRY code for more complex functions.

Collapse
 
mhe931 profile image
mhe931

Thank you so much for your post. It helps a lot.

p.s: dices have 6 sides I think!!

import random

# Not recommended
def roll_dice():
    return random.randint(0, 4)  # what is 4 supposed to represent?

# Recommended
DICE_SIDES = 4

def roll_dice():
    return random.randint(0, DICE_SIDES)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
drystanfuror profile image
Drystan-Furor

Ever played ttrpg’s?
Dices can have
4, 6, 8, 10, 12, 20, and 100 sides.
Polyhedral dice sets. So to me it did make sense.

Collapse
 
dangermanva profile image
Dan

“Dices” is not a word. Dice is already plural, referring to more than one cube. Die is the word to represent one cube.

Btw Good article, the point was well made.

Collapse
 
md2perpe profile image
Per Persson

I consider the name Las_name confusing. Is it supposed to be last_name or does Las mean something else? (In Sweden, LAS is a law about employment.)

Collapse
 
victorsgb profile image
victorsgb

I think it was a typo

Collapse
 
svschoenherr profile image
Sven Schönherr

There are far too many typos for an article about clean code.

Collapse
 
justmedev profile image
justmedev

the fetch and display function is totally fine by itself. It only makes sense to separate them when you need to only display or only fetch the users separately

Collapse
 
albertosesena profile image
Alberto Seseña

I found interesting this post. Some fundamentals but important points are present here. Good job!!!

Collapse
 
humblegorilla profile image
humblegorilla

Cool

Collapse
 
timhuang profile image
Timothy Huang

Keeping codes clean and easy to read is very important not only Python but also other languages. This is a very useful tips for developers to maintain their codes. Thanks for sharing.

Collapse
 
akindeled profile image
Akindeled

Thank you for this piece...

Collapse
 
devsouvik profile image
Souvik Guria

No one said anything about using comprehensions?