3 ms·
What about something like this (in JS)? // Option 1 const Pizzas = { Hawaiian: { type: 'hawaiian', crust: 'thin', sauce: 'tomat
by Pr0ject217 4y ago
What about something like this (in JS)?
// Option 1
const Pizzas = {
Hawaiian: {
type: 'hawaiian',
crust: 'thin',
sauce: 'tomato',
cheese: 'regular',
toppings: ['ham', 'pineapple'],
},
Pepperoni: {
type: 'pepperoni',
crust: 'thin',
sauce: 'tomato',
cheese: 'regular',
toppings: ['pepperoni'],
},
};
// Option 2
// Pizzas could be returned from an API, so that the pizza
types are configurable outside of the code
const response = [
{
type: 'hawaiian',
crust: 'thin',
sauce: 'tomato',
cheese: 'regular',
toppings: ['ham', 'pineapple'],
},
{
type: 'pepperoni',
crust: 'thin',
sauce: 'tomato',
cheese: 'regular',
toppings: ['pepperoni'],
},
];
const makePizza = (pizza) => requests.post(PIZZA_URL, pizza);
// Then, for Option 1
makePizza(Pizzas.Hawaiian);
makePizza(Pizzas.Pepperoni);
// Or, for Option 2 (e.g. user selected via a menu)
makePizza(selectedPizza);
- sjducb 4y agoThat's very clever, but the OP's point is that you shouldn't try to be clever. Your life will be easier if the code is simple. When I read the initial example I can understand it in the time taken to read the code. With your example I had to think for about 1-2 min before it made sense. If the codebase is full of clever stuff then I have to spend hours understanding all of the clever things before I can make changes. If everything is simple then it's easy to change. If you want to see where overengineering leads you then take a look at this project. https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris... It is satire but I have absolutely worked in places that write code like that. Good programmers know that it's 10x times harder to read code than write it, so they deliberately keep it simple so that they can read it later.
- Pr0ject217 4y agoThank you for your response. Would you mind clarifying what is clever about it? Thank you very much.