Hi Rowan, On Fri, Aug 5, 2016 at 6:04 AM, Rowan Collins <rowan.coll...@gmail.com> wrote: > On 04/08/2016 20:29, Yasuo Ohgaki wrote: >> >> One of my objective is following best practices. >> Prefer exception over error is one of them. Although, I strongly suggest >> to use exception for validation errors, I will have choices. > > > Best practice is to use exceptions to indicate an error *inside* the > function, i.e. something going wrong with the function itself. If I run (as > a simplified example) validate_var('abc', FILTER_NUMERIC) that's not > something going wrong inside validate_var(), so validate_var() shouldn't be > throwing an exception, IMO. > > Now, the *reason* I'm calling validate_var may be that the function I'm > writing will go wrong if the validation doesn't pass, but that's up to *me* > to throw an exception: > > if ( validate_var($foo, FILTER_NUMERIC) ) { > throw new InvalidArgumentException; > }
Input validation is like assertion shouldn't fail. However, it's possible to design code like this. > > That said, in another message you explained that this isn't intended for > "validation" in the sense of "form validation", but in a more restricted > sense of "dangerous data"; I think the name misled me, and may mislead > others, if that is the sole purpose of the function. I'm still not entirely > convinced, though, that a boolean-returning function wouldn't be enough to > fill both purposes. I understands there are use cases for both with/without exception. In fact, I implemented it without exception at first. My preference is to raise exception to encourage users to handle validation error correctly, i.e. terminate script execution, but my intention may not be interpreted correctly with or without exception. How about to have switch like assert()? It has exception on/off switch. Anyone prefer parameter flag? Regards, -- Yasuo Ohgaki yohg...@ohgaki.net -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php