Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107018 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 69073 invoked from network); 12 Sep 2019 23:09:50 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 12 Sep 2019 23:09:50 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id D12022D1FAC for ; Thu, 12 Sep 2019 13:45:48 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: X-Spam-Virus: No Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Thu, 12 Sep 2019 13:45:48 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id t16so29860993wra.6 for ; Thu, 12 Sep 2019 13:45:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=FaTLinNSb4Jaln7X0RMeQ74A2wiYlmguJcUIUvYo+N8=; b=StDAJBtktHsBfGy8oWdRaJW6zPbLIBIilrkPNLrU84EOS1ZvOGEt69dCKcygN98qZS PWXSqj9RGrofTL+CRpXMZaB4TSOQr+2EJd58+GHn9AxrrVkecB+kl2hxNKmztpajMxuB 84bHgWTIYpifo/UfReZe5GkFWGafANMwWyioTUGbJxsJd3+KSvajaQ1X92rj/DqfZppL Tesdqxrovta5ciYQihoFjxD3UNV1O0mGMX+BVN4+dWHbYuZGG821GfEF8z2hKAgGk70W 1OC7g0aT8WwkT6KbVXTs4ORgC7b3YMbye/QZM3iSAwKzdJBcl6WzHnv65nKYVtNDQBmf cHBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=FaTLinNSb4Jaln7X0RMeQ74A2wiYlmguJcUIUvYo+N8=; b=A9M1SV4lJ/jjAxh8MxvEFqnEVq+ofCegcpKQgEu1zvoocmTpz9C6CK860mMVQ87QFN On+2aZJgGJbeGlxdmFV84jqD5fjN5BXd10BGwZ+oRLXv+M7zy9Bk3pktd0LrC9aD5wKc b/hk+Idf/CmqpQ/EGoDxVp5egXF617vay9zvKjK7pvmo5vKUjt3R35Gq/vfa6UnHrEM9 bVLZARVRhJQSYhMGClMFx3HwUU5p8bcEoRzjQZpIjH7vnxxtnYlzhXlnnVG9v/Wz2Tbz nrDZFEe8v3HjbvmVQIeAG3Kynzfg/SJR9KIgD1CjmIOG2XiZsuYhN4gMdHwFt9Iq+CnO Z+jw== X-Gm-Message-State: APjAAAV3rZYDE/VdV66r7fML3rANxsFS8UMoZ7pBBH2vvwt05cLpOZWq +10Oa6ri3NkuafIP9fYkTBHHEjla X-Google-Smtp-Source: APXvYqzuK3GAnFjZQ7ZR/HuyJPfLZJ7PS3nVynCQZuZ4nHitwqolJeoK7nSfW1kY4eB3M5ROUTuNQw== X-Received: by 2002:a5d:4251:: with SMTP id s17mr3142440wrr.126.1568321146546; Thu, 12 Sep 2019 13:45:46 -0700 (PDT) Received: from [192.168.0.16] (cpc84253-brig22-2-0-cust114.3-3.cable.virginm.net. [81.108.141.115]) by smtp.googlemail.com with ESMTPSA id q10sm50399794wrd.39.2019.09.12.13.45.45 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Sep 2019 13:45:45 -0700 (PDT) To: PHP internals References: Message-ID: Date: Thu, 12 Sep 2019 21:45:43 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB X-Envelope-From: Subject: Re: [PHP-DEV] [RFC] Reclassifying engine warnings From: rowan.collins@gmail.com (Rowan Tommins) On 12/09/2019 15:43, Robert Korulczyk wrote: > One additional line will make your code much more obvious and easier to read and understand: > > $i ??= 0; > $i++; I don't find this code at all obvious: foreach ( $something as $foo ) {     $i ??= 0;     $i++; } I mean, huh? What's that zero doing there, is it resetting the variable every loop? Now, in that simple case, you can and probably should initialise the counter before the loop: $i=0; foreach ( $something as $foo ) {     $i++; } But that's not the example I gave earlier! The example I gave earlier was a multi-dimensional array: $foo = []; foreach ( $something as $key1 ) {     foreach ( $somethingElse as $key2 ) {           $foo[$key1][$key2]++;     } } Even using ??= the initialise-everything-before-use version looks like this: $foo = []; foreach ( $something as $key1 ) {     foreach ( $somethingElse as $key2 ) {           $foo[$key1] ??= [];           $foo[$key1][$key2] ??= 0;           $foo[$key1][$key2]++;     } } Again, the values are confusing: the end result will never contain an empty array at the first level, and will never contain a 0 at the second level. Those two lines aren't aiding the readability of that algorithm in any way; I have to read past them to find the actual business of the loop, which is counting something, using the ++ operator. What's more, they're not preventing any bugs either! If I accidentally reuse $foo from a previous loop, the extra lines won't reinitialise anything for me; if I initialise it to empty, the two loops are functionally identical. So that's where I came up with two suggestions to actually add to the language, rather than just taking away: - a more granular way to express that this code is not actually error-prone, combining the 3 lines back into one - or, a way to express the intent of the code more clearly, such as declaring the shape of an array Regards, -- Rowan Tommins (né Collins) [IMSoP]