▲ | qwertox 8 days ago | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
I don't see how this is hard to reason about, assuming this is the resulting code when using variables:
It also makes it easier to inspect the values after each step. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | jeroenhd 8 days ago | parent | next [-] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Correctly naming things is one of the harder challenges in computer programming. Putting effort into naming intermediates that you're going to throw out is a waste. Plus, the more variables you introduce, the more likely you'll accidentally re-use a variable name somewhere down the line. With PHP allowing variable initialization in one branch but not the other, and continuing execution by default when an undeclared variable is passed, declaring more variables can lead to an annoying class of bugs that would require significant (breaking) changes to the core language to completely eliminate. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | lawn 7 days ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Introducing a new variable every single line adda a bunch of cognitive load compared to the pipe operator. It's much easier skim with the pipe operator and it's more robust too (for example reordering is a pain with variables, it's easy to introduce errors). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | agos 8 days ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
I don't think inspecting this is easier than adding |> IO.inspect() to a pipe chain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | const_cast 7 days ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The main problem with this approach, as someone who programs in PHP daily, is it pollutes the scope. That makes debugging much, much harder - you lose track of variables, and the current state of the program is complicated. IMO, if a value is a throwaway, like an intermediate, we shouldn't be able to use it. So method chaining or nesting function calls prevents them. Then, when we break after these functions, we can't see fake values. It also prevents someone in the future mutating the throwaway values. Someone could easily insert logic or a call that mutates something in the middle of this and breaks the chain. One way this is prevented in PHP is just using functions. But then you have functions just for the sake of scope, which isn't really what they're for. That introduces other annoyances. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | cess11 8 days ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Such variable threading tends to be harder to skim through in production code, the intermediates become noise that's harder to filter out than a repeated symbol like |>. Preferably you should also be sure that the functions are compatible with the data type going in and only rarely have to break it to dump data mid-chain. If you expect that kind of erroring it's likely a builder-chain with -> is a better alternative and do logging in the methods. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | kristopolous 7 days ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
It's easier to write, copy paste, compose and comment | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | r34 8 days ago | parent | prev [-] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Your version includes 4 variables. Pipes don't create those intermediate variables, so they are more memory efficient. Readability is mostly matter of habit. One reads easily what he/she is used to read. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|