Skip to content

two holiday functions contradict DRY - dict solution is better #69

@sergey-samoylov

Description

@sergey-samoylov

On your youtube video "10 Tips to Keep Your Software Simple" timing 11:32 you state that it is better to have single responsibility functions and split one function into two. But as before split and so much after it these functions contradict DRY principle. To that, they have magic numbers. Which is not good at all.

A better solution might be with a dictionary:

HOLIDAY_CONFIG = {
    "single": {"days": 1, "msg": "Have fun on your holiday..."},
    "payout": {"days": 5, "msg": "Paying out a holiday. Left: {remaining_days}"},
}
 
def take_holiday(self, holiday_type: str) -> None:
    config = HOLIDAY_CONFIG[holiday_type]  # Raises KeyError if invalid
    if self.vacation_days < config["days"]:
        raise ValueError(f"Not enough holidays for {holiday_type}.")
    self.vacation_days -= config["days"]
    print(config["msg"].format(remaining_days=self.vacation_days))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions