Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Refactoring the code base

Posted on 2013-12-13
8
Medium Priority
?
324 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month20 days, 18 hours left to enroll

810 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