- Notifications
You must be signed in to change notification settings - Fork0
🛁 Clean Code concepts adapted for Python
License
danielOliveira97/clean-code-python
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Software engineering principles, from Robert C. Martin's bookClean Code,adapted for Python. This is not a style guide. It's a guide to producingreadable, reusable, and refactorable software in Python.
Not every principle herein has to be strictly followed, and even fewer will be universallyagreed upon. These are guidelines and nothing more, but they are ones codified over manyyears of collective experience by the authors ofClean Code.
Inspired fromclean-code-javascript
Targets Python3.7+
Bad:
ymdstr=datetime.date.today().strftime("%y-%m-%d")
Good:
current_date:str=datetime.date.today().strftime("%y-%m-%d")
Bad:Here we use three different names for the same underlying entity:
get_user_info()get_client_data()get_customer_record()
Good:If the entity is the same, you should be consistent in referring to it in your functions:
get_user_info()get_user_data()get_user_record()
Even betterPython is (also) an object oriented programming language. If it makes sense, package the functions together with the concrete implementationof the entity in your code, as instance attributes, property methods, or methods:
classUser:info :str@propertydefdata(self)->dict:# ...defget_record(self)->Union[Record,None]:# ...
We will read more code than we will ever write. It's important that the code we do write isreadable and searchable. Bynot naming variables that end up being meaningful forunderstanding our program, we hurt our readers.Make your names searchable.
Bad:
# What the heck is 86400 for?time.sleep(86400);
Good:
# Declare them in the global namespace for the module.SECONDS_IN_A_DAY=60*60*24time.sleep(SECONDS_IN_A_DAY)
Bad:
address='One Infinite Loop, Cupertino 95014'city_zip_code_regex=r'^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$'matches=re.match(city_zip_code_regex,address)save_city_zip_code(matches[1],matches[2])
Not bad:
It's better, but we are still heavily dependent on regex.
address='One Infinite Loop, Cupertino 95014'city_zip_code_regex=r'^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$'matches=re.match(city_zip_code_regex,address)city,zip_code=matches.groups()save_city_zip_code(city,zip_code)
Good:
Decrease dependence on regex by naming subpatterns.
address='One Infinite Loop, Cupertino 95014'city_zip_code_regex=r'^[^,\\]+[,\\\s]+(?P<city>.+?)\s*(?P<zip_code>\d{5})?$'matches=re.match(city_zip_code_regex,address)save_city_zip_code(matches['city'],matches['zip_code'])
Don’t force the reader of your code to translate what the variable means.Explicit is better than implicit.
Bad:
seq= ('Austin','New York','San Francisco')foriteminseq:do_stuff()do_some_other_stuff()# ...# Wait, what's `item` for again?dispatch(item)
Good:
locations= ('Austin','New York','San Francisco')forlocationinlocations:do_stuff()do_some_other_stuff()# ...dispatch(location)
If your class/object name tells you something, don't repeat that in yourvariable name.
Bad:
classCar:car_make:strcar_model:strcar_color:str
Good:
classCar:make:strmodel:strcolor:str
Tricky
Why write:
defcreate_micro_brewery(name):name="Hipster Brew Co."ifnameisNoneelsenameslug=hashlib.sha1(name.encode()).hexdigest()# etc.
... when you can specify a default argument instead? This also makes ist clear thatyou are expecting a string as the argument.
Good:
defcreate_micro_brewery(name:str="Hipster Brew Co."):slug=hashlib.sha1(name.encode()).hexdigest()# etc.
Limiting the amount of function parameters is incredibly important because it makestesting your function easier. Having more than three leads to a combinatorial explosionwhere you have to test tons of different cases with each separate argument.
Zero arguments is the ideal case. One or two arguments is ok, and three should be avoided.Anything more than that should be consolidated. Usually, if you have more than twoarguments then your function is trying to do too much. In cases where it's not, mostof the time a higher-level object will suffice as an argument.
Bad:
defcreate_menu(title,body,button_text,cancellable):# ...
Good:
classMenu:def__init__(self,config:dict):title=config["title"]body=config["body"]# ...menu=Menu( {"title":"My Menu","body":"Something about my menu","button_text":"OK","cancellable":False })
Also good
classMenuConfig:"""A configuration for the Menu. Attributes: title: The title of the Menu. body: The body of the Menu. button_text: The text for the button label. cancellable: Can it be cancelled? """title:strbody:strbutton_text:strcancellable:bool=Falsedefcreate_menu(config:MenuConfig):title=config.titlebody=config.body# ...config=MenuConfigconfig.title="My delicious menu"config.body="A description of the various items on the menu"config.button_text="Order now!"# The instance attribute overrides the default class attribute.config.cancellable=Truecreate_menu(config)
Fancy
fromtypingimportNamedTupleclassMenuConfig(NamedTuple):"""A configuration for the Menu. Attributes: title: The title of the Menu. body: The body of the Menu. button_text: The text for the button label. cancellable: Can it be cancelled? """title:strbody:strbutton_text:strcancellable:bool=Falsedefcreate_menu(config:MenuConfig):title,body,button_text,cancellable=config# ...create_menu(MenuConfig(title="My delicious menu",body="A description of the various items on the menu",button_text="Order now!" ))
Even fancier
fromdataclassesimportastuple,dataclass@dataclassclassMenuConfig:"""A configuration for the Menu. Attributes: title: The title of the Menu. body: The body of the Menu. button_text: The text for the button label. cancellable: Can it be cancelled? """title:strbody:strbutton_text:strcancellable:bool=Falsedefcreate_menu(config:MenuConfig):title,body,button_text,cancellable=astuple(config)# ...create_menu(MenuConfig(title="My delicious menu",body="A description of the various items on the menu",button_text="Order now!" ))
This is by far the most important rule in software engineering. When functions do morethan one thing, they are harder to compose, test, and reason about. When you can isolatea function to just one action, they can be refactored easily and your code will read muchcleaner. If you take nothing else away from this guide other than this, you'll be aheadof many developers.
Bad:
defemail_clients(clients:List[Client]):"""Filter active clients and send them an email. """forclientinclients:ifclient.active:email(client)
Good:
defget_active_clients(clients:List[Client])->List[Client]:"""Filter active clients. """return [clientforclientinclientsifclient.active]defemail_clients(clients:List[Client, ...])->None:"""Send an email to a given list of clients. """forclientinclients:email(client)
Do you see an opportunity for using generators now?
Even better
defactive_clients(clients:List[Client])->Generator[Client]:"""Only active clients. """return (clientforclientinclientsifclient.active)defemail_client(clients:Iterator[Client])->None:"""Send an email to a given list of clients. """forclientinclients:email(client)
Bad:
classEmail:defhandle(self)->None:# Do something...message=Email()# What is this supposed to do again?message.handle()
Good:
classEmail:defsend(self)->None:"""Send this message. """message=Email()message.send()
When you have more than one level of abstraction, your function is usually doing toomuch. Splitting up functions leads to reusability and easier testing.
Bad:
defparse_better_js_alternative(code:str)->None:regexes= [# ... ]statements=regexes.split()tokens= []forregexinregexes:forstatementinstatements:# ...ast= []fortokenintokens:# Lex.fornodeinast:# Parse.
Good:
REGEXES= (# ...)defparse_better_js_alternative(code:str)->None:tokens=tokenize(code)syntax_tree=parse(tokens)fornodeinsyntax_tree:# Parse.deftokenize(code:str)->list:statements=code.split()tokens= []forregexinREGEXES:forstatementinstatements:# Append the statement to tokens.returntokensdefparse(tokens:list)->list:syntax_tree= []fortokenintokens:# Append the parsed token to the syntax tree.returnsyntax_tree
Flags tell your user that this function does more than one thing. Functionsshould do one thing. Split your functions if they are following different codepaths based on a boolean.
Bad:
frompathlibimportPathdefcreate_file(name:str,temp:bool)->None:iftemp:Path('./temp/'+name).touch()else:Path(name).touch()
Good:
frompathlibimportPathdefcreate_file(name:str)->None:Path(name).touch()defcreate_temp_file(name:str)->None:Path('./temp/'+name).touch()
A function produces a side effect if it does anything other than take a value inand return another value or values. For example, a side effect could be writingto a file, modifying some global variable, or accidentally wiring all your moneyto a stranger.
Now, you do need to have side effects in a program on occasion - for example, likein the previous example, you might need to write to a file. In these cases, youshould centralize and indicate where you are incorporating side effects. Don't haveseveral 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 objectswithout 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.
Bad:
# This is a module-level name.# It's good practice to define these as immutable values, such as a string.# However...name='Ryan McDermott'defsplit_into_first_and_last_name()->None:# The use of the global keyword here is changing the meaning of the# the following line. This function is now mutating the module-level# state and introducing a side-effect!globalnamename=name.split()split_into_first_and_last_name()print(name)# ['Ryan', 'McDermott']# OK. It worked the first time, but what will happen if we call the# function again?
Good:
defsplit_into_first_and_last_name(name:str)->list:returnname.split()name='Ryan McDermott'new_name=split_into_first_and_last_name(name)print(name)# 'Ryan McDermott'print(new_name)# ['Ryan', 'McDermott']
Also good
fromdataclassesimportdataclass@dataclassclassPerson:name:str@propertydefname_as_first_and_last(self)->list:returnself.name.split()# The reason why we create instances of classes is to manage state!person=Person('Ryan McDermott')print(person.name)# 'Ryan McDermott'print(person.name_as_first_and_last)# ['Ryan', 'McDermott']
Coming soon
Coming soon
Coming soon
About
🛁 Clean Code concepts adapted for Python
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Languages
- Python100.0%