Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if conditions v/s interfaces

 
Chinmay Bajikar
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a program which is working fine.
Now I need to add a boolean flag and based on the state of the flag I need to add some functionality in the code.
what i mean is,
if (flag)
{
//do this...
}
else
{
//do that...
}
Now I need to do this several places in the code.
But my manager does not appreciate doing this.
He wants something which will allow for the flag status chk only once and all the processing happening afterwards w/o a check.
The solution I suggested by me was to have 2 seperate classes(one for "do this" and one for "do that"),which implement an interface.
Based on the first chk of the flg we instantiate
the proper instance.This elimnates the flag state
checking each time.
But now my manager :roll: is worried if runtime loading of the class is heavier or repeatedly checking an if condition is heavier.
Plzz advice me(and my boss ) as which one of the approaches is better,
thanks,
Chinmay.....
 
Vijayakumar Arya
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is the do this and do that same across all the places or different.
If it is same try to put that as a separate method in the same class and you can invoke the method at appropriate place based on the flag. This will ultimately reduce the size of your class.
Alternatively you can re-structure your class as

Ofcourse, invoking a method in an object is time consuming than the if condition.
If you are definitely looking for performance you can go on with if checks rather than going for seperate class.
The class will be loaded only once per JVM session, i.e still the java process is running and it will not be loaded everything a method is invoked. Invoking a method is quite expensive than if condition.
Hope this helps
[ February 21, 2003: Message edited by: Vijayakumar Arya ]
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chinmay Bajikar:
[...] But now my manager :roll: is worried if runtime loading of the class is heavier or repeatedly checking an if condition is heavier.
The better approach is the one which is clearer, more expressive, and more flexible.
At this stage of the work, you should be concerned about the integrity and expressivity of your design, the effectiveness of your algorithms, and the clarity and maintainability of your code. PERFORMANCE SHOULD NOT BE A CONSIDERATION.
Please do not take this as an invitation to write stupid code; as I said, the effectiveness of your algorithms is important, as this determines the scalability (in the big-O sense) of your application. This includes your major data structures, the Collections classes that you use, etc. Counting CPU cycles, and making the code harder to develop or read to shave of a few cycles, is on the other hand an utterly misguided thing to do at this stage.
In the real world, having a polymorphic method call or testing a boolean is not going to make any difference whatsoever.
- Peter
[ February 21, 2003: Message edited by: Peter den Haan ]
 
Gopi Balaji
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have methods that depend on conditional statements, which are depicted as enumerated constants ( [true, false], [ON, OFF, DISCONNECTED], [X, Y, Z]) the state pattern as described in the GoF book is ideal.
See State Pattern.
-GB.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, polymorphism was invented to eliminate having conditional logic like this scattered all over the place.
- Peter
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter den Haan:
Indeed, polymorphism was invented to eliminate having conditional logic like this scattered all over the place.

Well said!
 
Chinmay Bajikar
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Thanks all of you for ur replies.
These are surely going to help when my manager
comes asking for a solution.
Thanks again,
Chinmay.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic