Good article, thank you. For me it turned something that was more than a "vague feeling" into reasonable position.
There is one thing I'd like to add. In the example:
$customer = Customer::find($customerId);
imo $customer === null is the only reasonable check. instanceof should be used only when we expected find method might return several various instances. In other words, by checking with instanceof we ask "Who are you?", and checking for null - "Do you exist?". And these checks doesn't exclude each other and could be combined when needed.
Full stack web dev.
Studying FP web development approaches, while helping Mission Bit create paths to programming for underserved public school kids.
Previously @ Gradescope.
Just a style thing, but I agree... e.g. $customer instanceof Customer is less explicit than a null test and won't work with things like null object pattern or duck typing.
instanceof should be used only when we expected find method might return several various instances.
This actually seems to be case with Laravel, which is what i had in mind when writing this. Bad interfaces and accidental complexity makes you do weird workarounds. :)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi Aleksi,
Good article, thank you. For me it turned something that was more than a "vague feeling" into reasonable position.
There is one thing I'd like to add. In the example:
$customer = Customer::find($customerId);imo
$customer === nullis the only reasonable check.instanceofshould be used only when we expectedfindmethod might return several various instances. In other words, by checking withinstanceofwe ask "Who are you?", and checking fornull- "Do you exist?". And these checks doesn't exclude each other and could be combined when needed.Just a style thing, but I agree... e.g.
$customer instanceof Customeris less explicit than a null test and won't work with things like null object pattern or duck typing.This actually seems to be case with Laravel, which is what i had in mind when writing this. Bad interfaces and accidental complexity makes you do weird workarounds. :)