To speak to the wider point about DRY, it's a guiding principle for abstraction. If you have two kinds of abtractions for your method, one leads to code repetition, the other does not. Generally favour the does not.
The fact that you should have additional rules, like you shouldn't need rabbit hole debugging (jumping through a million files/objects) to understand core behaviour is not a failure of a useful guiding principle
pizza_base = {
crust: "thin",
sauce: "tomato",
cheese: "regular",
toppings: []
}
hawaiian_pizza = pizza_base {
toppings: ["ham", "pineapple"]
}
pepperoni_pizza = pizza_base {
toppings: ["pepperoni"]
}
def make_pizza(pizza):
requests.post(PIZZA_URL, pizza)It might be "fine", but you don't gain anything here while introducing both indirection and coupling. DRY is _not_ about data repetition. Data repetition is fine.
Alice and Bob having the same birthday is coincidental. And even if they are actually twins, you rather say that they are twins separately.
In your example you are just preserving keystrokes, but you don't say anything of value with 'pizza_base'. You haven't shown that 'pizza_base' is worth keeping track of or even mentioning.
A pepperoni_pizza with thick crust or extra cheese is still a pepperoni_pizza. A hawaiian_pizza's sauce being tomato doesn't relate to a pepperoni_pizza's sauce.
When coding data, just be explicit, verbose and keep it simple. Our text editors, IDEs and database APIs have affordances to change data in bulk. Those things are orders of magnitude easier if your data is simple, dumb and not complected.
There are circumstances when your version is actually better: when pizza_base has a real meaning in your domain, outside of the code representing it. The chefs, accountants and so on use the term day to day, and might even have specific pricing or techniques around it. Then pizza_base is an actual thing that you want to represent in some way.
However it could also just be a function, which derives from pizza_base from your raw data. Like is_pizza_base(). I would prefer that in general.
There is nothing wrong with dumb, raw, data. It gives you leverage and frees you from coupling.
mixed_pizza = {
crust: [["thin"], ["thick"]],
sauce: [["tomato"]],
cheese: [["regular"]],
toppings: [["beef"], ["pineapple", "pepperoni"]]
}
Where you offload the logic of making multi-topping pizzas into the data.