Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109042 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 97151 invoked from network); 15 Mar 2020 23:21:56 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Mar 2020 23:21:56 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BABB71804E0 for ; Sun, 15 Mar 2020 14:44:08 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 15 Mar 2020 14:44:08 -0700 (PDT) Received: by mail-qk1-f173.google.com with SMTP id t17so1090151qkm.6 for ; Sun, 15 Mar 2020 14:44:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=Vi4jSYVG0CDajNQkKuhGzg+w07p46NrB1xEQo3uu8Iw=; b=n5pChwoM40gdp51kiTLHFmhVmM4rAe5ZAZlkRydDrr7WPsBZrKMZItSUqd3/bDGEh+ ig2GNPUUigCUGfqTCzxeEiWDfILAxXegf6/Et36X9v8rfG5gUNlzPp2f8b5C6QSo+eIC FxZbmbCtL1DW26aRgXBIMOxM7A+Le9+fZst/XtG1R5iVcacL/DXXogKj78JprLFW/W9X 5EDI6Cg2b/2glNChfzR/tEiptPzJAdaOuwZa3Yk0Xcj39XgywevdO3aT18/pXqbwYhEu PtM/4s+stNbv9csu/Gs4VowqdsTJ+kSx2wwP4bZNTt97Sn2Y4KfR1EK/ds+itt9m0R5i xhyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=Vi4jSYVG0CDajNQkKuhGzg+w07p46NrB1xEQo3uu8Iw=; b=IWhfi8AtxWPpCciQROo4FkmqFY0T8W2HX0OZ7noAra9prYenOWqqY1PQ6lsHCJX22r vBgS9URO0LUpUwY/bL1j3Midg1YA/ItAcfaKSuIz6r1L4andM9LYPuZHSlLmCBhoN2fi 4mPlx2eM7vyyG8uPl1Y70JEQmHi8daXBb9gnbHtTKmamlz5Pf0UmTF2iemZicN3ep/Ak KkvDO7/TS6W0x2ULMgUdwk973yMZ55ObUyxcnE8HHLZOr0SPRbtwBf0uyjBmtD06Eu2C UcjOJe19/Tk3M08NoW7TQNITy+5b2GZCLPQUHHMDvcLz7ONKTIe/Rjv4cNkXxNQn4dfw n41w== X-Gm-Message-State: ANhLgQ2yL7Gilr0zl49GcudmjpSKkk80ZRXq5AP4a96Ms0OwgKvgEtwH 9PtmRSSnZIy3LxW7hA6OKPpLqtrfHmm7YQ== X-Google-Smtp-Source: ADFU+vv6g++j43OTYvzDEr7OyUelk3/GYWaaTp2KSjJ5SobqTuKmjgM6Fh4cchj9/zFAHbUf/pBBQQ== X-Received: by 2002:a37:a208:: with SMTP id l8mr21666944qke.302.1584308647638; Sun, 15 Mar 2020 14:44:07 -0700 (PDT) Received: from ?IPv6:2601:c0:c680:5cc0:8db9:b81a:6f2c:7616? ([2601:c0:c680:5cc0:8db9:b81a:6f2c:7616]) by smtp.gmail.com with ESMTPSA id x5sm894470qti.5.2020.03.15.14.44.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 15 Mar 2020 14:44:07 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) In-Reply-To: Date: Sun, 15 Mar 2020 17:44:06 -0400 Cc: Internals Content-Transfer-Encoding: quoted-printable Message-ID: <30C23964-6E52-4B2B-8947-8BD3E9E46354@newclarity.net> References: To: tyson andre X-Mailer: Apple Mail (2.3445.104.11) Subject: Re: [PHP-DEV] RFC idea: Block scoped variables with "let $x = expr" From: mike@newclarity.net (Mike Schinkel) > On Mar 15, 2020, at 2:53 PM, tyson andre = wrote: >=20 > Hi Mike, >=20 >> What I would rather see are new features in PHP that encourage = developer >> to break functionality into shorter and simpler functions and = methods, and to minimize nesting. >=20 > I would also like to see shorter functions in general, but: >=20 > - Other developers may prefer long functions/scripts despite my = preferences, > but their code would still benefit from readability by having = block-scoped variables enforced by the compiler What I am about to say is ironic since I too often feel people are = trying to enforce their view of coding on the world, but I do not think = we want to give people tools to make it easier to work with long = functions/scripts.=20 And while I do understand that other people can disagree, please = understand I am just stating my opinion here. > - Some problem domains are complex (e.g. business logic) and require a = lot of variables, > leading to long functions/scripts, and trying to split them up may = be error-prone > or cause awkward divisions of functionality. I would be very interested in seeing business logic coded into a long = function that literally cannot be made better by refactoring into = multiple smaller functions. If you have some, please do post to a Gist and share it to see if it in = fact it cannot be refactored and made "better." > - Even in short functions of a few dozen lines, it may be useful to = know if reused variables such as > `$i` are definitely scoped to a loop, when reviewing code. >=20 >> There is also an issue with block scoping that is reasonably = well-known >> among the GoLang community called variable shadowing >> and that is a developer seeing a variable in block scoping >> and thinking they are accessing or assigning the function-scoped = variable. >=20 > Variable shadowing is also an issue in JavaScript. > While JavaScript and GoLang don't allow introspection of the variable = scope > (as far as I know, not sure about reflection), > PHP does allow dynamic access by name ($$var, = `get_defined_variables()['var']` in a require()d file, XDebug), > and any proposal for block scoping in PHP will need to account for = that somehow. >=20 > - Aside: Personally, I set up linting for golang projects to forbid = shadowing. >=20 > My best plan for dealing with variable shadowing in PHP was to forbid = it entirely, > which I mentioned examples of in the proposal. >=20 >> { >> let $outer =3D 1; >> { >> let $outer =3D 2; // E_COMPILE_ERROR to declare in a = different (shadowed) scope, can only reassign >> $outer =3D 3; // this is allowed >> } >> } >=20 >> P.S. If you really need block scope within a long function or method = you can already create a closure and call it in the same expression: >=20 > I'm aware of this and have used it (e.g. to avoid modifying the global = scope in large projects), > but avoid it in functions due to the awkward syntax and the = performance overhead of function calls. Given the nature of long functions, I think the awkward syntax here = provides a nice disincentive to writing overly long functions. =20 But as I said, the closure does exist in case someone absolutely needs = to control the scope of a block within an existing long function, such = as the 1400 line function I've been refactoring for months now... > I'd generally just use a different variable name. >=20 > - Tyson