Solved

Refactoring the code base

Posted on 2013-12-13
8
266 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
 

Author Comment

by:roy_sanu
ID: 39722056
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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 500 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now