DEV Community

[Comment from a deleted post]
Collapse
 
mjrider profile image
Robbert Müller

and what would be wrong here by returning the empty array?

In this example the usage of null is bad. Because not finding anything is not an error/exception. And imposes extra (useless) checks on the callers code

Collapse
 
aidantwoods profile image
Aidan Woods

Perhaps I should have only included the code I was trying to highlight :p the particular line I was talking about was the the nullable named constructor:

if ($Inline = $handler::parse($Line))
{
    $Inlines[] = $Inline;
}

I'd agree that in the enclosing function, returning an empty array would be clearer/better. (The nullable is an artifact left over from that function previously only returning one object, which was then modified to return multiple). An array being a container object, has the privilege of an 'empty' or 'nothingness' state. Not all objects have this state (because it can not always make any sense) hence null (a separate type) being used to convey the 'nothingness' property.

In the nullable named constructor usage, no construction is not an error, and is a result that can only be determined by deferring expertise to the callee. Since it is not an error, there is also nothing to fix, the caller need not respond other than by continuing what it is doing. So IMO the exception is inappropriate for that particular usage.