by honkycat · 5y ago · 0 comments
Their argument for replacing the pipe operator is to do this instead:
foo(X) ->
final_function(maybe_function(X)).
into this:foo(X) ->
Maybe = maybe_function(X),
final_function(Maybe).
instead of this:x
|> maybe_function()
|> final_function()
Their exact words:
Spelling things out so pedantically makes code dead-simple & clear. Yes, there is a tad more code, but you will also note that nothing is hiding. Un-nesting simply dumbs things down. Now, who wouldn’t want that after hours of squinting at a screen?
People love to trot out the "It's more explicit, its a bit more code but isn't it more READABLE" argument about everything. I've heard this argument used to oppose information hiding while refactoring functions to be smaller.
And I would argue that "readable" is subjective, and that their argument is extremely weak. I love the pipeline operator and think it should be standard in every language.
Also, his "Maybe" variable becomes a pain to maintain when you have a pipeline of multiple functions.
edit: for bad formatting
But for someone who is used to languages where these constructs (or list comprehensions) are idiomatic, they are perfectly clear and explicit, and using a for loop instead adds complexity.
If you're interested in what actually gets executed (if you're trying to optimize for performance, say), then the `for` loop is clearly explicit than the abstract counterparts.
It depends on what what you're looking for.
In what language? GCC will happily not just optimize away your counter variable but perhaps even replace your whole loop with an SIMD operation; I suspect the Go compiler will do the same.
Also, if you're trying to optimize for performance then looking at what code gets executed is (to first order) useless; the dominant factor is cache efficiency and you can't see cache hits/misses by reading the code.
Cache efficiency is important, but so is not running O(n^2) algorithms when you didn't mean to. The latter has an outsized impact on the performance of a program, and explicit loops makes it very clear when something's up.
It seems to me that one of the benefits of the pipe operator is that it very clearly lays out the steps involved, aka
x
|> maybe_function()
|> final_function()
so that the code is dead-simple and clear. It doesn't seem nested in the say way that final_function(maybe_function(X)) is.Granted, I don't have nearly the level of experience of the author so I can't say for sure, but it seems to me like they just haven't had enough experience with the pipe operator to see how it benefits the code "beauty".
foo(X) ->
Y = maybe_function(X),
final_function(Y).
Both, in my eyes, yield a declarative flow without mutations and overwriting variablesThere is, of course, still people asking for a way to specify the argument to pipe into, but I, for one, am very pleased this was never introduced (and there are no signs it ever will be).