Last active
July 28, 2016 17:57
-
-
Save sprunka/c49e934bfb98fb21a02bc3c18b9223c8 to your computer and use it in GitHub Desktop.
Which is a better" coding practice, in this scenario? DRY or Single Level Indentation? ((Or is there a better way I'm just not seeing?))
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 | |
// DRY | |
if ($condition) { | |
$output->alterOnce(); | |
if ($secondCondition) { | |
$output->alterMore(); | |
} | |
} | |
return $output | |
/* ******************************* */ | |
// One level of indentation... | |
if (!$condition) { | |
return $output; | |
} | |
$output->alterOnce(); | |
if ($secondCondition) { | |
$output->alterAgain(); | |
} | |
return $output; |
if ($condition && !$condition2) {
$output->alterOnce();
}
if ($condition && $condition) {
$output->alterOnce();
$output->alterMore();
}
but i think I would go with second, i don't consider return $output
as duplication...
Ideally I would try to forge this into something where ->alter(Collection $changes)
so it works with 1 or more, but that's hard to judge without concrete examples.
Thank you both!
There was a bit more code between the innermost cheeky braces, just oversimplified here.
I am using the second format. I like single indentation and no else as mental exercises, as well as easier to read. This situation didn't have an else, but every time i find one in existing code, I try to refactor it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While single level indention is cleaner to read, the DRY approach would be what I would do.