1

I have been programming for over 15 years now. I consider myself a very good programmer, but I understand (like all of us) there are things that I need to work on. One of these things is code repetition when dealing with conditions. I will give a generic sample:

if(condition1)
{
     //perform some logic
     if(condition2)
     {
          //perform some logic
          if(condition3)
          {
               //Perform logic
          }
          else
          {
               //MethodA(param)
          }
     }
     else
     {
          //MethodA(param)
     }
}
else
{
     //MethodA()
}

Now, I cannot make it easy by doing the following:

if(condition1 && condition2)
{

}
else
{

}

I cannot do this since I need to perform some logic if condition1 is true and before I test condition2.

Is there a way to structure if...else blocks to where if you need to call a method in each else blocks, you are not repeating yourself?

gnat
  • 21,442
  • 29
  • 112
  • 288
Ethosik
  • 119
  • 2
  • I recommend reading up on [Karnaugh maps](http://en.wikipedia.org/wiki/Karnaugh_map). They are used to visualize a group of boolean conditions as well as reduce and combine them. What this means is you may be able to use such a map to reduce the number of conditionals and produce better-organized code. You will likely not map out booleans often, but understanding this concept is important to programming sanely. –  Aug 23 '14 at 22:28

1 Answers1

5

Your piece of code can easily be rewritten into:

if(not condition1)
{
    MethodA()
    return
}

//perform some logic
if(condition2)
{
     //perform some logic
     if(condition3)
     {
          //Perform logic
          return
     }
}

MethodA(param)

Then, a part of the code can be extracted into a method:

int Hello()
{
    if(condition1)
    {
        World()
    }
    else
    {
        MethodA()
    }
}

void World()
{
    //perform some logic
    if(condition2)
    {
        //perform some logic
        if(condition3)
        {
            //Perform logic
            return
        }
    }

    MethodA(param)
}

The custom logic in conditions is preserved, but you don't have code duplication.

Also, when code duplication is just a call of a method, this is not so annoying as if there was several LOCs repeated or if you were repeating a call with the same five arguments. Refactor it if it's easy; if not, duplication is fine.

Arseni Mourzenko
  • 134,780
  • 31
  • 343
  • 513
  • What about the few cases where I need to call the overload for MethodA? Look at if(condition3) above, the else block there needs to call MethodA(param) for the overload. – Ethosik Aug 23 '14 at 17:39
  • 1
    Where does this need multiple return *values*? Multiple return statements, yes, but virtually every language allows that. –  Aug 23 '14 at 17:39
  • @delnan: I edited the answer. Thank you for noting the mistake. – Arseni Mourzenko Aug 23 '14 at 17:48
  • @MainMa Thanks, this should be what I need! I always try to avoid using the return calls, is it considered bad practice to use them? – Ethosik Aug 23 '14 at 17:57
  • 3
    @Ethosik That's a pretty obsolete opinion IMO. Exiting early is always more readable than `{ { { { } } } }`-style pyramids like in your question. Seriously the logic is impossible to follow when it gets that deep and your eyes certainly cannot follow it top to bottom like reading a page. – djechlin Aug 23 '14 at 18:20