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

Appearance settings

smc0210/clean-code-python

Open more actions menu
ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

clean-code-python

๋ชฉ์ฐจ

  1. ๋„์ž…
  2. ๋ณ€์ˆ˜
  3. ํ•จ์ˆ˜
  4. ๊ฐ์ฒด์™€ ์ž๋ฃŒ๊ตฌ์กฐ
  5. ํด๋ž˜์Šค
    1. S: Single Responsibility Principle (SRP)
    2. O: Open/Closed Principle (OCP)
    3. L: Liskov Substitution Principle (LSP)
    4. I: Interface Segregation Principle (ISP)
    5. D: Dependency Inversion Principle (DIP)
  6. Don't repeat yourself (DRY)

๋„์ž…

์†Œํ”„ํŠธ์›จ์–ด ์—”์ง€๋‹ˆ์–ด๋ง์˜ ์›์น™์œผ๋กœ ๋ถˆ๋ฆฌ๋Š” Robert C. Martin์˜ ์ €์„œ Clean Code, ๋ฅผ ํŒŒ์ด์ฌ์œผ๋กœ ๊ฐ์ƒ‰ํ•œ ๋ฒ„์ „์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์œผ๋กœ ์ฝ๊ธฐ ์‰ฝ๊ณ , ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ ๋ฆฌํŒฉํ† ๋ง์ด ๊ฐ€๋Šฅํ•œ ์†Œํ”„ํŠธ์›จ์›Œ๋ฅผ ์ œ์ž‘ํ•˜๋Š” ๊ฐ€์ด๋“œ ์ž…๋‹ˆ๋‹ค.

๋ณธ ๋ฌธ์„œ์˜ ๋ชจ๋“  ์›์น™์„ ์—„๊ฒฉํ•˜๊ฒŒ ์ค€์ˆ˜ ํ•  ํ•„์š”๋Š” ์—†์œผ๋ฉฐ, ์‹ฌ์ง€์–ด ๋” ์ ์€ ์‚ฌ๋žŒ๋“ค์ด ๋ณดํŽธ์ ์œผ๋กœ ๋™์˜ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ๋“ค์€ ์ง€์นจ์ผ ๋ฟ์ด์ง€ ๊ทธ ์ด์ƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ์ˆ˜๋…„ ๊ฐ„ Clean Code์˜ ์ €์ž๋“ค์— ์˜ํ•ด ์ถ•์ ๋œ ๊ฒฝํ—˜๋“ค์„ ํ†ตํ•ด ์ฒด๊ณ„ํ™”๋œ ๊ฒƒ๋“ค์ž…๋‹ˆ๋‹ค.

clean-code-javascript์—์„œ ์˜๊ฐ์„ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค.

Python3.7+ ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค.

๋ณ€์ˆ˜

์˜๋ฏธ ์žˆ๊ณ  ๋ฐœ์Œํ•˜๊ธฐ ์‰ฌ์šด ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์„ธ์š”.

์•ˆ ์ข‹์€ ์˜ˆ:

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

์ข‹์€ ์˜ˆ:

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

โฌ† ๋งจ ์œ„๋กœ

๋™์ผํ•œ ์œ ํ˜•์˜ ๋ณ€์ˆ˜์— ๋™์ผํ•œ ์–ดํœ˜๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.

์•ˆ ์ข‹์€ ์˜ˆ: ์—ฌ๊ธฐ์„œ๋Š” ๋™์ผํ•œ ๊ธฐ๋ณธ ์—”ํ‹ฐํ‹ฐ(Entity)์— ๋Œ€ํ•ด ์„ธ ๊ฐ€์ง€ ๋‹ค๋ฅธ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.:

get_user_info()
get_client_data()
get_customer_record()

์ข‹์€ ์˜ˆ: ๋งŒ์•ฝ ์—”ํ‹ฐํ‹ฐ(Entity)๊ฐ€ ๋™์ผํ•˜๋‹ค๋ฉด ํ•จ์ˆ˜์—์„œ ์—”ํ‹ฐํ‹ฐ(Entity)๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ฐ ์ผ๊ด€์„ฑ์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.:

get_user_info()
get_user_data()
get_user_record()

