DEV Community


Discussion on: Writing Clean Code

slifin profile image
Adrian Smith • Edited
function can_view(string $scope, int $owner_id) : bool {

    $is_public = $scope === 'public';
    $user_is_admin = Auth::user()->hasRole('admin');
    $is_private = $scope === 'private';
    $is_owner = Auth::user()->id === $owner_id;

    $current_user_can_view = $is_public || $user_is_admin || ($is_private && $is_owner);
    return $current_user_can_view;

Inline conditions inside if statements discard the opportunity for a name, as a maintainer I care more about the intent of the code at the time of creation than its implementation, please keep telling me intent through names at every opportunity

By breaking everything down I wonder if $is_private is even required?