Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96848 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 84729 invoked from network); 12 Nov 2016 13:06:50 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Nov 2016 13:06:50 -0000 Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.44 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 74.125.82.44 mail-wm0-f44.google.com Received: from [74.125.82.44] ([74.125.82.44:33799] helo=mail-wm0-f44.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9A/12-35596-9E317285 for ; Sat, 12 Nov 2016 08:06:50 -0500 Received: by mail-wm0-f44.google.com with SMTP id u144so3547460wmu.1 for ; Sat, 12 Nov 2016 05:06:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=ipNSSaFV7BZdOqAEOMXObHFolkqQHmGpXemLDm4AQpE=; b=05jUHQEWizFOYhKG0rOLfy3MXpnVarzeMv5VvdivFEs5oIx10tKDcuL+msfIXb8nrq P1zX6jPK75DCwAYaoZEzeljkZkkFVbf7gvr+o5ruy68ratyLq1MD7PW4O29gtZSEbXC/ xcQHJTA/0zXuYYewLxSZRNzopgYh3kVA9ucmWR4Le3E++zAE5xZXHFaQpaBcDnUtS1zd +U3/bG9UDE3YbDEo+Y625d2mZ9psb3rQk9sivzXbYOeUJeSDDqNIX83wJ7cgooc9TY8g hT/BdI4FakPeKhLjQ6KDVl8YedaLtwlw2EjNv1B57vMZpPPOfGtnSUxznT8O4kaqKkLn dpKw== 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=ipNSSaFV7BZdOqAEOMXObHFolkqQHmGpXemLDm4AQpE=; b=Gleisuh8UanifDdxa1zXjq3iB5u4prCxuV6RBfUl04hAEX/JtPSy0nI3Xyqj6mj4e1 iTwC5QnAUB5CqWgYYhMl3QDnZhWWGTyJBKiZepltXu2fi+ht+XmtvRs5N1sd3I7GTNJB cGoQofsf09vh57KEeIBNeFu1iQyRcFl4n3Jlo/+W8Lt8hU12qQYtqfDO/wqoEl8iM0WA 2tggq0kDCv3XHOuh27nAH9EkVaVg5Gc1/5rJRJ/43AJi0BIARHY9McOCFWQLahj9Q8FH +PpM3bLGeztSpMg7m5lITeih9fqefmjuR1NHY8fRF5z4zDB28N9xJXsOULe2wvmR7S1r syvw== X-Gm-Message-State: ABUngvc/UhCHOKP1cV9HbZpGxHDbVXWfpHROFqiEtdZXWTZG6tCfkewEAi/GzCQvA6gLP3p1Qk5XTKEVUzl/xA== X-Received: by 10.194.158.100 with SMTP id wt4mr15971277wjb.148.1478956006545; Sat, 12 Nov 2016 05:06:46 -0800 (PST) MIME-Version: 1.0 Received: by 10.80.170.79 with HTTP; Sat, 12 Nov 2016 05:06:46 -0800 (PST) In-Reply-To: References: Date: Sat, 12 Nov 2016 14:06:46 +0100 Message-ID: To: Joe Watkins Cc: "Christoph M. Becker" , PHP internals Content-Type: multipart/alternative; boundary=089e0115ee3c689eba05411a479d Subject: Re: [PHP-DEV] Re: C89 vs. C99 From: nikita.ppv@gmail.com (Nikita Popov) --089e0115ee3c689eba05411a479d Content-Type: text/plain; charset=UTF-8 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 --089e0115ee3c689eba05411a479d--