Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:69641 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 99629 invoked from network); 17 Oct 2013 17:03:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Oct 2013 17:03:40 -0000 Authentication-Results: pb1.pair.com smtp.mail=jdavidlists@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=jdavidlists@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.171 as permitted sender) X-PHP-List-Original-Sender: jdavidlists@gmail.com X-Host-Fingerprint: 209.85.223.171 mail-ie0-f171.google.com Received: from [209.85.223.171] ([209.85.223.171:36176] helo=mail-ie0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DE/32-12663-C6810625 for ; Thu, 17 Oct 2013 13:03:40 -0400 Received: by mail-ie0-f171.google.com with SMTP id tp5so4460091ieb.16 for ; Thu, 17 Oct 2013 10:03:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=fDWQnlM3+H3vnczb4N4lTGQlsdS/uKZXjCwEdvK9np0=; b=GYYsCR1vSZRG8oWOMzOidJSITwPOSXYTV250AObMTgzzWOPomgZD3WNZuCdeHHeWBL /pEm/pRvDixzj17ajuFrX2FzTKCMELX2n+IE9EmGDDu6xU4nY68uEJVPvh26PZkW79ql lhEDthtP660rL5jwMvnd7MXTpPfVIdmwXh/a1XEXE5Do9w3jHZWbNtu6sG447xR8XUMT UNatzpHS8hxFj9xbzaqM7YlnoPw2CqZNZf2wvfm9xNLKR8X5sDrE09Z37UL86/mo9eDU /LUewbZoTiLPOO5K601vTiNY89sgEWjALWSw0aBruJZzmZNZ7GcE2oK1hd5r/nKTLOFi Ha/w== MIME-Version: 1.0 X-Received: by 10.50.73.66 with SMTP id j2mr7311320igv.1.1382029417689; Thu, 17 Oct 2013 10:03:37 -0700 (PDT) Sender: jdavidlists@gmail.com Received: by 10.43.180.131 with HTTP; Thu, 17 Oct 2013 10:03:37 -0700 (PDT) In-Reply-To: References: <525FC834.4060501@php.net> <1534105.bNC2os93J1@rofl> <525FDAC3.6060103@php.net> Date: Thu, 17 Oct 2013 13:03:37 -0400 X-Google-Sender-Auth: 81xcp7vlYMN25qUIrqwQ-Ga6uGs Message-ID: To: Nikita Popov Cc: Joe Watkins , PHP internals , Michael Wallner Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Assertions From: j.david.lists@gmail.com (J David) On Thu, Oct 17, 2013 at 12:12 PM, Nikita Popov wrote: > I would greatly appreciate a more detailed explanation as to how these > examples abuse assertions. All three use the failure of an assertion to alter the flow of execution of the program. Essentially you wrote, "Nobody is arguing that assertions should be used as a control-flow structure" and then provided three examples of using assertions to do exactly that. All three cause unexpected behavior if the asserted condition is not true and assertions are disabled because the condition was not evaluated. In all three of those examples, the behavior you want is easily obtained by throwing an exception. In fact, what some people might do in those situations is: assert($myObject->internalState == OBJECT_STATE_VALID, "internal object state not valid"); if ($myObject->internalState != OBJECT_STATE_VALID) throw FMLException("internal object state not valid"); (In this case, you would probably do the PHPUnit stuff with assertions disabled.) Assertions are a debugging/development check designed to facilitate proof of correctness. They are not present or used in production code. There is no danger that a failed assertion will ever cause a production app to abort. They serve one purpose, which is to bring things to a screeching halt when somethat that should never, ever happen has happened. And their value comes *because* they are independent of methods used to handle potentially-recoverable errors. Why do we need a new way to throw an exception? Why do we need to mess up another tool that has a different purpose to do it? What's wrong with writing the three-line function one time to get the behavior you want? It's totally reasonable to tie in to the stack-trace generating code to provide as much information as possible about why an assertion failed, similar to what exceptions do. But assertions must not be catchable through any normal mechanism, and the minute code execution resumes after a check fails, that check is absolutely a control-flow structure, and it is not an assertion. Thanks!