@Speakermen

В чем соль рефакторинга?

Доброго времени суток. В чём смысл рефакторинга если код разрастается в разы или я что то делаю не так? Или в моем примере нету прирост можно увидеть например в архитектурных решениях?:

Code

import dataclasses
from enum import Enum

"""
def calculate(x, y) -> int:
    return {"+": x + y, "-": x - y, "*": x * y, "/": x / y, "%": x % y}

print(calculate(1, 2)["+"])
print(calculate(1, 2)["-"])
print(calculate(1, 2)["/"])
print(calculate(1, 2)["*"])
print(calculate(1, 2)["%"])
"""

"""
@dataclasses.dataclass
class Operands:
    x: int
    y: int


class Operators(Enum):
    PLUS = "+"
    MINUS = "-"
    MULTIPLY = "*"
    DIVISION = "/"
    MODULO = "%"


def calculate(operands: Operands) -> int:
    return {Operators.PLUS: adding(operands), Operators.MINUS: subtracting(operands), Operators.MULTIPLY: multiplying(operands), Operators.DIVISION: dividing(operands), Operators.MODULO: modularDivision(operands)}


def adding(operands: Operands) -> int:
    return operands.x + operands.y


def subtracting(operands: Operands) -> int:
    return operands.x - operands.y


def multiplying(operands: Operands) -> int:
    return operands.x * operands.y


def dividing(operands: Operands) -> int:
    return operands.x / operands.y


def modularDivision(operands: Operands) -> int:
    return operands.x % operands.y


print(calculate(Operands(x=1, y=2))[Operators.PLUS])
print(calculate(Operands(x=1, y=2))[Operators.MINUS])
print(calculate(Operands(x=1, y=2))[Operators.DIVISION])
print(calculate(Operands(x=1, y=2))[Operators.MULTIPLY])
print(calculate(Operands(x=1, y=2))[Operators.MODULO])
"""


@dataclasses.dataclass
class Operands:
    x: int
    y: int


class Operators(Enum):
    PLUS = "+"
    MINUS = "-"
    MULTIPLY = "*"
    DIVISION = "/"
    MODULO = "%"
    All = "all"


def calculate(operands: Operands) -> int:
    return {Operators.PLUS: adding(operands), Operators.MINUS: subtracting(operands), Operators.MULTIPLY: multiplying(operands), Operators.DIVISION: dividing(operands), Operators.MODULO: modularDivision(operands)}


def adding(operands: Operands) -> int:
    return operands.x + operands.y


def subtracting(operands: Operands) -> int:
    return operands.x - operands.y


def multiplying(operands: Operands) -> int:
    return operands.x * operands.y


def dividing(operands: Operands) -> int:
    return operands.x / operands.y


def modularDivision(operands: Operands) -> int:
    return operands.x % operands.y


def calculate(operands: Operands, operators: Operators) -> int:
    match operators:
        case operators.PLUS:
            print(adding(operands))
        case operators.MINUS:
            print(subtracting(operands))
        case operators.MULTIPLY:
            print(multiplying(operands))
        case operators.DIVISION:
            print(dividing(operands))
        case operators.MODULO:
            print(modularDivision(operands))
        case operators.All:
            print(f'Adding (+) = {adding(operands)}')
            print(f'Subtracting (-) = {subtracting(operands)}')
            print(f'Multiplying (*) = {multiplying(operands)}')
            print(f'Dividing (/) = {dividing(operands)}')
            print(f'Modular division (%) = {modularDivision(operands)}')
        case _:
            print("There is no such operator!")


"""
calculate(Operands(x=1, y=2), Operators.PLUS)
calculate(Operands(x=1, y=2), Operators.MINUS)
calculate(Operands(x=1, y=2), Operators.MULTIPLY)
calculate(Operands(x=1, y=2), Operators.DIVISION)
calculate(Operands(x=1, y=2), Operators.MODULO)
calculate(Operands(x=1, y=2), Operators.All)
"""

calculate(Operands(x=120, y=120), Operators.All)



6411984f42485037817168.png
641198d3ef7b0336925404.png
  • Вопрос задан
  • 150 просмотров
Решения вопроса 1
saboteur_kiev
@saboteur_kiev Куратор тега Python
software engineer
Есть приложение. Простое. Оно начинает усложняться. Ты его дописываешь, допиливаешь. Проходит год-два, у тебя уже комбайн.

Если сесть и подумать над всеми фичами которые в приложении есть сейчас, записать все реквайрнмнеты и например начать писать приложение, которое умеет это все делать с нуля, скорее всего новое приложение будет кардинально отличаться по архитектуре.

Рефакторинг - это преесмотр на разных уровнях твоего приложения, чтобы изменить его структуру, его подходы к чему-либо, учитывая все прошедшие изменения в приложении или платформах вокруг.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
27 апр. 2024, в 13:49
300000 руб./за проект
27 апр. 2024, в 13:30
30000 руб./за проект
27 апр. 2024, в 13:22
600 руб./за проект