DEV Community


Discussion on: Writing Clean Code

emandtf profile image

Sorry but your final "avoid nested code" alters the original method:
1) "owner_id" was never compared to anything in the original code, but you compared with "user()->id" field. In the original code it was just checked to be Valid/NonNull.
2) you never checked if "Auth::user()" returns a NOT NULL value before use it, so your code could give a NullPointer Exception if it returns NULL and you directly access to "->id" field.

gonedark profile image
Jason McCreary Author

You're right. I corrected the initial examples.

As far as the null check, it is indeed missing. But to your point, adding it would alter the existing behavior. I have left it off for simplicity.