?
Solved

Refactoring the code base

Posted on 2013-12-13
8
Medium Priority
?
309 Views
Last Modified: 2013-12-28
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

0
Comment
Question by:roy_sanu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39716414
0
 

Author Comment

by:roy_sanu
ID: 39716778
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39716836
No
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:roy_sanu
ID: 39729302
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
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 39723679
That won't actually help you in the long run. Your business rules will likely become more, rather than less complex and it won't scale. In fact, shortening the code in that way (particularly using the ternary operator) will give you the same amount of spaghetti - it will just be even harder to understand and maintain.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39729755
:)
0
 

Author Comment

by:roy_sanu
ID: 39744402
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!


0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question