Avatar of roy_sanu
roy_sanu
Flag 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

JavaJava EE

Avatar of undefined
Last Comment
roy_sanu

8/22/2022 - Mon
CEHJ

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
CEHJ

No
Your help has saved me hundreds of hours of internet surfing.
fblack61
roy_sanu

ASKER
roy_sanu

ASKER
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
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
CEHJ

:)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
roy_sanu

ASKER
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!