Hi,

On Fri, Apr 13, 2012 at 15:00, Nikita Popov <nikita....@googlemail.com> wrote:
> On Fri, Apr 13, 2012 at 11:37 AM, Pal Konyves <paul.kony...@gmail.com> wrote:
>> I don't see the point of empty( function() ).
>>
>> You tipically use empty on values that holds information you want to use
>> later in the program flow (a string, an integer). That means you'd better
>> extract it to a variable because you want to avoid calling twice a function
>> that provides this value.
> You don't always need the value.
>
> Browsing around Google it seems that one of the most common source of
> the function call in write context error is people trying to do
> if(empty(trim($xyz)). You don't necessarily need the result of
> trim($xyz), so it's reasonable not to save it into a temporary
> variable.

well, empty(trim()) actually is a good example for when you don't want
to use empty(). People typically assume empty checks for the empty
string here, and it doesn't.

>
> Another example for example would be a function like this:
>
> public function isValid() {
>    if (...) { $this->addError('xyz'); }
>    if (...) { $this->addError('abc'); }
>    if (...) { $this->addError('foo'); }
>    if (...) { $this->addError('bar'); }
>
>    return empty($this->getErrors());
> }
>
> Furthermore you don't necessarily have to throw the return value away.
> For example I commonly write code like this:
>
> if (null === $result = $this->foo()) {
>    throw new Exception(...);
> }
> $result->doSomething();
>
> You can do something similar with empty():
>
> if (empty($values = $this->getValues()) {
>    return;
> }
> $this->doSomethingWith($values);
>
> I know, not everyone likes that kind of coding style, but I think it
> has it's uses.

I believe the change is good, but only because it removes an annoying
error condition that is hard to figure out for beginners.

Other than that I don't believe it is useful though: people that know
what empty() really does can use "!expr" when expr is not a variable,
which is less confusing, especially for strings.

Best,

>
> Nikita
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>



-- 
Etienne Kneuss
http://www.colder.ch

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to