Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🛁 Clean Code concepts adapted for Python

License

NotificationsYou must be signed in to change notification settings

EarthCodingLab/clean-code-python

 
 

Repository files navigation

Build Status

Table of Contents

Introduction

Robert C. Martin의 저서Clean Code의 소프트웨어 엔지니어링 원칙들을 Python으로 소개합니다. 본 문서는 스타일 가이드가 아닙니다. Python에서 읽기 쉽고 재사용 가능하며 리팩토링 가능한 소프트웨어를 제작하기 위한 가이드입니다.

Not every principle herein has to be strictly followed, and even fewer will beuniversally agreed upon. These are guidelines and nothing more, but they areones codified over many years of collective experience by the authors ofCleanCode.

clean-code-javascript를 바탕으로 작성되었습니다.

Python 3.7 이상 버전을 사용합니다.

Variables

의미 있고 발음하기 쉬운 변수명을 사용하기

나쁜 예:

importdatetimeymdstr=datetime.date.today().strftime("%y-%m-%d")

추가로, 변수명에 str이라는 타입을 명시해 줄 필요가 없습니다.

좋은 예:

importdatetimecurrent_date:str=datetime.date.today().strftime("%y-%m-%d")

⬆ back to top

같은 타입의 변수에는 같은 단어 사용하기

나쁜 예:동일한 엔티티에 대해 세 가지 다른 이름을 사용하고 있습니다:

defget_user_info():passdefget_client_data():passdefget_customer_record():pass

좋은 예: 동일한 엔티티라면, 함수에서 엔티티를 일관되게 참조해야 합니다:

defget_user_info():passdefget_user_data():passdefget_user_record():pass

더 좋은 방법으로Python은 객체 지향 언어입니다. 따라서 가능하다면 의미가 있는 인스턴스 attributes, 프로퍼티 메서드 또는 메서드와 같이 코드에서 엔티티의 구체적인 구현과 함께 함수를 패키징합니다.

fromtypingimportUnion,DictclassRecord:passclassUser:info:str@propertydefdata(self)->Dict[str,str]:return {}defget_record(self)->Union[Record,None]:returnRecord()

⬆ back to top

검색하기 쉬운 이름 사용하기

우리는 통상 쓰는 것보다 더 많은 양의 코드를 읽습니다. 따라서 코드가 읽기 쉽고 검색하기 쉬운 것이 중요합니다. 프로그램을 이해하는데 의미가 있도록 변수를 짓지 않는다면 읽는 사람이 힘들 것 입니다. 이름을 검색하기 쉽도록 작성합니다.

나쁜 예:

importtime# What is the number 86400 for again?time.sleep(86400)

좋은 예:

importtime# Declare them in the global namespace for the module.SECONDS_IN_A_DAY=60*60*24time.sleep(SECONDS_IN_A_DAY)

⬆ back to top

설명 가능한 변수 사용하기

나쁜 예

importreaddress="One Infinite Loop, Cupertino 95014"city_zip_code_regex=r"^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$"matches=re.match(city_zip_code_regex,address)ifmatches:print(f"{matches[1]}:{matches[2]}")

개선된 예:

더 낫긴 하지만 여전히 정규식에 크게 의존하고 있습니다.

importreaddress="One Infinite Loop, Cupertino 95014"city_zip_code_regex=r"^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$"matches=re.match(city_zip_code_regex,address)ifmatches:city,zip_code=matches.groups()print(f"{city}:{zip_code}")

좋은 예:

서브패턴을 활용한 네이밍으로 정규식의 의존성을 줄일 수 있습니다.

importreaddress="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)ifmatches:print(f"{matches['city']},{matches['zip_code']}")

⬆ back to top

암시적인 의미 피하기

코드를 읽는 사람이 변수의 의미를 해석해야 하도록 두지 마십시오.
명시적인 것이 암시적인 것보다 낫습니다.

나쁜 예:

