13 Tip Clean Code in Python (Variables, Functions)
13 Tip Clean Code in Python (Variables, Functions)
Clean code
Variables
Functions
Targets Python3.7+
@miladkoohi
1 Variables
Use meaningful and pronounceable variable names
Additionally, there's no need to add the type of the variable (str) to its name.
2 Variables
Use the same vocabulary for the same type of variable
n better:
Eve
2 Variables
Even better Python is (also) an object oriented programming
language. If it makes sense, package the functions together with
the concrete implementation of the entity in your code, as
instance attributes, property methods, or methods:
3 Variables
Use searchable names
We will read more code than we will ever write. It's important that
the code we do write is readable and searchable. By not naming
variables that end up being meaningful for understanding our
program, we hurt our readers. Make your names searchable.
4 Variables
Use explanatory variables
Good:
Decrease dependence on regex by naming subpatterns.
5 Variables
Avoid Mental Mapping
Don’t force the reader of your code to translate what the variable
means. Explicit is better than implicit.
6 Variables
Don't add unneeded context
... when you can specify a default argument instead? This also
makes it clear that you are expecting a string as the argument.
8 Functions
Functions should do one thing
9 Functions
Function arguments (2 or fewer ideally)
A large amount of parameters is usually the sign that a function is
doing too much (has more than one responsibility). Try to
decompose it into smaller functions having a reduced set of
parameters, ideally less than three.
The main point is to avoid common pitfalls like sharing state between
objects without any structure, using mutable data types that can be
written to by anything, or using an instance of a class, and not
centralizing where your side effects occur. If you can do this, you will be
happier than the vast majority of other programmers.
13 Functions
Avoid side effects
im milad koohi
Senior Software Developer
if like repost
@miladkoohi