Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92966 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 59573 invoked from network); 29 Apr 2016 23:57:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Apr 2016 23:57:15 -0000 Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 66.111.4.29 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 66.111.4.29 out5-smtp.messagingengine.com Received: from [66.111.4.29] ([66.111.4.29:56455] helo=out5-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 77/00-58459-7C4F3275 for ; Fri, 29 Apr 2016 19:56:55 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 0698C204DB for ; Fri, 29 Apr 2016 19:56:53 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute3.internal (MEProxy); Fri, 29 Apr 2016 19:56:53 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=JPM2ajjn1fGOGa+ CzQISnrl2GUg=; b=Bgr4qS1M/+CB+RG89m87rshRbcnxmBtKqFsCJXQN7vp+C8Q Yot+f1b7fpkmEvqYJDohKlovo+BXBO+wwM3L3KqhkkkHBaVADekYQjls1HT/BmKp zI6wd8y2rZ5qDBWNntDgbuLkoWUqIXOT62W6T0cIjy5Qh4rjsnpZJB5MHYKU= X-Sasl-enc: ++6cF2qSdSMUUHkVlZOlmCHy5vLCp5sxFpd4x2KRwEFK 1461974212 Received: from [192.168.42.5] (c-50-178-40-84.hsd1.il.comcast.net [50.178.40.84]) by mail.messagingengine.com (Postfix) with ESMTPA id B155FC00013 for ; Fri, 29 Apr 2016 19:56:52 -0400 (EDT) To: internals@lists.php.net References: Message-ID: <5723F4C4.6080207@garfieldtech.com> Date: Fri, 29 Apr 2016 18:56:52 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Pipe Operator From: larry@garfieldtech.com (Larry Garfield) On 04/29/2016 06:37 PM, Bob Weinand wrote: > Hey, > >> Am 29.04.2016 um 21:58 schrieb Sara Golemon : >> >> This is one of my favorites out of HackLang. It's pure syntactic >> sugar, but it goes a long way towards improving readability. >> https://wiki.php.net/rfc/pipe-operator > Thanks for proposing, so that I am able to show what I don't like about the |> pipe operator: > > In general, the pipe operator is hiding anti-patterns. It makes anti-patterns look nicer. > > Take the first example: > > $ret = > array_merge( > $ret, > getFileArg( > array_map( > function ($x) use ($arg) { return $arg . '/' . $x; }, > array_filter( > scandir($arg), > function ($x) { return $x !== '.' && $x !== '..'); } > ) > ) > ) > ); > > Sure, it *looks* nicer with the pipe operator, if you really want to nest it. But that nesting is horrible in itself. It should be written as: > > $files = array_filter(scandir($arg), function ($x) { $x != '.' && $x != '..'; }); > $fullPath = array_map(function($x) use ($arg) { return "$arg/$x"; }, $files); > $allFiles = array_merge($allFiles, getFileArg($fullPath)); > > By limiting the nesting level to 2, the code gets itself much more readable and in addition, the variable names are self-explaining, so that you can jump right in. > As already mentioned, the |> variant looks nicer than the above, but it still lacks the most important part; documentation in form of obvious variable names after each small step. > > It's always nice when code can look nicely, but this doesn't encourage writing understandable code. This RFC is just providing another way to shoot any future readers into the feet. > Readers want to quickly grasp what is going on. Having to follow a whole list of operations whose result is written to a variable $ret is telling them absolutely nothing. > > TL;DR: > -1: The |> pipe operator encourages a write-only style. > > Bob I disagree. The absence of an intermediate variable name does not inherently hinder readability/debuggability (nor does its presence inherently improve it). It just makes more work for the code author to come up with potentially pointless variable names. What I see in |> is an end-run around the "can't call methods on arrays because they're not collections" problem. Viz, if we were dealing with an actual collection (as in Javascript, or many PHP libraries), we could do the following: $ret = $scandir($arg) ->filter(function($x) { return $x !== '.' && $x != '..'; }) ->map(function ($x) use ($arg) { return $arg . '/' . $x; }) ->apply('getFileArg') ->merge($ret); Which would be even nicer. (This is also where the short-lambda proposal would come in useful.) That's perfectly readable to me, and very clear what's happening. My concern, if anything, is that |> feels more like a stop-gap hack around our clunky an uncomposable array handling rather than a solution to it. I like the idea, I just wonder if we shouldn't fix the underlying problem and make functions better composable in the first place. --Larry Garfield