Live data from Hacker News

Get rid of those boolean function parameters (2015)

mortoray.com

101–110 of 119 posts

Re: Get rid of those boolean function parameters (2015)

#101
post #39

Named parameters misses the point. Functions should do one thing and only one thing. This is why we have cos, sin, and tan and not a universal function for trigonometry: trig(mode='cos', ...). Such functions often become cumbersome to use since they are essentially multiple functions baked into one.

I do agree with you completely. But the problem here is as old as programming itself. It's not always easy to define function boundries.

If your function returns pizza, as mentioned in other comment, adding some toppings won't change the boundaries. It still returns pizza, with peperoni or not. It doesn't change how you make the pizza. You're just adding more data to it. You may solve it with syntax, language data structures, etc. Whatever you like to make it more readable to the caller. But probably you'd want to pass toppings in arguments.

On the other hand if you have a boolean param that changes how function works. That's questionable in my opinion. Say you want to return list of users from DB but omit interns, sometimes, and you need to call API (or query DB) to know if someone's intern. You could define `omitInterns` bool argument but it seems clunky to me.

I may be mistaken, though. As said: defining boundries is not easy.

This also touches other problem a bit. Should we strive to decrease `if` branching in our functions or not? I personally tend to branch very early on, so later I can follow straight path. That's not always possible, but if it is, it helps greatly. Makes code easier to follow.

Re: Get rid of those boolean function parameters (2015)

#102
post #39

Named parameters misses the point. Functions should do one thing and only one thing. This is why we have cos, sin, and tan and not a universal function for trigonometry: trig(mode='cos', ...). Such functions often become cumbersome to use since they are essentially multiple functions baked into one.

function pizza(boolean pepperoni, boolean bacon, boolean mushroom, boolean artichoke) now becomes 16 distinct functions.

But it should just be:

  function getPizza(toppings: Iterable): Pizza {
    ...
  }

Re: Get rid of those boolean function parameters (2015)

#103

Earlier quoted context omitted.

