2

I do not understand how you think about it:https://wiki.php.net/rfc/nullable_types

when It is widely confirmed, that using nulls is bad practice

Where am I wrong?

thanks.

I'm not criticizing !. I do not understand the motivations

10

Nulls are “bad” because many languages make every type implicitly nullable – that weakens the type system considerably as every operation may or may not result in a null pointer exception. However, most of the time the expectation is that variables are not null, thus making potential null values even more insidious.

Explicit syntax for nullable types solves part of this problem: because the value may explicitly be null, you can be expected to check first. This strengthens the type system a bit.

More importantly, this makes the type system more self-documenting. With explicitly nullable types there is a clearer difference between “some type T, which could in theory also be null” and “a type that is either T or null – check the value before using”.

On a type theory level, this is just a special case of “union types” (i.e. either–or types). These are frequently used implicitly in dynamic languages like PHP, e.g. strpos() which returns an index or FALSE. That can lead to bugs (e.g. the index zero is also false-y). Being able to explicitly document that with a type system (and allowing potential bugs to be detected via static analysis tools) is great, especially for larger and more mature code bases.

  • 2
    Combining dynamic typing and implicit conversion is a bug-farm by itself, even if unrelated to the folly of universal nullability. – Deduplicator Jul 14 '18 at 14:23
  • THANKS! this is a good answer! for me... in practice nullable allows to identify and better manage this complication – BruceStackOverFlow Jul 15 '18 at 14:33

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.