- Small Batches
- Posts
- WHY does it look so puuurrrtyy?
WHY does it look so puuurrrtyy?
[rating: beginner]
PEP-8 is Pythons code style guide. It tells you how to format class, function & variables names, use whitespace for improved (or simply consistent) readability, and many other things.
Whatâs the point?
Having uniform code style helps everyone become familiar with what reading the code should look like. There is a flow state where the code can be skimmed to quickly acquire a sense of what the code is doing, & where to find relevant information. Maintaining code is typically a combination of this flow state - quickly acquiring information & then slowing down to think carefully about specific sections. Code that is inconsistent in style breaks that flow & forces the reader to think consciously & slowly about all of it, instead of just some of it.
Code that is non-conformant can even be a hint that something needs to be investigated. For example, to a team that consistently uses style formatting, it suggests that the contributor may have been rushing and therefore more care should be taken in review to check for errors.
Generally speaking thereâs too many things involved in maintaining consistent code style for a person to remember and apply correctly all the time. By using an âoff the shelfâ formatting tool, nobody has to think about style any more - just use the tool(s) and itâs the same everywhere.
â ď¸ Note the implication that you shouldnât tweak the configuration for your own preferences as that is adding a maintenance liability. Just use the tools with their default settings [except isort
which needs to be configured to be style compatible to black
].
Using PEP-8
PEP-8 compliant formatting tools like isort
, black
& flake8
are useful & important.
A famous quote from PEP-8 you might have seen around is:
A Foolish Consistency is the Hobgoblin of Little Minds
Unfortunately this is often used to justify ignoring code style guidelines and doing your own thing. In my experience this is a poor choice.
Any project starting from scratch should make sure to be PEP-8 compliant, especially if it is going to be open source. It just means that more people are going to find the code easy to read & understand because more people are familiar with PEP-8 style, whether they realize it or not.
The more important statement from PEP-8 is this one:
do not break backwards compatibility just to comply with this PEP!
â ď¸ Legacy projects should be maintained in the naming style theyâve established. Thatâs why even Python standard libraries sometimes donât conform to PEP-8 - they existed before PEP-8 was even a thing & Python works very hard to maintain compatibility in a pragmatic & structured way.
An important thing to keep in mind is that most PEP-8 formatting tools will only adjust formatting, they wonât rename variable, function or class names because of the potential side-effects for someone using the code.
I have reformatted many projects into PEP-8 style in the past. If youâre just using isort, black & flake8, they will not change names in the code so thereâs typically a big improvement in readability while maintaining the legacy naming conventions for compatibility.
If you really want to update variable, function & class name formatting there are separate tools that will do so, with varying degrees of success - good judgement required.
So hereâs my summary:
Always use formatting tools before committing code to source control. Use tools like
pre-commit
&invoke
to facilitate that.Make it part of your CI pipeline to check that committed code is style compliant & fail the pipeline if not.
Use formatters on legacy projects provided they donât adjust variable, function or class names. Be aware that when formatting is first applied to a legacy project there will be lots of changes to the code that donât impact its function.
Variable function & lambda names should be
snake_case
âConstantsâ (a convention, not language syntax) should be
SHOUTING_CASE
. The exception is inside functions whereflake8
insists that variables & constants always be snake case.Class names should be
CamelCase
References
Just a reminderâŚYouâre probably receiving this email because you subscribed to the Small Batches newsletter. Donât want to receive it anymore? Donât file it to spam, use the unsubscribe link below! đ | Forwarded this post? Liked what you read & want more? |
I'm Russell đżď¸. A long time engineer discovering new life as a first-time solopreneur.
I build software delivery systems using Docker & Python automation that increase speed & reduce errors - saving money & improving profitability.
Reply