Good Coding Practices
Mar 9, 2023
One of Guido’s key insights is that code is read much more often than it is written. The guidelines provided here are intended to improve the readability of code and make it consistent across the wide spectrum of Python code. As PEP 20 says, “Readability counts”.
A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is the most important.
or
spending a few seconds to neatly format your code can greatly improve the legibility to future readers, making the intent of the code far clearer, and will make finding bugs easier to spot.
(have you spotted the mistake in the snippets above?)
Good coding style is like correct punctuation: you can manage without it, butitsuremakesthingseasiertoread
All style guides are fundamentally opinionated. Some decisions genuinely do make code easier to use (especially matching indenting to programming structure), but many decisions are arbitrary. The most important thing about a style guide is that it provides consistency, making code easier to write because you need to make fewer decisions.
snake_case
. Generally variables should be nouns, functions should be verbs."
instead of '
for quoting text{lintr}
+ {styler}
are your new best friends{lintr}
{lintr}
is a static code analysis tool that inspects your code (without running it){
and (
’s){styler}
{styler}
is an RStudio add in that automatically reformats your code, tidying it up to match the style guide{styler}
. You can then revert back to the staged changed if needed.{lintr}
look like?{styler}
source("filename.R)
to run the code in that fileview slides at the-strategy-unit.github.io/data_science/presentations