Php – how to adhere to the Don’t-Repeat-Yourself (DRY) principle when there will be too many if-then-else making the code unreadable

code-reusedrymaintainabilityphp

I'd like to adhere to the Don't-Repeat-Yourself principle, but sometimes when I write PHP together with HTML and CSS, if I re-use the same code for different situations, my code soon will have so many if-then-else that the code is not easily maintainable.

This may be a bigger issue if Smarty, the templating engine is used, because most code editor won't match up {if} {else} {/if} So the programmer needs to look for the matching tag visually, and is not easy when there are 3 or 4 levels of nested {if} {else} {/if}.

In such situation, is there a way to stick to DRY, but still have good maintainable code?

Best Solution

Just so we have more info... What program are you using to write your code?

Tips for not repeating yourself:

Use some sort of templates. Doing this keeps you from having to repeat code for displaying content in each of your pages. I.E. If you have a site with 20 pages and you decide to change your layout, you don't want to have to go through and then change all 20 of your pages.

Use functions. If you have code that performs a specific task, DON'T write that code multiple times throughout your program/page. Create a function and then call it in each spot where you need that task performed. That way if you need to make a change you just modify that one function and don't have to search through your code to find every place that you performed that task. If you know about classes & methods (a method is a function in a class), for many tasks, this is even better as it provides you with data encapsulation and allows you to group related functions together so that you can include the class in future projects as needed.

If you are having difficulty with lots of if/else statements and code not being very readable there are a few things you can do:

1. Consider trying a new editor. Code folding is a must. Some editors also have vertical lines that highlight and match up indented code so you know what goes with what. If you want a decent free editor, I would recommend Notepad++ as it has both these features (just google it, I can't add links here).

2. There are techniques you can use to reduce the number of nested if statements that you have...

Example (this code):

if (item1 == true) {
   if (item2 == true) {
      item3 = 5;
   }
   else {
      item3 = 10;
   }
}
else {
   if (item2 == true) {
      item3 = 15;
   }
   else {
      item3 = 20;
   }
}

Can also be flattened out into:

if (item1 == true && item2 == true) {
   item3 = 5;
}
else if (item1 == true && item2 == false) {
   item3 = 10;
}
else if (item1 == false && item2 == true) {
   item3 = 15;
}
else {
   item3 = 20;
}

So if you have 3 or 4 levels of nested if/elses and you want to flatten them out, you may find your code more readable to use multiple arguments such as above. It does the same thing, it's just a matter of preference for which way you do it.

Try and not mix your logic (I'm assuming PHP) and your display (I'm assuming HTML/CSS). This is not always easy to do, but using templates and css, it is possible. Let me give you a practical example of how you can do this on a home page that displays a users name as a welcome message.

Inline PHP (try to avoid):

<html>
   <body>
      <?php
      if ($logged_in == true) {
         echo "Welcome ",$user->name(),"!";
      }
      else {
         echo "Welcome Guest!";
      }
      ?>
   </body>
</html>

Logic separate from display (better):

<?php
if ($logged_in == true) {
   $greetingMessage = "Welcome ",$user->name(),"!";
}
else {
   $greetingMessage = "Welcome Guest!";
}
?>

<html>
   <body>
      <?php echo $greetingMessage; ?>
   </body>
</html>

^ Keeping your logic separate from your view (HTML) will help you not get overwhelmed when your project starts getting really complex. It's best just not to mix! :)

Good luck man!

Related Question