Avoid Code Repetition in Condition Statements
Posted
by
Ethosik
on Programmers
See other posts from Programmers
or by Ethosik
Published on 2014-08-23T17:06:57Z
Indexed on
2014/08/23
22:33 UTC
Read the original article
Hit count: 165
language-agnostic
|conditions
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?
© Programmers or respective owner