Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

13 Tip

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

Good: If the entity is the same, you should be consistent in referring to


it in your functions:

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

If your class/object name tells you something, don't repeat that in


your variable name.
7 Variables
Use default arguments instead of short circuiting or conditionals

... 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.

If the function has a single responsibility, consider if you can


bundle some or all parameters into a specialized object that will be
passed as an argument to the function. These parameters might
be attributes of a single entity that you can represent with a
dedicated data structure. You may also be able to reuse this entity
elsewhere in your program. The reason why this is a better
arrangement is than having multiple parameters is that we may be
able to move some computations, done with those parameters
inside the function, into methods belonging to the new object,
therefore reducing the complexity of the function.
9 Functions
Function arguments (2 or fewer ideally)
9 Functions
Function arguments (2 or fewer ideally)
9 Functions
Function arguments (2 or fewer ideally)
+Even fancier, Python3.8+ only
10 Functions
Function names should say what they do
11 Functions
Functions should only be one level of abstraction
When you have more than one level of abstraction, your function
is usually doing too much. Splitting up functions leads to
reusability and easier testing.
11 Functions
Functions should only be one level of abstraction
12 Functions
Don't use flags as function parameters
13 Functions
Avoid side effects
A function produces a side effect if it does anything other than take a
value in and return another value or values. For example, a side effect
could be writing to a file, modifying some global variable, or accidentally
wiring all your money to a stranger.

Now, you do need to have side effects in a program on occasion - for


example, like in the previous example, you might need to write to a file. In
these cases, you should centralize and indicate where you are
incorporating side effects. Don't have several functions and classes that
write to a particular file - rather, have one (and only one) service that
does it.

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

You might also like