seq= ("Austin","New York","San Francisco")foriteminseq:# do_stuff()# do_some_other_stuff()# Wait, what's `item` again?print(item)

좋은 예:

locations= ("Austin","New York","San Francisco")forlocationinlocations:# do_stuff()# do_some_other_stuff()# ...print(location)

⬆ back to top

불필요한 맥락 제거하기

클래스/객체 이름에서 알 수 있는 내용이 있으면, 해당 변수명에서 반복하지 마십시오.

나쁜 예:

classCar:car_make:strcar_model:strcar_color:str

좋은 예:

classCar:make:strmodel:strcolor:str

⬆ back to top

Use default arguments instead of short circuiting or conditionals

Tricky

Why write:

importhashlibdefcreate_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 it clearthat you are expecting a string as the argument.

좋은 예:

importhashlibdefcreate_micro_brewery(name:str="Hipster Brew Co."):slug=hashlib.sha1(name.encode()).hexdigest()# etc.

⬆ back to top

Functions

함수는 한 가지 일만 하기

소프트웨어 엔지니어링에서 가장 중요한 규칙입니다. 함수가 한가지 이상의 일을 수행한다면 함수를 구성, 테스트, 추론하기 어려워집니다. 함수를 한 가지 일만 수행하도록 분리한다면 쉽게 리팩토링 할 수 있으며, 코드는 더욱 깔끔해집니다. 이 가이드에서 이 한 가지만 기억하더라도 다른 많은 개발자보다 앞서게 될 것입니다.

나쁜 예:

fromtypingimportListclassClient:active:booldefemail(client:Client)->None:passdefemail_clients(clients:List[Client])->None:"""Filter active clients and send them an email.    """forclientinclients:ifclient.active:email(client)

좋은 예:

fromtypingimportListclassClient:active:booldefemail(client:Client)->None:passdefget_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.    """forclientinget_active_clients(clients):email(client)

generator를 사용하여 더욱 개선할 수 있습니다.

더 좋은 예

fromtypingimportGenerator,IteratorclassClient:active:booldefemail(client:Client):passdefactive_clients(clients:Iterator[Client])->Generator[Client,None,None]:"""Only active clients"""return (clientforclientinclientsifclient.active)defemail_client(clients:Iterator[Client])->None:"""Send an email to a given list of clients.    """forclientinactive_clients(clients):email(client)

⬆ back to top

함수 인자 (이상적으로는 2개 이하)

함수에 많은 파라미터가 있는 경우 함수가 너무 많은 작업 (하나 이상의 책임) 을 수행하고 있다는 신호일 수 있습니다. 이상적으로 2개 이하의 파라미터를 가지도록 함수를 분해해보세요.

만약 함수가 하나의 책임만을 가진다면, 일부 또는 모든 파라미터를 함수에 전달할 특수화된 객체로 묶어보는 것을 고려해보세요. 이러한 파라미터는 전용 데이터 구조로 표현될 수 있는 단일 엔티티의 애트리뷰트일 수 있습니다. 또한 이러한 엔티티는 다른 곳에서도 재사용할 수 있습니다. 여러 파라미터를 사용하는 것보다 이 방법이 나은 이유는 함수 내부에서 해당 파라미터로 수행되는 일부 계산을 새 엔티티에 속하는 메서드로 옮겨 함수의 복잡도를 줄일 수 있기 떄문입니다.

나쁜 예:

defcreate_menu(title,body,button_text,cancellable):pass

Java-esque:

classMenu:def__init__(self,config:dict):self.title=config["title"]self.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)->None:title=config.titlebody=config.body# ...config=MenuConfig()config.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!"    ))

Even fancier, Python3.8+ only

fromtypingimportTypedDictclassMenuConfig(TypedDict):"""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:booldefcreate_menu(config:MenuConfig):title=config["title"]# ...create_menu(# You need to supply all the parametersMenuConfig(title="My delicious menu",body="A description of the various items on the menu",button_text="Order now!",cancellable=True    ))

