Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96850 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 89048 invoked from network); 12 Nov 2016 13:21:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Nov 2016 13:21:26 -0000 Authentication-Results: pb1.pair.com smtp.mail=pthreads@pthreads.org; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=pthreads@pthreads.org; sender-id=unknown Received-SPF: error (pb1.pair.com: domain pthreads.org from 74.125.82.68 cause and error) X-PHP-List-Original-Sender: pthreads@pthreads.org X-Host-Fingerprint: 74.125.82.68 mail-wm0-f68.google.com Received: from [74.125.82.68] ([74.125.82.68:34317] helo=mail-wm0-f68.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E5/03-35596-55717285 for ; Sat, 12 Nov 2016 08:21:25 -0500 Received: by mail-wm0-f68.google.com with SMTP id g23so3267764wme.1 for ; Sat, 12 Nov 2016 05:21:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pthreads-org.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=XfPnytXZfCfnh+fiTfyzzXzqah7pWCWbjoXaRgZh4qs=; b=aRhaSAI9xIJzWI/94T5Tp0TMrubXfEr1CcVqRYEBuQZbhYuPmIrUPAlADxihFzwAIC hs8KKxm8rac8YWJ51X0lMOKRAwvy6Nnnlm89f75e+qenz7+uymsvRn0s7pXPBZUFaaGq G+9iVyni4ExzMVFgyFij9Fk/CyJLNzJRRyhoc/xX9MoevEo4e/PEktN7iDdq+bAwyRxr 5ENXok+/dMRcp38+A9cLwg/fe+gR4QxVsqTYO7D3XlxfuLoBX3wkf25x0IezuADo6Ur7 e8lJuAE31bo+qqlpSzmA7amowr2Bo2s8uiqqj11p8cnCueA11EWBkNfqxPDoYfjTdp7b UKZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=XfPnytXZfCfnh+fiTfyzzXzqah7pWCWbjoXaRgZh4qs=; b=Esok+V23M9xRbKZ02tBMLK7P868ZWz7LXM3AO9/wYbsQzqsBJeXWQBndzSaYVdD5J7 vjQBlj0HbevJ2o02Kcu9zydMJqig1GURR1BnJxR+99dYsQtllIgExBpXie8YW5kmgvnL 1OYIH75VSso2SPbDh1iV6ULqGpfLydTvCC4DsRY42A+O+gi+SmLagfT3LzTTSAFwjn5m qEhm68tjsVzLJ1h5CbgOfpPAmSDWn8GswAFrXggQ106fjG38S4M9iHvjT7cDEgxVKCJZ 3TYCcnTIJZlRJpiZj2pKQIvzI+4d6/Y62oAijGmlq2gh38Q+Bq27SjFB5fRz0Ybwf2Lw yvmg== X-Gm-Message-State: ABUngvfEVYX/a0iv6lZZemIltzTKR6u3bG+F4xFPCEjXsqKqgFUp8PKGjc9Eq+SCG9KeSIRkVaPdKqb0/aJiMQ== X-Received: by 10.28.13.9 with SMTP id 9mr2421639wmn.50.1478956882437; Sat, 12 Nov 2016 05:21:22 -0800 (PST) MIME-Version: 1.0 Received: by 10.80.161.230 with HTTP; Sat, 12 Nov 2016 05:21:21 -0800 (PST) X-Originating-IP: [109.157.245.230] In-Reply-To: References: Date: Sat, 12 Nov 2016 13:21:21 +0000 Message-ID: To: Nikita Popov Cc: "Christoph M. Becker" , PHP internals Content-Type: multipart/alternative; boundary=001a11444d449dd2ef05411a7bd2 Subject: Re: [PHP-DEV] Re: C89 vs. C99 From: pthreads@pthreads.org (Joe Watkins) --001a11444d449dd2ef05411a7bd2 Content-Type: text/plain; charset=UTF-8 Morning Nikita, All good points, that it's hard to refute. However, right now, I know where all variables that are going to be used are declared, no matter the size of the function or it's complexity. If not at the very top, eyes get good at scanning for blocks. What I don't want is to have to scan already complicated code to find out when a variable was declared. We need to have rules of thumb, and unless someone can defend their use rigorously, I would say the rule of thumb that code shouldn't be mixi ought to be followed. At least it ought to be followed in the top layer of PHP (anything inside a PHP|ZEND_FUNCTION and such). A defence of elegance is legitimate, so long as their use does genuinely increase elegance. Such examples exist, but I think more examples of mixi code being harder to follow exist ... Cheers Joe On Sat, Nov 12, 2016 at 1:06 PM, Nikita Popov wrote: > On Sat, Nov 12, 2016 at 1:40 PM, Joe Watkins > wrote: > >> Morning, >> >> > okay, I'm only really interested in declarations mixed with code >> >> Not sure if serious ... but I will harass you to change code that is mixi >> [1]. >> >> I think actually disallowing mixing lends some readability and uniformity >> to the code in php-src, that I would hate to see disappear ... >> >> Cheers >> Joe >> > > Depends on the situation. > > Very commonly this arbitrary restriction requires me to rewrite code into > a way that is objectively worse, just to avoid repeating initializations. > Typical example: C99 allows me to write nice code like this: > > void foo(foo_t arg) { > if (!foo_valid(arg)) { > return; > } > > bar_t bar = get_bar(arg); > if (!bar_valid(bar)) { > return; > } > > // Lots of variables here for internal computation > type1 a = ...; > type2 b = ...; > type3 c = ...; > type4 d = ...; > } > > The important part is that I can easily do an early-return. C89 instead > forces me to do either this: > > void foo(foo_t arg1, bar_t arg2) { > bar_t bar; > type1; > type2; > type3; > type4; > > if (!foo_valid(arg)) { > return; > } > > bar = get_bar(arg); > if (!bar_valid(bar)) { > return; > } > > // Lots of variables here for internal computation > a = ...; > b = ...; > c = ...; > d = ...; > } > > Which I find incredibly ugly, as its repetitive and declarations are far > removed from their source. > > Alternatively what I can write is this: > > void foo(foo_t arg) { > if (foo_valid(arg)) { > bar_t bar = get_bar(arg); > if (bar_valid(bar)) { > // Lots of variables here for internal computation > type1 a = ...; > type2 b = ...; > type3 c = ...; > type4 d = ...; > } > } > } > > This is also ugly, because it creates deeply nested code. To avoid code > that nests many levels deep, what we often do instead (I'm sure you've seen > this often enough in the php-src codebase) is to instead create huge if > conditions. You know, the kind where the condition has 6 lines and contains > assignments on lines 3 and 5. This is part of the reason. > > That's one half of the problem. C89 makes early-returns ugly, while I > think it is nowadays universally recognized that early-returns are > preferable over deeply nested code. > > The other half of the problem is that, as a rule, the lifetime of a > variable should be minimal to avoid misuse. If you have code like this > > type var; > if (xyz) { > // ... > } else { > var = 123; > use(var); > } > > then nothing prevents you from using "var" in the "if" branch, where this > variable is not initialized. If instead you write > > if (xyz) { > // ... > } else { > type var = 123; > use(var); > } > > this mistake fundamentally cannot happen -- the compiler prevents you from > making it. > > Both of these examples are fine under C89, but the same point also holds > within a single block. You want to limit where a certain identifier is > valid, so you don't use it outside the valid scope. > > That's my 2c. The problem with the early returns is something I run into > All. The. Time. I find it really annoying that I constantly have to rewrite > nice linear code into a deeply nested structure just to work around this > limitation. > > Nikita > --001a11444d449dd2ef05411a7bd2--