DEV Community

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

Collapse
jmccabe profile image
John McCabe • Edited on

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
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
svschoenherr profile image
Sven Schönherr • Edited on

For loop should also not be indented...

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
a5537 profile image
A5537 • Edited on

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.