Published September 14, 2021 | https://doi.org/10.59350/n8m8s-1t122

How to Code with Me - Beyond Linters

Creators & Contributors

  • 1. ROR icon Harvard University

This post is about my personal code style guide that are beyond the enforcement of my flake8 plugins or black. I'll try and update it over time.

Exit code blocks as early as possible

Sometimes you have an if statement that does some stuff when the conditional is true, or raises an error if it isn't. Here's the wrong way to write this:

if cond:
    ...  # big code block
else:
    raise ValueError

It's better to raise the value error as quickly as possible, because then you can dedent the big code block. This is particularly good if you have lots of nested code blocks in conditionals, loops, etc.

if not cond:
    raise ValueError

...  # big code block

The same thing is true for return statements. The following code where a value is set then returned is bad:

if cond:
    ... # big code block
    value = ... # final line that assigns value
else:
    value = ...  # one-liner assigning value
return value

It's better to flip the conditional and return the value quickly, since it only takes one line to assign it in the else block

if not cond:
    return ...  # one-liner assigning value

... # big code block
return ... # final line that assigns value

Additional details

Description

This post is about my personal code style guide that are beyond the enforcement of my flake8 plugins or black. I'll try and update it over time.

Identifiers

GUID
https://cthoyt.com/2021/09/13/beyond-linters
URL
https://cthoyt.com/2021/09/13/beyond-linters.html

Dates

Issued
2021-09-14T01:18:00
Updated
2021-09-14T01:18:00