Skip to content

Instantly share code, notes, and snippets.

@sprunka
Last active July 28, 2016 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sprunka/c49e934bfb98fb21a02bc3c18b9223c8 to your computer and use it in GitHub Desktop.
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?))
<?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;
@matthewtrask
Copy link

While single level indention is cleaner to read, the DRY approach would be what I would do.

@rdohms
Copy link

rdohms commented Jul 28, 2016

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.

@sprunka
Copy link
Author

sprunka commented Jul 28, 2016

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