Am 26.08.2012 01:57, schrieb Ferenc Kovacs:

     > So you are saying that your (teams) IDE doesn't tell you the method
     > signature which contains also the default values?
     > I guess that the fact that many of the php core functions have
    optional
     > arguments and non null defaults must be really a PITA for you.

    I think what he was alluding to is the problem where one changes the
    default.

    All usages of the method/function then need to be updated. Using
    null today is very much like using the proposed 'default' parameter
    skipping RFC.


true, using null as a default value for your optional arguments means
that you don't really need the parameter skipping RFC (at least for the
functions that you control) or the named params, because you can pass
null for the in-between optional arguments.
ofc. most modern IDEs also have some kind of refactoring feature, which
would make it easier for you to update the function calls to reflect the
change in the default value, but it would be harder to tell the original
intention ($limit is 30 here, because it was the default and the caller
wanted to set the next optional argument, or $limit is intentionally set
to 30 and the next optional argument also happen to be needed).
thanks for clearing that one up for me.

Having every optional argument defaulting to null means that it can be
frequent scenario, when you have a variable, which is guaranteed to be
defined, but can be null.
But I still think that it isn't that frequent that you want to keep the
non falsely values or set a default value otherwise.
I mean if you expect a number, then you don't want your passed 0 to be
replaced with the default, or if you expect a string, you don't want "0"
or maybe even "" to be replaced with the default, etc..
So you couldn't use the ternary assignment for those scenarios.

Yes, it doesn't work in this cases, but I must say

- I try to avoid optional parameters whereever useful. The caller should be aware of what he is doing. I prefer to define "preferred values" as constants instead, that can be used whereever a preferred behaviour is wanted. - I do't implement very much functions/methods, where (e.g.) 0 is allowed and should _not_ be the default (same for strings and so on), thus '$index = $index ?: 0;' is valid regardless wether $index is 0 or null in the first place. Or if I expect a string the empty string '' is most of the time a special cases (or required). If I expect an object is just obvious, that they can never evaluate to false.

I don't think we need argue about this, because this two points are more or less personal preferences/opinions. But this said there are only some few edge cases (for me), where I need to distinguish between '', 0, null, ..., and in this few cases, I can still use the good old `$index = is_null($index) ? 123 : $index;` ;).

At the end: It works just great to use null as general-purpose-default. I don't care about a special 'default'-keyword, because in my mind something like that already exists in form of 'null': It's either "the default primitive value", or "no-object" (in case an object was expected, but not required).



--
Ferenc Kovács
@Tyr43l - http://tyrael.hu


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

Reply via email to