Link to home
Start Free TrialLog in
Avatar of roy_sanu
roy_sanuFlag for India

asked on

Refactoring the code base

Hello experts

Let me know the better approach of writing the code as it has become difficult to maintain, please ignore the naming conventional, it has lots of if conditions which is hard to maintain.

Better  approach is recommended.

Thanks


private void startQuestion(String tttt) {
				// questionTemplate ---------------------------------------------------------------
				if(isEnable("xxxxx")){
					// yyyyy
					if(isEnable("yyyyy")){
						if("zzzz".equalsIgnoreCase(tttt)){
							enableFlag("QuestionUserRole");
							xxxxxUserRole=new SAXQuestionUserRole();
						}
						
					}else if("useryyyyy".equalsIgnoreCase(tttt)) {
						enableFlag("yyyyy");
						useryyyyy = new LinkedHashSet<SAXQuestionUserRole>();
					}
					// kkkk
					if(isEnable("kkkk")){
						if("xxxxxCustomerCategory".equalsIgnoreCase(tttt)){
							enableFlag("xxxxxCustomerCategory");
							xxxxxCustomerCategory=new Lxxxxx();
						}
						
					}else if("kkkk".equalsIgnoreCase(tttt)) {
						enableFlag("kkkk");
						kkkk = new LinkedHashSet<Lxxxxx>();
					}
					// lversion
					else if(isEnable("lversion")){
						// QRRV
						if ("QRRV".equalsIgnoreCase(tttt)) {
							enableFlag("QRRV");
							QRRV = new SAXQLV();
						}
						
					} else if ("lversion".equalsIgnoreCase(tttt)) {
						enableFlag("lversion");
						lversion = new LinkedHashSet<SAXQLV>();
					}
					
					// newLogicalVersion
					else if ("newLogicalVersion".equalsIgnoreCase(tttt)) {
						enableFlag("newLogicalVersion");
						newLogicalVersion = new SAXQLV();
					}
					
					// programs ---------------------------------------------------------------
					else if(isEnable("programs")){						
						// xxxxxProgram								
							if ("xxxxxProgram".equalsIgnoreCase(tttt)) {
								enableFlag("xxxxxProgram");
								xxxxxProgram = new SAXxxxxxProgram();
							}						
															
					}else if ("programs".equalsIgnoreCase(tttt)) {
							enableFlag("programs");
							xxxxxPrograms =  new LinkedHashSet<SAXxxxxxProgram>();
						}
					
					
				}else if ("xxxxx".equalsIgnoreCase(tttt)) {
					enableFlag("xxxxx");
					xxxxx = new SAXxxxxx();
				}
				

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of roy_sanu

ASKER

I am not asking  for rule engines but is there is any other approach to how to
reduce the cyclomatic complexity on the code like so many if conditions
No
I've requested that this question be closed as follows:

Accepted answer: 0 points for roy_sanu's comment #a39722056

for the following reason:

got the solution from the above link
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)
Hi,

I would like your thought also....................
One my friend suggested me to go for the reflection....

 His comments is below..

My   suggestion is to reduce no of "if" statements. One of my idea is to do it is using Java reflection APIs.
Based of SAX parsing input i.e "tttt", the handler dynamically invokes respective method, thus we can eliminate lot of "if" statements (Also code can be modular & readable using your Handlers).

Hope this helps!