Re: Nullsafe
| From: | Claude Pache | Date: | Tue, 03 Nov 2020 19:56:33 +0000 |
| Subject: | Re: Nullsafe | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112171@lists.php.net to get a copy of this message | ||
> Le 3 nov. 2020 à 17:38, Eugene Sidelnyk <zsidelnik@gmail.com> a écrit :
>
> Hello, internals!
> I am wondering why don't we use ordinary
-> operator
> with safe null
> handling? Programmers will be more prone to return null values. And thus,
> in most of cases ?-> will replace
> ->.
> Why do we need another operator, if we can implement null safe in current
> operator without BC breaks?
Hi,
In a parallel world where people never commit bugs, yes, this is reasonable semantics.
However, it happens more than once that a null value occurs by accident where an object is expected
by the programmer. In this case, I very much prefer to have a Warning or an Error, typically handled
by an error handler which sends a bug report to the developer, then triggers an emergency stop. That
makes it much easier to locate and fix bugs.
Of course, this debuggability facility have to be balanced with the inconvenience to having to write
more code in cases where a null value is expected. Having to write “?->” instead of
“->” in those cases is a minimal inconvenience. Here, I mean “minimal” in a very literal
way: exactly one character of difference.
Also, there is the following benefit: When I write “->”, I am stating that the LHS value
should never be null; when I write “?->”, I am stating that I expect that the LHS is
sometimes null. Thus, my code is documented in a very concise way (between zero and one character),
which is an aid for any future reader (including myself).
Aside: The name “nullsafe” is misleading. The operator is not about safety, it is about handling
null values in a concise way. In fact, in some circumstances, sweeping an unexpected null value
under the rug may be totally unsafe.
—Claude