3 ms·
I find the idea that the ideal function takes no arguments to be astonishing. There are only two kinds of functions that take no arguments: 1. Those that retur
by mikeash 13y ago
I find the idea that the ideal function takes no arguments to be astonishing. There are only two kinds of functions that take no arguments:
1. Those that return the same result every time.
2. Those that mutate some internal state (or, roughly equivalently, those that inspect internal state being mutated elsewhere).
#1 isn't all that useful in general, although there are obviously cases where it's exactly what you want. For #2, while state is useful at times and a necessary evil in many other cases, it's hardly ideal.
Even a single parameter seems decidedly un-ideal. To do useful work in general, you're typically going to want to take two parameters: something to be modified, and the modification to make. This can be done with state (modify in place) or functionally (return a new object with the modifications applied). Once again, having a single argument seems to imply either something not very useful (basically a getter or some similar derive-a-value function) or something relying on mutable state. You need two arguments to achieve the sort of combinatorial power that makes functions interesting and properly useful.
- andrewaylett 13y agoI wonder if the book, which I assume is talking about Java, is ignoring the implicit zeroth argument which is the object you're invoking the function on. Obviously in C you have to make that parameter explicit, but I've got enough experience of both languages to know that people often don't consider the object to be a parameter to one of its member functions. So if you're converting that rule back to C, you'd need to add one to each element of the rule. The one parameter rule is saying that when special cases are common, a special function to handle it is a good thing. So writing `i++` rather than `i = i + 1` or `next(node)` rather than `skip(node,1)`.
- mikeash 13y agoThat would make sense, except for the part where Java doesn't have functions at all, but rather methods. I imagine you could call them functions, but I haven't seen it done, and that sort of terminology sloppiness leads exactly to confusion like this.
- grannyg00se 13y agoThanks for the responses. The text is definitely referring to Java methods as functions, and not counting the implicit object instance (this) as a parameter. The text also considers multiple parameters of the same type that are treated identically as lists counting as one parameter.
- mikeash 13y agoAppreciate the clarification. The advice sounds much more sane that way. It sounds like a terribly confusing way to put it, but maybe it's better in context.