Support of method chaining is popular, used in JQuery, D3.js, or just about any lib:
alternative, is traditional
Now, method chaining is convenient. Because it's a linear syntax, no nesting. Nesting is harder to read and harder to write.
it works by having each preceding term returning a object that happens to have the next method as property.
- you cannot chain arbitrary functions.
- the dot chain syntax only works for method that exists as a object's property.
with function nesting, it's simple. It's just functions. Functions return results. You feed functions one after the other.
Actually, the method chaining way is not necessarily semantically more complex.
true, you cannot chain arbitrary methods. You can chain object + method only when they are designed to be used together. However, it's the same with functions. You cannot chain arbitrary functions that doesn't make sense together, such as string function and a number function.
then, thinking about the issue further…
it's not true now that the chaining function by dot notation is on par with nesting functions.
true, they both needs the return type match the next function's parent-object or input. But, the object-with-property way is not a precise way of types. That is, with function nesting, it works only if a function's output matches the input of next function. So, on the whole, we can roughly say, it depends on data type of the lang, or type checking.
all things considered, the conclusion is that, with function nesting, it's easy to understand, and that the type of an output must match type of next input, is easy to understand. But with dot notation chaining, the way it depends on object+property relationships, is complex to understand.
- Ontology of Postfix Notation, Method Chaining, and Unix Pipe
- On the Idiocy of Dot Notation of Object Oriented Programing Languages
- Method Chaining, Postfix Operator, and OOP
ok. What else? is it possible to write
Discussions at Google Plus https://plus.google.com/+XahLee/posts/Tw6pdF7v9nr
Ask me question on patreon