aspose file tools*
The moose likes Java in General and the fly likes if conditions v/s interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "if conditions v/s interfaces" Watch "if conditions v/s interfaces" New topic
Author

if conditions v/s interfaces

Chinmay Bajikar
Ranch Hand

Joined: Dec 08, 2001
Posts: 159
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.....


The strength of the Wolf is the pack & the strength of the pack is the wolf....Rudyard Kipling
Vijayakumar Arya
Ranch Hand

Joined: Jan 27, 2003
Posts: 76
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 ]

Thanks,<p>Vijay<p>The Hand that gives, Gathers.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Jan 23, 2003
Posts: 84
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

Joined: Apr 20, 2000
Posts: 3252
Indeed, polymorphism was invented to eliminate having conditional logic like this scattered all over the place.
- Peter
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Peter den Haan:
Indeed, polymorphism was invented to eliminate having conditional logic like this scattered all over the place.

Well said!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Chinmay Bajikar
Ranch Hand

Joined: Dec 08, 2001
Posts: 159
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: if conditions v/s interfaces
 
Similar Threads
find problems in my design/locking mech
A Hard Puzzle
Static methods cannot be override ?
Why do I need AJAX?
Deleted Flag