Last active
August 29, 2015 14:01
-
-
Save aramonc/ed06833a16249039dbf2 to your computer and use it in GitHub Desktop.
How bad is it to have code like this for readability & maintainability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if($paramFetcher['owner'] != null && ($owner = $userMgr->getById($paramFetcher['owner']))) { | |
$org->setOwner($owner); | |
} | |
?> | |
vs. | |
<?php | |
if($paramFetcher['owner'] != null) { | |
if($owner = $userMgr->getById($paramFetcher['owner'])){ | |
$org->setOwner($owner); | |
} | |
} | |
?> | |
FINAL: | |
<?php | |
$owner = $this->getUserManager()->getById($paramFetcher['owner']); | |
if ($owner) { | |
$org->setOwner($owner); | |
} | |
?> |
I agree with the above.
I would probably create an isOwner() function (containing two separate if blocks) and call it to do the checks, returning a bool.
Reusable, testable, self documenting, and less cyclomatic complexity.
Ended up changing it. I realized I was duplicating some code to get users and that if the parameter for getById()
is null
, it will return null
. So there was no need to check $paramFetcher['owner']
individually.
Thank you all!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hmmm ... first thought was that the first way, imho, is best for both but then I started to doubt ... I'd go with #1 for only that, if you need to check more than that then go with #2