๋” ๋‚˜์€ ์˜ˆ ํŒŒ์ด์ฌ์€ (๋˜ํ•œ) ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ฝ”๋“œ์—์„œ ์—”ํ‹ฐํ‹ฐ(Entity)์˜ ๊ตฌ์ฒด์ ์ธ ๊ตฌํ˜„๊ณผ ํ•จ๊ป˜ ์ธ์Šคํ„ด์Šค ์†์„ฑ(attributes), ํ”„๋กœํผํ‹ฐ ๋ฉ”์†Œ๋“œ(property methods) ๋˜๋Š” ๋ฉ”์†Œ๋“œ(methods)๋กœ ํ•จ์ˆ˜๋ฅผ ํŒจํ‚ค์ง€ํ™” ํ•˜์‹ญ์‹œ์˜ค:

class User:
    info : str

    @property
    def data(self) -> dict:
        # ...

    def get_record(self) -> Union[Record, None]:
        # ...

โฌ† ๋งจ ์œ„๋กœ

๊ฒ€์ƒ‰ ๊ฐ€๋Šฅํ•œ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์„ธ์š”.

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.

์šฐ๋ฆฌ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋” ๋งŽ์€ ์ฝ”๋“œ๋ฅผ ์ฝ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ฝ๊ณ  ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ์˜๋ฏธ๊ฐ€ ์žˆ๋Š” ๋ณ€์ˆ˜์ด๋ฆ„์„ ์ง“์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ฝ”๋“œ๋ฅผ ์ฝ๋Š” ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ์ƒ์ฒ˜๋ฅผ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒ€์ƒ‰ ๊ฐ€๋Šฅํ•œ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์„ธ์š”.

์•ˆ ์ข‹์€ ์˜ˆ:

# ๋„๋Œ€์ฒด 86400์ด ๋ญ์•ผ?
time.sleep(86400);

์ข‹์€ ์˜ˆ:

# ๋ชจ๋“ˆ์˜ ์ „์—ญ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์„ ์–ธํ•˜์‹ญ์‹œ์˜ค.
SECONDS_IN_A_DAY = 60 * 60 * 24

time.sleep(SECONDS_IN_A_DAY)

โฌ† ๋งจ ์œ„๋กœ

์„ค๋ช…์ ์ธ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.

์•ˆ ์ข‹์€ ์˜ˆ:

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])

๋‚˜์˜์ง€ ์•Š์€ ์˜ˆ:

๋” ๋‚ซ์ง€๋งŒ ์—ฌ์ „ํžˆ ์ •๊ทœํ‘œํ˜„์‹์— ํฌ๊ฒŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค.

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)

์ข‹์€ ์˜ˆ:

ํ•˜์œ„ ํŒจํ„ด์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•˜์—ฌ ์ •๊ทœ์‹์— ๋Œ€ํ•œ ์˜์กด์„ฑ์„ ์ค„์ž…๋‹ˆ๋‹ค.

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'])

โฌ† ๋งจ ์œ„๋กœ

Mental Mapping์„ ํ”ผํ•˜์„ธ์š”

์ฝ”๋“œ๋ฅผ ์ฝ๋Š” ์‚ฌ๋žŒ์ด ๋ณ€์ˆ˜์˜ ์˜๋ฏธ๋ฅผ ๋ฒˆ์—ญํ•˜๋„๋ก ๊ฐ•์š”ํ•˜์ง€ ๋งˆ์„ธ์š”. ์•”์‹œ์ ์ธ ๊ฒƒ๋ณด๋‹ค ๋ช…์‹œ์ ์ธ ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

์•ˆ ์ข‹์€ ์˜ˆ:

seq = ('Austin', 'New York', 'San Francisco')

for item in seq:
    do_stuff()
    do_some_other_stuff()
    # ...
    # ์ž ๊น๋งŒ์š”, `item`์ด ๋ญ์—์š”?
    dispatch(item)

์ข‹์€ ์˜ˆ:

locations = ('Austin', 'New York', 'San Francisco')

for location in locations:
    do_stuff()
    do_some_other_stuff()
    # ...
    dispatch(location)

โฌ† ๋งจ ์œ„๋กœ

๋ถˆํ•„์š”ํ•œ ๋ฌธ๋งฅ์„ ์ถ”๊ฐ€ํ•˜์ง€ ๋งˆ์„ธ์š”.

ํด๋ž˜์Šค/๊ฐ์ฒด์˜ ์ด๋ฆ„์ด ๋ญ”๊ฐ€๋ฅผ ๋งํ•œ๋‹ค๋ฉด ๋ณ€์ˆ˜ ์ด๋ฆ„์—์„œ ๋ฐ˜๋ณตํ•˜์ง€ ๋งˆ์„ธ์š”.

