Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67279 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 54226 invoked from network); 2 May 2013 22:46:57 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 May 2013 22:46:57 -0000 Authentication-Results: pb1.pair.com smtp.mail=slangley@google.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=slangley@google.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain google.com designates 209.85.216.43 as permitted sender) X-PHP-List-Original-Sender: slangley@google.com X-Host-Fingerprint: 209.85.216.43 mail-qa0-f43.google.com Received: from [209.85.216.43] ([209.85.216.43:55707] helo=mail-qa0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2E/03-37535-0ECE2815 for ; Thu, 02 May 2013 18:46:56 -0400 Received: by mail-qa0-f43.google.com with SMTP id bs12so62347qab.16 for ; Thu, 02 May 2013 15:46:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=2GfxyMvpE8ifmmHq8IGtVPJnMonY4TIGU+wlbpiOlsQ=; b=SSlg/iVhNLSPsY4KpDQ/U2025MleN9VGxhEB2536nh3bMmFkhY71g3ZplzoJa1FED4 czV5N+TAzQyvnySA+MDw9gQXbRX5x9RX/l15JeONggdgDZ/gJAncq8m7w97mrPcUtoAQ udyUf1KfvjL33DoKBzxAVBWRiZXYWI5sPs3pACWuJPQmm+vmtdZZxpYGJCwdXM1vkio4 hqrJm8oCJr745QPR10REbmslTZY+XBZajukSnDeCxV/h1hWraOQYJK2TekfQJAXeHRMM Fr3Ov6ZT5X2WsZYo5ga9igGb5uNh6eewtmimBR8U8jTFx/Q+TERm57nyJjwFwzZKerE0 Xm8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type:x-gm-message-state; bh=2GfxyMvpE8ifmmHq8IGtVPJnMonY4TIGU+wlbpiOlsQ=; b=PxE8B79G7vghwGaOiAEqiMpUynvPFrUPQ7n0UuUt31RXDLs75s85Pa8gD/q1/SowHR cltgty/NltLVKGlpSNZJIJKPcyoREq07IqoYF1pIw/UfhhWdguPRtcfebIclVB1GO8WH PsXctYUtNvZyH1XRQRnEqW25UrPlVZQiK7Qj13IgaJV0kug9OYkhWO+hnkC7QCSZKMI+ qYYU4JVtBXpZ+C//Hy4IdjXJzWEybIWn/lBhVIMBWnc619Dk224ursqj0LMyOZ6Aoz4P HZXvl4DzvkT39RgRXwt2A5dPFxOv3CecPWgf5jnh/mS6+H9LibINlNnLHGFq4pw3Gzmf 3efQ== MIME-Version: 1.0 X-Received: by 10.49.49.36 with SMTP id r4mr10866208qen.65.1367534813457; Thu, 02 May 2013 15:46:53 -0700 (PDT) Received: by 10.229.101.8 with HTTP; Thu, 2 May 2013 15:46:53 -0700 (PDT) In-Reply-To: References: Date: Fri, 3 May 2013 08:46:53 +1000 Message-ID: To: Adam Jon Richardson Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=047d7b6783acc665c304dbc4024f X-Gm-Message-State: ALoCoQmZvvhBDi4AIg0UBUNt2J7awlqk5uRPWzgq8DL/DXa1cyfm8zJ52w78kamCj9exv7WsbBTQMyOlf4I0OAvqinznuZC9h+V77OE2VqvyQHG0pZE7ekSLQRRt7D909EB/i9SuG0Wozj+zEFiN79hJa4P4cNxoTLN0CZ3qt/fI7BEhcUc856jaHYlUcpLb/OqHFdo8FVOs Subject: Re: [PHP-DEV] A better error handling approach. From: slangley@google.com (Stuart Langley) --047d7b6783acc665c304dbc4024f Content-Type: text/plain; charset=ISO-8859-1 So is the feature you're describing is tuples and a use case of that feature is an easier way to do error handling? On Fri, May 3, 2013 at 8:00 AM, Adam Jon Richardson wrote: > PHP currently has two separate approaches to handling errors: > - Errors > - Exceptions > > Both have their issues. > > Using and responding to errors requires that the returned value (if > there is one) perform double duty as both a potential valid response > AND, in the case of error, a flag that something went wrong. It's easy > to forget to check that a particular call succeeded and specifically > handle the error. I find myself constantly revisiting the > documentation to ensure I know the specific error flag for functions, > and performing post-hoc analysis of error logs to make sure I didn't > miss an error flag that I would be better served by handling in the > standard flow of my app. > > Using and responding to exceptions is a heavy-handed approach, as any > particular exception can bring the entire application down. > Additionally, the process of raising and catching exceptions loses > context of the error, often requiring many different subclasses of > exceptions to maintain the error state. Issues arise when the language > constructs for exceptions are liberally used in situations that are > not truly "exceptional," and where the the exception handling becomes > a glorified goto. > > I've been programming in Go, and the ability to pass back multiple > arguments, one of which is conventionally the error state, is > wonderful. It's simple, clear, concise, and powerful. The code calling > into the error-capable function knows the context and can handle > errors appropriately according to the application requirements. > > In PHP, we could provide the ability to automatically pack up a > comma-separated list of values as an array: > > return $var1, $var2; > // returns [$var1, $var2]; > > And, we could provide the ability to automatically unpack an array > into separate variables: > > $var1, $var2 = myFunction(); > // same as list($var1, $var2) = myFunction(); > > This functionality would allow us to embrace the convention that an > error is passed back as one of the return values: > > $dbh, $err = db\connect(); > // check for error > if ($err) { > // handle db connection error > } > // carry on normally > $dbh->query("blah blah blah"); > > The simplicity (just passing back values), clarity (the code to handle > errors is proximal to the code that caused the error), and efficiency > (not having to create bunches of exception subclasses to maintain > context/information, and exceptions are expensive) of this approach is > a beautiful thing. You can see an explanation of Go error handling > here: > http://blog.golang.org/2011/07/error-handling-and-go.html > > Concern 1: Isn't this just saving a few keystrokes? > > Allowing developers to pass back an array and then access the members > of the array isn't the same thing, and we're not just saving a few > keystrokes if this is implemented. Providing this ability promotes > usage and clarity, and I'm confident Go would not have gotten > developers to follow this convention if the syntax didn't facilitate > the approach. I now write my Python code to handle errors in this same > manner, taking advantage of its ability to handle comma-separated > lists as tuples (something I didn't know about until recently, I must > confess): > > http://stackoverflow.com/questions/423710/return-more-than-one-value-from-a-function-in-python > > Concern 2: Why don't you just use Python or Go? > > I really like using PHP for web development. > > Concern 3: Are you saying we should get rid of errors and exceptions? > > To be clear, errors have their place (maybe an extension failed to > load, or a warning about usage of undeclared vars, etc.), and so do > exceptions (for events that are truly "exceptional", that should cause > a major revision to the branching/flow of the application and/or stop > the application in its tracks if left unhandled.) > > Adam > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --047d7b6783acc665c304dbc4024f--