Home  /  Questions  /  Question



60   95.3
Aug 30, 2010


Question on multiple if else statements in code

Hi,

I am working on a maintenance project and I see at lot of places the application logic is written in if..else if..else conditions.

Many times the if conditions check the value of a variable against a hard-coded string/number. I think there is something wrong with the code and it needs to be improved.

Can anyone please suggest whether I should change such code with any specific pattern?
Also, it would be helpful if someone could share an example/link that shows sample conversion code.

Thanks in advance.



50   50
Sep 07, 2010
You MUST re-factor your code on first place to make it clean and readable (though it may add more LinesOfCodes

I have seen plethora of similar spaghetti code in PRODuction env. where the coder had left the organization or it was the NEED of hour to publish such code.

DPattern choice depends how complex is the algorithm inside the if blocks is?

Strategy is the pattern where you can put Different Algo int saperate subclasses and use them independently (same as If blocks)

Note that you can NOT configure mix of these Algo - Builder does that



1,128   99.9
Aug 31, 2010
Hi,

Without seeing your code, I would recommend you to take a look
at the strategy design pattern. There is a previous post on a similar topic
here (http://dofactory.com/topic/1158/replace-conditional-vb-net-select-case-with-strategy-pattern-how.aspx).


Good Luck!
 1 comment
 
One more link, helps to remove conditional check. http://www.codeproject.com/KB/architecture/FactoryPattern.aspx --- Saurabh Saxena  Sep 07, 2010