I am a Bad Programmer (it's not my day job, though I have managed programmers for many years), so maybe I am just missing the point in examples like this - but I often find this is the case in many code samples for language features. They usually have a trite example that shows how it can work in a really basic case.
This is helpful from a purely technical level - as here, I can see what is going on neatly.
But they rarely seem to show it being used how the designer expects it to be used in the wild to save time, or increase productivity, or whatever. Like, why would anyone want to do this?
I would personally NEVER want to see a function like that in a codebase when it could be written so much more clearly as in your examples. Although I personally would prefer a third style:
... more verbose, but I never see the point in trying to save a line if it sacrifices even a small bit of readability. Again, as a Bad Programmer, I like things to be spelled out super clearly whenever possible, so maybe that's just me.
This. I too am a bad programmer and readability is so much more important to me than the LOC count.
Sure, the pipes example might look nifty, but from a practical viewpoint, it's just not that useful. Next thing you know, someone throws an eval into the mix by mistake, and we end up with unintended consequences.
You (bouldersharp) already posted this 2 weeks ago. You seem to be very proud about this. But can you explain what the big deal is? I don't understand why anyone would want to use it.
One use case for this would be unstacking a bunch of array_* functions since the nesting can start getting to be a little dense. Here's a common function I run into a lot:
function cleanIdsTraditionally(array $input): array
{
return array_unique(array_filter(array_map('intval', $input)));
}
Here, I think the added context of the pipe negates the enhanced readability of having each method on its own line. Anything more complex than this would benefit from being piped, but at that point, it would probably be better to break the steps down into more discrete functions anyways.
Overall, not a bad idea, but in practice, it would be hard to justify having around
Right now I see an example of how to write unreadable and hard-to-debug code
Being a senior PHP dev I would not let pass such code into our master branch, mainly because it is syntactic sugar that does not help with a specific problem and also does not attribute to a clearer and readable code at all.
Remember: In a company, where we rely on the code to earn our living the two factors readbilty and testabilty are the single most important ones. Because if you are off to holiday and your coworkers can't get a grasp of your (seemingly) clever code, than the whole company has a problem.
Whenever a method merely calls other methods in sequence, returning the result of the last call, it's just too easy for me to simply use a pipe function. I guess it's mostly a matter of preference.
PHP can't autoload functions, only classes. Thus if you wrap it in a static function inside a class you can autoload the the function by calling the class, Pipe::new(...), without doing a require "file.php".
That's what I was wondering too, especially considering there is a helper function that just wraps it. Seems rather unnecessary.
<?php
function pipe(...$args) {
// Retrive first argument as initial value
$value = array_shift($args);
// Call the functions passing $value and assign its return
foreach($args as $func) $value = $func($value);
// Return computed value
return $value;
}
echo pipe("10 WAYS to EAT more HEALTHY",
fn($s) => strtolower($s),
fn($s) => str_replace(" ", "-", $s)
);
Perhaps you could extend the Pipe class and thus the pipe() method returns an object for chainability? That's the only reason I can immediately think of.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian Kernighan
Surely this is simpler and more succinct?
Even if you wanted to break it up into two lines: A better example might go a long way here.Edit: Never walk away to refill a coffee before pressing submit or the rest of HN will beat you to it. :P