1

In my code there are number of If(s) without else conditions (really else not required) but I think should there be any other code which has much efficiency than multiple if(s). FYI my if are like if(Some And Or NOTs) if(conditions AND OR NOTS) ...?

Please help to optimize the code as it has increased to much huge volume?

Sankalp
  • 2,030
  • 7
  • 30
  • 41
  • 2
    Could you post example of your code? – Mikita Belahlazau Sep 20 '11 at 07:46
  • 4
    Just write clear code: don't try to be "smart": http://java.sun.com/developer/technicalArticles/Interviews/devinsight_1/ – Bart Kiers Sep 20 '11 at 07:47
  • I wouldn't call `Nested Ifs` unoptimized code. – mohdajami Sep 20 '11 at 07:50
  • 1
    @BartKiers HOW CAN I MAKE SURE MY APP IS NOT TAKING TOO MUCH OF EXTRA TIME AND REDUCING THE PERFORMANCEs as there are multiple and or nots in If condition. – Sankalp Sep 20 '11 at 07:52
  • @LondonDreams, by profiling your app, not by worrying about the things you mention in your OP. Please take the time to read the link I posted. – Bart Kiers Sep 20 '11 at 07:53
  • Whatever you decide to do to your code, make sure you benchmark it to see if you actually gain anything. There's no point in making code less readable if it isn't going to speed it up. – Mysticial Sep 20 '11 at 07:54
  • The answer depends on the nature of what you're actually doing. Performance is unlikely to be a real concern, unless the conditionals include long-running methods. Make clarity your first goal, it will often lead you to a better solution. – Dave Newton Sep 20 '11 at 08:31
  • [http://stackoverflow.com/questions/234458/do-polymorphism-or-conditionals-promote-better-design] is quite more clearing concepts. – Sankalp Sep 20 '11 at 11:22

4 Answers4

2

replace Conditional with Polymorphism - < refactoring , improving the design of existing code> ,write by martin fowler

swanliu
  • 781
  • 3
  • 8
  • can u pls add few lines of hint here ? – Sankalp Sep 20 '11 at 07:55
  • 1
    search web for 'replace Conditional with Polymorphism' – swanliu Sep 20 '11 at 08:02
  • The answer is poorly written, but it's still good advice. +1 to balance the downvotes – Sean Patrick Floyd Sep 20 '11 at 08:09
  • 2
    Agreed. If you look around there are some threads here on SO which illustrate the point. http://stackoverflow.com/questions/234458/do-polymorphism-or-conditionals-promote-better-design – Andrew Fielden Sep 20 '11 at 08:47
  • 2
    Also check out [this previous answer of mine](http://stackoverflow.com/questions/3786358/get-rid-of-ugly-if-statements/3788488#3788488) (including all up/downvotes and furious discussions and even [a guest appearance in DailyWTF](http://forums.thedailywtf.com/forums/p/20192/234306.aspx)) – Sean Patrick Floyd Sep 20 '11 at 12:42
2

I am not sure if you tried this already, but can you group your if statements into meaningful functions?could prevent code duplication, and sometimes for small stuff it's better then Polymorphism.

for example if you have:

Girl shirly;
Girl ruth;
if(shirly.pretty && shirly.smart && (!shirly.married)){
   ...
}
if(ruth.pretty && ruth.smart && (!ruth.married)){
   ...
}

//a better way will be
if(doILove(shirly)){
...
}
if(doILove(ruth)){
...
}
//or in case of a more general statment
if(doLoveCondition(shirly.pretty,shirly.smart,!shirly.married){
...
}
if(doLoveCondition(ruth.pretty,ruth.smart,!ruth.married){
...
}

if you would have posted the code, it would have been easier to find a specific solution.

ofir
  • 58
  • 6
1

You might want to look into the switch statement.

Brissles
  • 3,833
  • 23
  • 31
  • 3
    Quote from java tutorial (http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html): A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings) – Alex K Sep 20 '11 at 07:46
1

Profiling application is in my opinion the best answer to decide whether the code needs to optimized. It always has a two way answer too much optimizing even may cause performance loss and keep it simple make the code much readable and even optimized according to line of codes.

Sankalp
  • 2,030
  • 7
  • 30
  • 41