> how would you refactor JS's fetch() as @flavius29663 said ( https://news.ycombinator.com/item?id=28593669 ) you can use the builder pattern FetchBuilder() .withUrl(ur) .withMode("cors") .withCache(true) .withHeader('Content-Type', 'application/json') .accept('*/*') .post() .then(response => response.json()) .then(data => console.log(data));

I'm not following how moving the options out of the function parameters and into the call chain makes the actual function more maintainable. It's still doing the exact same thing with the exact same options it's just pulling them from elsewhere. If anything you now have more functions to maintain on top of the function that does many different things based on the calling info. // The original "misses the point" trig(…

Personally I don't like using with prefix in builders, I was simply presenting an example from another comment.

the difference, IMO, is - in Javascript - that this

    // The style fetch() uses today
    trig({mode: "cos", typ: "hyperbolic"})
                       ^^^
would fail silently while

    // The builder refactor
    trig().mode("cos").typ("hyperbolic")(val)
would trigger a compilation error

but, IMO, passing objects is good enough most of the times, and I consider it a much better solution over passing boolean flags

Re: Get rid of those boolean function parameters (2015)

#104

Earlier quoted context omitted.

not necessarly. First of all, this function pizza(boolean pepperoni, boolean bacon, boolean mushroom, boolean artichoke) breaks down when you want to add ham, potatoes and sausages to the pizza. Secondly, you can optimize for the common case: fn pizza() # -> default pizza e.g. margherita fn pizza(list_of_ingredients) # -> your custom pizza if you we are talking of simple functions and not more complex patterns, such…

What kind of monster puts potatoes on a pizza?

You don't know what you're talking about :)

In the image: pizza with potatoes, a typical roman recipe

https://i0.wp.com/www.puntarellarossa.it/wp/wp-content/uploa...

Re: Get rid of those boolean function parameters (2015)

#105

Earlier quoted context omitted.

What kind of monster puts potatoes on a pizza?

You don't know what you're talking about :) In the image: pizza with potatoes, a typical roman recipe https://i0.wp.com/www.puntarellarossa.it/wp/wp-content/uploa...

I don't care who invented it. It sounds fucking terrible.

You might as well put some mashed potatoes in your rice and some pasta in a sandwich while you're at it.

It's like someone said "what type of carbs would you like with this meal" and the answer was "yes".

Re: Get rid of those boolean function parameters (2015)

#106

Earlier quoted context omitted.

You just moved the problem to a different layer. In your case you would end up calling your function like this: DoToppings(true, false, true, true); Whereas in my case you would call the builder directly with (only) the params you want different than the defaults. You could use named arguments, but that doesn't solve the problem completely. You will still have a large method signature, hard to use, harder to refactor…

> You just moved the problem to a different layer. No, I think you're misunderstanding. Weird... Aha: My fault, sorry. > In your case you would end up calling your function like this: DoToppings(true, false, true, true); What?!? Heck no, that wasn't what I meant, why would you think that? [Goes repeatedly clicking "parent"] Aha, I see: Sorry, the threads and sub-threads have branched so I got confused as to where we…

Duh, I meant

   function MakePizza(Fillings: PizzaFillings);
of course.

Re: Get rid of those boolean function parameters (2015)

#107

Earlier quoted context omitted.

You don't know what you're talking about :) In the image: pizza with potatoes, a typical roman recipe https://i0.wp.com/www.puntarellarossa.it/wp/wp-content/uploa...

I don't care who invented it. It sounds fucking terrible. You might as well put some mashed potatoes in your rice and some pasta in a sandwich while you're at it. It's like someone said "what type of carbs would you like with this meal" and the answer was "yes".

> I don't care who invented it. It sounds fucking terrible.

Sorry, but why should people care about what sounds terrible to you?

> put some mashed potatoes in your rice and some pasta in a sandwich while you're at it

If you weren't too obsessed with yourself, you'll know that that pasta actually exists, it's called "pasta e patate" and someone has put it in a sandwich for sure...

there is also a very popular variant made of pasta, potatoes and mussels.

> It's like someone said "what type of carbs would you like with this meal" and the answer was "yes".

It's like someone asked you "what are you first World problems" and your answer was "yes"

The recipe I'm talking about come from Italian rural tradition, when people were poor and carbs were the only thing they could afford to eat to keep being alive, not a privileged people's self inflicted fictional problem.

Sorry for the brutal honesty.

But you anglophones are not qualified to judge other culture's food. Your food is usually terrible.

Re: Get rid of those boolean function parameters (2015)

#108

Earlier quoted context omitted.

I don't care who invented it. It sounds fucking terrible. You might as well put some mashed potatoes in your rice and some pasta in a sandwich while you're at it. It's like someone said "what type of carbs would you like with this meal" and the answer was "yes".

> I don't care who invented it. It sounds fucking terrible. Sorry, but why should people care about what sounds terrible to you? > put some mashed potatoes in your rice and some pasta in a sandwich while you're at it If you weren't too obsessed with yourself, you'll know that that pasta actually exists, it's called "pasta e patate" and someone has put it in a sandwich for sure... there is also a very popular variant…

> Sorry, but why should people care about what sounds terrible to you?

I never asked you to care what sounds terrible to me.

Opinions are like assholes. Everyone has one, most are full of shit, and I don't really care if you don't like mine.

If you supposedly don't care what I think, why bother trying to tell me my opinion is wrong? You can't have it both ways.

> you weren't too obsessed with yourself, you'll know that that pasta actually exists

Ok sure buddy. It's totally on me that a meal that may consist of entirely carbs is not common/popular outside of Italy. Totally my fault.

> It's like someone asked you "what are you first World problems" and your answer was "yes"

Eating a diet of just carbs is literally not a first world problem, it's a 3rd world problem because people can't afford (or can't adequately store) proteins, fresh vegetables etc.

> when people were poor and carbs were the only thing they could afford to eat to keep being alive, not a privileged people's self inflicted fictional problem.

People have eaten much worse sounding things than your double-carb special, no doubt. The difference is - you're bollocking on like it's a perfect meal, and the idea that it doesn't sound appealing is apparently insulting to you.

Who could have possibly ever foreseen that a dish made out of necessity because people literally had nothing more than two kinds of otherwise bland starchy carbohydrates, would not seem appealing when other options are available?

An opinion isn't brutal honesty bub. You have one, I have one. The difference is I am well aware that mine is an opinion.

Good job on the generalisations though. "the food of half a billion people is usually terrible, here come try some carbs on carbs.".

Re: Get rid of those boolean function parameters (2015)

#109
post #95

Earlier quoted context omitted.

The next person to read that isn't going to say "Wow this is so clever." They are going to say "Wow this guy didn't know about booleans."

Did you just skip over the beginning of the discussion, where everyone agreed that having a bunch of anonymous booleans is bad for readability, and circle back to advocating the status quo that everyone else is trying to improve on?

Everyone doesn't agree that this is bad for readability, and the status quo is the status quo for a reason.

Re: Get rid of those boolean function parameters (2015)

#110
post #75
post #47

Earlier quoted context omitted.

I started doing this because of React but at this point ({}) is my default way of starting a function. The only thing I dislike is that it's not super ergonomic for explicit typescript declarations (but great when using typescript to check .js files).

I don't find it too bad to do interface IFunctionParameters { userId: string; name: string; age: number; } const example = ({userId, name, age}: IFunctionParameters) => {...}

The most annoying thing about this to me (and to be clear, I do exactly this all the time) is when you mouseover `example` at a use site, all you see is `IFunctionParameters`, not the definition of `IFunctionParameters`. At least in VS Code.
Post reply on HN