Reputation: 916
I'm working a large piece of code and for brevity sake find myself setting variables inside of if statements, example, if ( $subscriber = $this->subscriber->addSubscriber($request->all()) )
. Expanded example below.
It works great and probably is good indicator to use ternary statement. However, before I create a problem for myself that will result in late nights of refactoring.
I feel I need to ask if this problematic or bad practice? Should declare before use i.e. traditional approach? Sorry if this is basic question, but cannot find anything on the here or PHP forums.
public function postSubscriber(Request $request)
{
if ( $subscriber = $this->subscriber->addSubscriber($request->all()) ) {
return response()->json($subscriber, 200);
}
return response()->json("Oops! something went wrong with your subscription.", 500);
}
Upvotes: 1
Views: 51
Reputation: 161
No, as I see it, it isn't likely it will cause problems that will require refactoring. Re: whether traditional approach is more readable, that part of your question is entirely subjective and is best answered with 'what is most readable to you?'
Upvotes: 2