-3

There are plenty of nested if else statements in the legacy code (VB Script) which I want to migrate to a meaningful representation for easier maintenance in a Java application. Most of these statements are used to generate a sql statement based on user selection of parameters. Any suggestions to better model this logical branching problem as a data structure viz., directed graphs?

For example, if the parameters are:

age department grade

String finalSQL = “”;
if(department is not ‘HR’){
   // append something to finalSQL
if (age between 21-35)
{
  // append something to finalSQL
}
else if (age between 35-40)
{
// append something to finalSQL
}else{
// append something to finalSQL
}
} else {

if(grade > g7){
// append something to finalSQL
}else if (grade is g2 or g4 or g6){
// append something to finalSQL
} else{
// append something to finalSQL
}

}

return finalSQL;
Cid
  • 1,453
  • 1
  • 18
  • 37
  • Can you please show some pseudo code? The easiest way to avoid `if-else` statements is to take advantage of `polymorphism` – Chetan Kinger Apr 25 '15 at 08:57
  • share some code please – Panther Apr 25 '15 at 08:58
  • 1
    Could you give a more concrete example? There are some ways to avoid `if-else` chains (e.g. the [Strategy Pattern](http://en.wikipedia.org/wiki/Strategy_pattern)), but which one(s) to use depends on the exact problem. – Turing85 Apr 25 '15 at 08:58
  • Could you give the details like is there any nested if or it is simply if -elseif ....? – Blip Apr 25 '15 at 08:58
  • See http://stackoverflow.com/questions/29469195/how-to-refactor-code-to-avoid-multiple-if-s-from-interview/29476152#29476152 – Chetan Kinger Apr 25 '15 at 09:06
  • These if else statements are running for pages chunking some part of a bigger SQL. For better readability I have taken all the chunks of the SQL to a separate properties file. But then still I have to write all the if else statements which are present in the legacy code :( – Cid Apr 25 '15 at 09:27

1 Answers1

-1

One alternative of multiple if else statement where these conditions represents a specific state , then i usually go for State Design Pattern or command Pattern if they are simple commands

Think of vending mention where depending on state of machine like empty/non empty/no of coins, you have specific behaviour

Another readable way is switch- case statement but thats mainly makes it more readable

See this Converting many 'if else' statements to a cleaner approach

Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • *Another readable way is switch- case statement but thats mainly makes it more readable* -> this is not true. For primitives, `switch-case` is more performant. One way or the other, `switch-case` does not solve OP's problem. – Turing85 Apr 25 '15 at 09:11
  • My problem is that there are several complex combinations of input parameter values for which different string chunks are added to the final sql string. – Cid Apr 25 '15 at 09:29
  • 2
    i don't understand negative voting without reason . Seriously negative vote without reason should be banned – M Sach Apr 25 '15 at 11:48