merged branch rybakit/constant (PR #966)
authorFabien Potencier <fabien.potencier@gmail.com>
Sat, 26 Jan 2013 14:56:04 +0000 (15:56 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Sat, 26 Jan 2013 14:56:04 +0000 (15:56 +0100)
commitc3a9c538829c63f4643d1d5c46100dd6803b34a4
treea695c4242f3616d662387e3727b75f89e92cf609
parent93c6a8dedc46c73ccdd61342de3d5e761e4412f6
parenta1b7ec56e6dfc94977e9000ab1567beee8ab3239
merged branch rybakit/constant (PR #966)

This PR was merged into the master branch.

Commits
-------

a1b7ec5 Tweak twig_constant()

Discussion
----------

Tweak twig_constant()

---------------------------------------------------------------------------

by Tobion at 2013-01-16T10:52:56Z

Why is it better? The old version returns earlier.

---------------------------------------------------------------------------

by rybakit at 2013-01-16T11:25:39Z

It's better for several reasons, imho:
1. Negation has gone, which is better for perception
2. Single function exit point
3. The same functionality in less code

And what do you mean by "The old version returns earlier"?

---------------------------------------------------------------------------

by Tobion at 2013-01-16T13:33:22Z

It means the old version uses a variable less and we prefer early return statements when possible.

---------------------------------------------------------------------------

by rybakit at 2013-01-16T14:24:26Z

@Tobion, but how could it be "earlier"? In both cases the ```return``` statement goes exactly after the condition check:

1)
```php
if (!$object) {                   // 1. check condition
    return constant($constant);   // 2. return result
}
```
2)
```php
if ($object) {                    // 1. check condition
    // ...
}

return constant($constant);       // 2. return result
```
Do you want to say that case 1) is faster?