⬆ back to top

함수 이름은 함수가 무슨 일을 하는지 알기 쉽게

나쁜 예:

classEmail:defhandle(self)->None:passmessage=Email()# What is this supposed to do again?message.handle()

Good:

classEmail:defsend(self)->None:"""Send this message"""message=Email()message.send()

⬆ back to top

함수는 하나의 추상화 수준만 가지기

만약 하나 이상의 추상화 수준을 가진다면, 함수는 너무 많은 일을 할 수도 있습니다. 함수를 재사용 가능하고 테스트하기 쉽게 분리하세요.

나쁜 예:

# type: ignoredefparse_better_js_alternative(code:str)->None:regexes= [# ...    ]statements=code.split('\n')tokens= []forregexinregexes:forstatementinstatements:passast= []fortokenintokens:passfornodeinast:pass

좋은 예:

fromtypingimportTuple,List,DictREGEXES:Tuple= (# ...)defparse_better_js_alternative(code:str)->None:tokens:List=tokenize(code)syntax_tree:List=parse(tokens)fornodeinsyntax_tree:passdeftokenize(code:str)->List:statements=code.split()tokens:List[Dict]= []forregexinREGEXES:forstatementinstatements:passreturntokensdefparse(tokens:List)->List:syntax_tree:List[Dict]= []fortokenintokens:passreturnsyntax_tree

⬆ back to top

함수 파라미터로 플래그 사용하지 않기

플래그는 사용자에게 함수가 한 가지 이상의 일을 한다는 것을 알려줍니다. 함수는 한가지 일만을 수행해야 합니다. 따라서 불리언 값에 따라 다른 코드 경로를 따르도록 함수를 분리하세요.

Bad:

fromtempfileimportgettempdirfrompathlibimportPathdefcreate_file(name:str,temp:bool)->None:iftemp:        (Path(gettempdir())/name).touch()else:Path(name).touch()

Good:

fromtempfileimportgettempdirfrompathlibimportPathdefcreate_file(name:str)->None:Path(name).touch()defcreate_temp_file(name:str)->None:    (Path(gettempdir())/name).touch()

⬆ back to top

사이드 이펙트 피하기

함수가 입력값을 받아서 다른 값들을 반환하는 것 이외에 다른 작업을 수행하면 사이드 이펙트를 일으킵니다. 예를 들어, 파일 쓰기, 전역 변수 수정, 실수로 모르는 사람에게 돈을 송금하는 것 등이 있습니다.

프로그램에서 때때로 사이드 이펙트가 필요할 수 있습니다. 예를 들어, 이전 예와 같이 파일에 쓰기 작업이 필요할 수 있습니다. 이러한 경우에는 사이트 이펙트를 포함하는 위치를 중앙 집중화하고 표시해야 합니다. 특정 파일에 쓰기 작업을 하는 여러 함수와 클래스를 가지지 말고 하나의 서비스만을 가지세요.

요점은 구조 없이 객체 간에 상태를 공유하거나, 누구나 쓸 수 있는 가변 데이터 유형을 사용하거나, 클래스의 인스턴스를 사용하거나, 부작용이 발생하는 위치를 중앙 집중화하지 않는 것과 같은 일반적인 함정을 피하는 것입니다. 이를 통해 대다수의 다른 프로그래머보다 행복하게 작업할 수 있을 것입니다.

나쁜 예:

# type: ignore# This is a module-level name.# It's good practice to define these as immutable values, such as a string.# However...fullname="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!globalfullnamefullname=fullname.split()split_into_first_and_last_name()# MyPy will spot the problem, complaining about 'Incompatible types in# assignment: (expression has type "List[str]", variable has type "str")'print(fullname)# ["Ryan", "McDermott"]# OK. It worked the first time, but what will happen if we call the# function again?

좋은 예:

fromtypingimportList,AnyStrdefsplit_into_first_and_last_name(name:AnyStr)->List[AnyStr]:returnname.split()fullname="Ryan McDermott"name,surname=split_into_first_and_last_name(fullname)print(name,surname)# => 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"]

⬆ back to top

Classes

Single Responsibility Principle (SRP)

Robert C. Martin writes:

A class should have only one reason to change.

"Reasons to change" are, in essence, the responsibilities managed by a class orfunction.

In the following example, we create an HTML element that represents a commentwith the version of the document:

Bad

fromimportlibimportmetadataclassVersionCommentElement:"""An element that renders an HTML comment with the program's version number     """defget_version(self)->str:"""Get the package version"""returnmetadata.version("pip")defrender(self)->None:print(f'<!-- Version:{self.get_version()} -->')VersionCommentElement().render()

This class has two responsibilities:

  • Retrieve the version number of the Python package
  • Render itself as an HTML element

Any change to one or the other carries the risk of impacting the other.

We can rewrite the class and decouple these responsibilities:

Good

fromimportlibimportmetadatadefget_version(pkg_name:str)->str:"""Retrieve the version of a given package"""returnmetadata.version(pkg_name)classVersionCommentElement:"""An element that renders an HTML comment with the program's version number     """def__init__(self,version:str):self.version=versiondefrender(self)->None:print(f'<!-- Version:{self.version} -->')VersionCommentElement(get_version("pip")).render()

The result is that the class only needs to take care of rendering itself. Itreceives the version text during instantiation and this text is generated bycalling a separate function,get_version(). Changing the class has no impacton the other, and vice-versa, as long as the contract between them does notchange, i.e. the function provides a string and the class__init__ methodaccepts a string.

As an added bonus, theget_version() is now reusable elsewhere.

개방/폐쇄 원칙(Open/Closed Principle (OCP))

“시스템을 확장함으로써 새로운 기능을 통합하세요. (시스템을) 수정하지 말고요.”, Uncle Bob.

객체는 확장에는 개방적이어야 하지만 수정에는 폐쇄적이어야 합니다. 내부 계약의 변경 없이 객체(클래스 등)에서 제공하는 기능을 활용해 확장이 가능해야 합니다. 객체가 깔끔하게 확장될 수 있도록 디자인되어야 이와 같은 방법이 가능해집니다.

다음의 예시는 HTTP 요청을 처리하고 응답을 반환하는 간단한 웹 프레임워크를 구현합니다.View 클래스에는 HTTP 서버가 GET 요청을 클라이언트로부터 받았을 때 호출될 단일 메서드.get()가 있습니다.

View는 의도적으로 단순하고text/plain 응답을 반환합니다. 또한 템플릿 기반의 HTML 응답을 반환하고자 하기에,TemplateView라는 하위 클래스를 사용합니다.

나쁜 예

fromdataclassesimportdataclass@dataclassclassResponse:"""An HTTP response"""status:intcontent_type:strbody:strclassView:"""A simple view that returns plain text responses"""defget(self,request)->Response:"""Handle a GET request and return a message in the response"""returnResponse(status=200,content_type='text/plain',body="Welcome to my web site"          )classTemplateView(View):"""A view that returns HTML responses based on a template file."""defget(self,request)->Response:"""Handle a GET request and return an HTML document in the response"""withopen("index.html")asfd:returnResponse(status=200,content_type='text/html',body=fd.read()               )

TemplateView 클래스는 더 많은 기능을 수행하기 위해 부모 클래스의 내부 동작을 수정했습니다. 이렇게 함으로써, 이제 일정 시간 정지해야하는.get() 메서드의 구현을 변경하지 않기 위해View에 의존하게 됩니다. 예를 들어,View의 파생 클래스들에 대한 몇 가지 추가 검사를 도입하려할 때, 적어도 하나의 하위 유형에 오버라이딩 되어있고 이를 업데이트해야 하기 때문에 불가능합니다.

이 클래스들을 다시 디자인해서 문제를 해결하고View 클래스가 (수정하지 않고) 깔끔하게 확장될 수 있도록 해봅시다.

좋은 예

fromdataclassesimportdataclass@dataclassclassResponse:"""An HTTP response"""status:intcontent_type:strbody:strclassView:"""A simple view that returns plain text responses"""content_type="text/plain"defrender_body(self)->str:"""Render the message body of the response"""return"Welcome to my web site"defget(self,request)->Response:"""Handle a GET request and return a message in the response"""returnResponse(status=200,content_type=self.content_type,body=self.render_body()          )classTemplateView(View):"""A view that returns HTML responses based on a template file."""content_type="text/html"template_file="index.html"defrender_body(self)->str:"""Render the message body as HTML"""withopen(self.template_file)asfd:returnfd.read()

body의 소스를 바꾸려면render_body()에 오버라이딩해야 합니다만, 이 방식은'오버라이딩을 위해 서브타입을 불러온다' 라는 단일이고 잘 정의된 책임을 가집니다. 이는 서브타입에 의해 확장될 수 있게끔 디자인되었습니다.

객체 상속과 객체 구성의 장점을 둘 다 활용하는 또 다른 좋은 방법은Mixins을 사용하는 것입니다.

Mixins는 다른 관련 클래스들과 독점적으로 사용되게끔 하게 해주는 베어본 클래스입니다. 대상의 동작을 변경하기 위해 다중상속을 활용하여 '혼합' 되어있습니다.

몇 가지 규칙들이 있습니다.

  • Mixins는 항상객체로부터 상속받아야 합니다.
  • Mixins는 다음과 같이 항상 대상 클래스 앞에 와야합니다.class Foo(MixinA, MixinB, TargetClass): ...

다른 좋은 예

fromdataclassesimportdataclass,fieldfromtypingimportProtocol@dataclassclassResponse:"""An HTTP response"""status:intcontent_type:strbody:strheaders:dict=field(default_factory=dict)classView:"""A simple view that returns plain text responses"""content_type="text/plain"defrender_body(self)->str:"""Render the message body of the response"""return"Welcome to my web site"defget(self,request)->Response:"""Handle a GET request and return a message in the response"""returnResponse(status=200,content_type=self.content_type,body=self.render_body()          )classTemplateRenderMixin:"""A mixin class for views that render HTML documents using a template file     Not to be used by itself!     """template_file:str=""defrender_body(self)->str:"""Render the message body as HTML"""ifnotself.template_file:raiseValueError("The path to a template file must be given.")withopen(self.template_file)asfd:returnfd.read()classContentLengthMixin:"""A mixin class for views that injects a Content-Length header in the     response     Not to be used by itself!     """defget(self,request)->Response:"""Introspect and amend the response to inject the new header"""response=super().get(request)# type: ignoreresponse.headers['Content-Length']=len(response.body)returnresponseclassTemplateView(TemplateRenderMixin,ContentLengthMixin,View):"""A view that returns HTML responses based on a template file."""content_type="text/html"template_file="index.html"

보시다시피, Mixins는 객체를 관련 기능들을 함께 패키징하여 단일 책임을 갖고 있고 깔끔하게 분리될 수 있는 재사용 가능한 클래스로 만듭니다. 클래스 확장은 추가 클래스의 '혼합'으로 달성됩니다.

인기있는 Django 프로젝트는 Mixins을 많이 활용하여 클래스 기반의 뷰를 구성합니다.

FIXME: Mixins에서typing.Protocol를 사용해 타입 체킹하는 방법이 명확해지면 코드에서도 다시 활성화하도록 합니다.

Liskov Substitution Principle (LSP)

“Functions that use pointers or references to base classesmust be able to use objects of derived classes without knowing it”,Uncle Bob.

This principle is named after Barbara Liskov, who collaborated with fellowcomputer scientist Jeannette Wing on the seminal paper*"A behavioral notion of subtyping" (1994). A core tenet of the paper is that"a subtype (must) preserve the behaviour of the supertype methods and also allinvariant and history properties of its supertype".

In essence, a function accepting a supertype should also accept all itssubtypes with no modification.

Can you spot the problem with the following code?

Bad

fromdataclassesimportdataclass@dataclassclassResponse:"""An HTTP response"""status:intcontent_type:strbody:strclassView:"""A simple view that returns plain text responses"""content_type="text/plain"defrender_body(self)->str:"""Render the message body of the response"""return"Welcome to my web site"defget(self,request)->Response:"""Handle a GET request and return a message in the response"""returnResponse(status=200,content_type=self.content_type,body=self.render_body()          )classTemplateView(View):"""A view that returns HTML responses based on a template file."""content_type="text/html"defget(self,request,template_file:str)->Response:# type: ignore"""Render the message body as HTML"""withopen(template_file)asfd:returnResponse(status=200,content_type=self.content_type,body=fd.read()               )defrender(view:View,request)->Response:"""Render a View"""returnview.get(request)

The expectation is thatrender() function will be able to work withViewand its subtypeTemplateView, but the latter has broken compatibility bymodifying the signature of the.get() method. The function will raiseaTypeErrorexception when used withTemplateView.

If we want therender() function to work with any subtype ofView, we mustpay attention not to break its public-facing protocol. But how do we know whatconstitutes it for a given class? Type hinters likemypy will raise an errorwhen it detects mistakes like this:

error: Signature of "get" incompatible with supertype "View"<string>:36: note:      Superclass:<string>:36: note:          def get(self, request: Any) -> Response<string>:36: note:      Subclass:<string>:36: note:          def get(self, request: Any, template_file: str) -> Response

Interface Segregation Principle (ISP)

“Keep interfaces smallso that users don’t end up depending on things they don’t need.”,Uncle Bob.

Several well known object oriented programming languages, like Java and Go,have a concept called interfaces. An interface defines the public methods andproperties of an object without implementing them. They are useful when wedon't want to couple the signature of a function to a concrete object; we'drather say "I don't care what object you give me, as long as it has certainmethods and attributes I expect to make use of".

Python does not have interfaces. We have Abstract Base Classes instead, whichare a little different, but can serve the same purpose.

Good

fromabcimportABCMeta,abstractmethod# Define the Abstract Class for a generic Greeter objectclassGreeter(metaclass=ABCMeta):"""An object that can perform a greeting action."""@staticmethod@abstractmethoddefgreet(name:str)->None:"""Display a greeting for the user with the given name"""classFriendlyActor(Greeter):"""An actor that greets the user with a friendly salutation"""@staticmethoddefgreet(name:str)->None:"""Greet a person by name"""print(f"Hello{name}!")defwelcome_user(user_name:str,actor:Greeter):"""Welcome a user with a given name using the provided actor"""actor.greet(user_name)welcome_user("Barbara",FriendlyActor())

Now imagine the following scenario: we have a certain number of PDF documentsthat we author and want to serve to our web site visitors. We are using aPython web framework and we might be tempted to design a class to manage thesedocuments, so we go ahead and design a comprehensive abstract base class forour document.

Error

importabcclassPersistable(metaclass=abc.ABCMeta):"""Serialize a file to data and back"""@property@abc.abstractmethoddefdata(self)->bytes:"""The raw data of the file"""@classmethod@abc.abstractmethoddefload(cls,name:str):"""Load the file from disk"""@abc.abstractmethoddefsave(self)->None:"""Save the file to disk"""# We just want to serve the documents, so our concrete PDF document# implementation just needs to implement the `.load()` method and have# a public attribute named `data`.classPDFDocument(Persistable):"""A PDF document"""@propertydefdata(self)->bytes:"""The raw bytes of the PDF document"""          ...# Code goes here - omitted for brevity@classmethoddefload(cls,name:str):"""Load the file from the local filesystem"""          ...# Code goes here - omitted for brevitydefview(request):"""A web view that handles a GET request for a document"""requested_name=request.qs['name']# We want to validate this!returnPDFDocument.load(requested_name).data

But we can't! If we don't implement the.save() method, an exception will beraised:

Can't instantiate abstract class PDFDocument with abstract method save.

That's annoying. We don't really need to implement.save() here. We couldimplement a dummy method that does nothing or raisesNotImplementedError, butthat's useless code that we will need to maintain.

At the same time, if we remove.save() from the abstract class now we willneed to add it back when we will later implement a way for users to submittheir documents, bringing us back to the same situation as before.

The problem is that we have written aninterface that has features we don'tneed right now as we are not using them.

The solution is to decompose the interface into smaller and composableinterfaces that segregate each feature.

Good

importabcclassDataCarrier(metaclass=abc.ABCMeta):"""Carries a data payload"""@propertydefdata(self):          ...classLoadable(DataCarrier):"""Can load data from storage by name"""@classmethod@abc.abstractmethoddefload(cls,name:str):          ...classSaveable(DataCarrier):"""Can save data to storage"""@abc.abstractmethoddefsave(self)->None:          ...classPDFDocument(Loadable):"""A PDF document"""@propertydefdata(self)->bytes:"""The raw bytes of the PDF document"""          ...# Code goes here - omitted for brevity@classmethoddefload(cls,name:str)->None:"""Load the file from the local filesystem"""          ...# Code goes here - omitted for brevitydefview(request):"""A web view that handles a GET request for a document"""requested_name=request.qs['name']# We want to validate this!returnPDFDocument.load(requested_name).data

Dependency Inversion Principle (DIP)

“Depend upon abstractions, not concrete details”,Uncle Bob.

Imagine we wanted to write a web view that returns an HTTP response thatstreams rows of a CSV file we create on the fly. We want to use the CSV writerthat is provided by the standard library.

Bad

importcsvfromioimportStringIOclassStreamingHttpResponse:"""A streaming HTTP response"""     ...# implementation code goes heredefsome_view(request):rows= (          ['First row','Foo','Bar','Baz'],          ['Second row','A','B','C','"Testing"',"Here's a quote"]     )# Define a generator to stream data directly to the clientdefstream():buffer_=StringIO()writer=csv.writer(buffer_,delimiter=';',quotechar='"')forrowinrows:writer.writerow(row)buffer_.seek(0)data=buffer_.read()buffer_.seek(0)buffer_.truncate()yielddata# Create the streaming response  object with the appropriate CSV header.response=StreamingHttpResponse(stream(),content_type='text/csv')response['Content-Disposition']='attachment; filename="somefilename.csv"'returnresponse

Our first implementation works around the CSV's writer interface bymanipulating aStringIO object (which is file-like) and performing severallow level operations in order to farm out the rows from the writer. It's a lotof work and not very elegant.

A better way is to leverage the fact that the writer just needs an object witha.write() method to do our bidding. Why not pass it a dummy object thatimmediately returns the newly assembled row, so thattheStreamingHttpResponseclass can immediate stream it back to the client?

Good

importcsvclassEcho:"""An object that implements just the write method of the file-like     interface.     """defwrite(self,value):"""Write the value by returning it, instead of storing in a buffer."""returnvaluedefsome_streaming_csv_view(request):"""A view that streams a large CSV file."""rows= (          ['First row','Foo','Bar','Baz'],          ['Second row','A','B','C','"Testing"',"Here's a quote"]     )writer=csv.writer(Echo(),delimiter=';',quotechar='"')returnStreamingHttpResponse(          (writer.writerow(row)forrowinrows),content_type="text/csv",headers={'Content-Disposition':'attachment; filename="somefilename.csv"'},     )

Much better, and it works like a charm! The reason it's superior to theprevious implementation should be obvious: less code (and more performant) toachieve the same result. We decided to leverage the fact that the writer classdepends on the.write() abstraction of the object it receives, without caringabout the low level, concrete details of what the method actually does.

This example was taken froma submission made to the Django documentationby this author.

⬆ back to top

Don't repeat yourself (DRY)

Try to observe theDRYprinciple.

Do your absolute best to avoid duplicate code. Duplicate code is bad because itmeans that there's more than one place to alter something if you need to changesome logic.

Imagine if you run a restaurant and you keep track of your inventory: all yourtomatoes, onions, garlic, spices, etc. If you have multiple lists that you keepthis on, then all have to be updated when you serve a dish with tomatoes inthem. If you only have one list, there's only one place to update!

Often you have duplicate code because you have two or more slightly differentthings, that share a lot in common, but their differences force you to have twoor more separate functions that do much of the same things. Removing duplicatecode means creating an abstraction that can handle this set of different thingswith just one function/module/class.

Getting the abstraction right is critical. Bad abstractions can be worse thanduplicate code, so be careful! Having said this, if you can make a goodabstraction, do it! Don't repeat yourself, otherwise you'll find yourselfupdating multiple places any time you want to change one thing.

Bad:

fromtypingimportList,Dictfromdataclassesimportdataclass@dataclassclassDeveloper:def__init__(self,experience:float,github_link:str)->None:self._experience=experienceself._github_link=github_link@propertydefexperience(self)->float:returnself._experience@propertydefgithub_link(self)->str:returnself._github_link@dataclassclassManager:def__init__(self,experience:float,github_link:str)->None:self._experience=experienceself._github_link=github_link@propertydefexperience(self)->float:returnself._experience@propertydefgithub_link(self)->str:returnself._github_linkdefget_developer_list(developers:List[Developer])->List[Dict]:developers_list= []fordeveloperindevelopers:developers_list.append({'experience':developer.experience,'github_link':developer.github_link        })returndevelopers_listdefget_manager_list(managers:List[Manager])->List[Dict]:managers_list= []formanagerinmanagers:managers_list.append({'experience':manager.experience,'github_link':manager.github_link        })returnmanagers_list## create list objects of developerscompany_developers= [Developer(experience=2.5,github_link='https://github.com/1'),Developer(experience=1.5,github_link='https://github.com/2')]company_developers_list=get_developer_list(developers=company_developers)## create list objects of managerscompany_managers= [Manager(experience=4.5,github_link='https://github.com/3'),Manager(experience=5.7,github_link='https://github.com/4')]company_managers_list=get_manager_list(managers=company_managers)

Good:

fromtypingimportList,Dictfromdataclassesimportdataclass@dataclassclassEmployee:def__init__(self,experience:float,github_link:str)->None:self._experience=experienceself._github_link=github_link@propertydefexperience(self)->float:returnself._experience@propertydefgithub_link(self)->str:returnself._github_linkdefget_employee_list(employees:List[Employee])->List[Dict]:employees_list= []foremployeeinemployees:employees_list.append({'experience':employee.experience,'github_link':employee.github_link        })returnemployees_list## create list objects of developerscompany_developers= [Employee(experience=2.5,github_link='https://github.com/1'),Employee(experience=1.5,github_link='https://github.com/2')]company_developers_list=get_employee_list(employees=company_developers)## create list objects of managerscompany_managers= [Employee(experience=4.5,github_link='https://github.com/3'),Employee(experience=5.7,github_link='https://github.com/4')]company_managers_list=get_employee_list(employees=company_managers)

⬆ back to top

Translations

This document is also available in other languages:

⬆ back to top

About

🛁 Clean Code concepts adapted for Python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python98.8%
  • Makefile1.2%

[8]ページ先頭

©2009-2025 Movatter.jp