DEV Community

Discussion on: Writing Clean Code

Collapse
 
haraldkampen profile image
Harald Kampen • Edited

I don't think a return before the function ends is good code. A function or method should only have one return.

function canView($scope, $ownerId) {
$canView = $scope === 'public';
if (! $canView) {
$canView = Auth::user()->hasRole('admin');
}
if (! $canView) {
$canView = $scope === 'private' && Auth::user()->id === $ownerId;
}
return $canView;
}

Collapse
 
haraldkampen profile image
Harald Kampen

BTW: can somebody explain which keyword are starting and ending code snippets?

Collapse
 
gonedark profile image
Jason McCreary

As noted in the article, I don't believe in only one return. I admit there are trade-offs on both sides, in this case you are tracking $canView which carries it's own complexity.