์•ˆ ์ข‹์€ ์˜ˆ:

class Car:
    car_make: str
    car_model: str
    car_color: str

์ข‹์€ ์˜ˆ:

class Car:
    make: str
    model: str
    color: str

โฌ† ๋งจ ์œ„๋กœ

๋‹จ๋ฝ ๋˜๋Š” ์กฐ๊ฑด๋ฌธ ๋Œ€์‹  ๊ธฐ๋ณธ ์ธ์ˆ˜(arguments)๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.

Tricky

Why write:

def create_micro_brewery(name):
    name = "Hipster Brew Co." if name is None else name
    slug = hashlib.sha1(name.encode()).hexdigest()
    # etc.

... when you can specify a default argument instead? This also makes ist clear that you are expecting a string as the argument.

์ข‹์€ ์˜ˆ:

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

โฌ† ๋งจ ์œ„๋กœ

ํ•จ์ˆ˜

ํ•จ์ˆ˜ ์ธ์ˆ˜(arguments) (2๊ฐœ ์ดํ•˜)

Limiting the amount of function parameters is incredibly important because it makes testing your function easier. Having more than three leads to a combinatorial explosion where you have to test tons of different cases with each separate argument.

ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜(parameter)์˜ ์–‘์„ ์ œํ•œํ•˜๋Š” ๊ฒƒ์€ ํ•จ์ˆ˜ ํ…Œ์ŠคํŠธ๋ฅผ ๋ณด๋‹ค ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์„ธ๊ฐœ ์ด์ƒ์ด๋ฉด ์„œ๋กœ ๋‹ค๋ฅธ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ฐ๊ฐ์˜ ๊ฐœ๋ณ„ ์ธ์ˆ˜(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 two arguments then your function is trying to do too much. In cases where it's not, most of the time a higher-level object will suffice as an argument.

์ธ์ˆ˜(arguments)๊ฐ€ ์—†๋Š” ๊ฒƒ์ด ์ด์ƒ์ ์ธ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜ ๋˜๋Š” ๋‘๊ฐœ์˜ ์ธ์ˆ˜(arguments)๋Š” ๊ดœ์ฐฎ์œผ๋ฉฐ ์„ธ ๊ฐœ๋Š” ํ”ผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์ƒ์€ ๋ชจ๋‘ ํ†ตํ•ฉ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ธ์ˆ˜(arguments)๊ฐ€ ๋‘๊ฐœ ์ด์ƒ์ด๋ฉด ํ•จ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋Œ€๊ฒŒ ์ƒ์œ„ ๊ฐ์ฒด์˜ ์ธ์ˆ˜๋กœ ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค.

์•ˆ ์ข‹์€ ์˜ˆ:

def create_menu(title, body, button_text, cancellable):
    # ...

์ข‹์€ ์˜ˆ:

class Menu:
    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

class MenuConfig:
    """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: str
    body: str
    button_text: str
    cancellable: bool = False


def create_menu(config: MenuConfig):
    title = config.title
    body = 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!"
# ์ธ์Šคํ„ด์Šค ์†์„ฑ(attribute)์€ ๊ธฐ๋ณธ ํด๋ž˜์Šค ์†์„ฑ(attribute)๋ณด๋‹ค ์šฐ์„ ํ•ฉ๋‹ˆ๋‹ค.
config.cancellable = True

create_menu(config)

Fancy

from typing import NamedTuple


class MenuConfig(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: str
    body: str
    button_text: str
    cancellable: bool = False


def create_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

from dataclasses import astuple, dataclass


@dataclass
class MenuConfig:
    """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: str
    body: str
    button_text: str
    cancellable: bool = False

def create_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 more than one thing, they are harder to compose, test, and reason about. When you can isolate a function to just one action, they can be refactored easily and your code will read much cleaner. If you take nothing else away from this guide other than this, you'll be ahead of many developers.

์ด๊ฒƒ์€ ์†Œํ”„ํŠธ์›จ์–ด ์—”์ง€๋‹ˆ์–ด๋ง์—์„œ ๋‹จ์—ฐ์ฝ” ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ทœ์น™์ž…๋‹ˆ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ํ•œ ๊ฐ€์ง€ ์ด์ƒ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ž‘์„ฑ, ํ…Œ์ŠคํŠธ ๋ฐ ์ถ”๋ก ํ•˜๊ธฐ๊ฐ€ ๋” ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜์˜ ๋™์ž‘์œผ๋กœ ๋ถ„๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์‰ฝ๊ฒŒ ๋ฆฌํŒฉํ† ๋ง ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊น”๋”ํ•ด์ง‘๋‹ˆ๋‹ค.

์•ˆ ์ข‹์€ ์˜ˆ:

def email_clients(clients: List[Client]):
    """Filter active clients and send them an email.
    """
    for client in clients:
        if client.active:
            email(client)

์ข‹์€ ์˜ˆ:

def get_active_clients(clients: List[Client]) -> List[Client]:
    """Filter active clients.
    """
    return [client for client in clients if client.active]


def email_clients(clients: List[Client, ...]) -> None:
    """Send an email to a given list of clients.
    """
    for client in clients:
        email(client)

Do you see an opportunity for using generators now?

Even better

def active_clients(clients: List[Client]) -> Generator[Client]:
    """Only active clients.
    """
    return (client for client in clients if client.active)


def email_client(clients: Iterator[Client]) -> None:
    """Send an email to a given list of clients.
    """
    for client in clients:
        email(client)

โฌ† ๋งจ ์œ„๋กœ

Function names should say what they do

์•ˆ ์ข‹์€ ์˜ˆ:

class Email:
    def handle(self) -> None:
        # Do something...

message = Email()
# What is this supposed to do again?
message.handle()

Good:

class Email:
    def send(self) -> None:
        """Send this message.
        """

message = Email()
message.send()

โฌ† ๋งจ ์œ„๋กœ

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.

์•ˆ ์ข‹์€ ์˜ˆ:

def parse_better_js_alternative(code: str) -> None:
    regexes = [
        # ...
    ]

    statements = regexes.split()
    tokens = []
    for regex in regexes:
        for statement in statements:
            # ...

    ast = []
    for token in tokens:
        # Lex.

    for node in ast:
        # Parse.

Good:

REGEXES = (
   # ...
)


def parse_better_js_alternative(code: str) -> None:
    tokens = tokenize(code)
    syntax_tree = parse(tokens)

    for node in syntax_tree:
        # Parse.


def tokenize(code: str) -> list:
    statements = code.split()
    tokens = []
    for regex in REGEXES:
        for statement in statements:
           # Append the statement to tokens.

    return tokens


def parse(tokens: list) -> list:
    syntax_tree = []
    for token in tokens:
        # Append the parsed token to the syntax tree.

    return syntax_tree

โฌ† ๋งจ ์œ„๋กœ

Don't use flags as function parameters

Flags tell your user that this function does more than one thing. Functions should do one thing. Split your functions if they are following different code paths based on a boolean.

์•ˆ ์ข‹์€ ์˜ˆ:

from pathlib import Path

def create_file(name: str, temp: bool) -> None:
    if temp:
        Path('./temp/' + name).touch()
    else:
        Path(name).touch()

Good:

from pathlib import Path

def create_file(name: str) -> None:
    Path(name).touch()

def create_temp_file(name: str) -> None:
    Path('./temp/' + name).touch()

โฌ† ๋งจ ์œ„๋กœ

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.

์•ˆ ์ข‹์€ ์˜ˆ:

# This is a module-level name.
# It's good practice to define these as immutable values, such as a string.
# However...
name = 'Ryan McDermott'

def split_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!
    global name
    name = 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:

def split_into_first_and_last_name(name: str) -> list:
    return name.split()

name = 'Ryan McDermott'
new_name = split_into_first_and_last_name(name)

print(name)  # 'Ryan McDermott'
print(new_name)  # ['Ryan', 'McDermott']

Also good

from dataclasses import dataclass

@dataclass
class Person:
    name: str

    @property
    def name_as_first_and_last(self) -> list:
        return self.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

โฌ† ๋งจ ์œ„๋กœ

ํด๋ž˜์Šค

Single Responsibility Principle (SRP)

Open/Closed Principle (OCP)

Liskov Substitution Principle (LSP)

Interface Segregation Principle (ISP)

Dependency Inversion Principle (DIP)

Coming soon

โฌ† ๋งจ ์œ„๋กœ

Don't repeat yourself (DRY)

Coming soon

โฌ† ๋งจ ์œ„๋กœ

About

๐Ÿ› Clean Code concepts adapted for Python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%
Morty Proxy This is a proxified and sanitized view of the page, visit original site.