• Post Reply Bookmark Topic Watch Topic
  • New Topic

Whats the best approach?  RSS feed

 
umar hathab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have one method which does some operations depending on a flag.I have to call this method from another method.But I already know the value of flag before calling the method.So I want to know which is the best way?should i call the method if the flag is true? or i should put the flag condition inside the method and call that method anyway?
the code may be like this?
1.Approach1.
boolean flag=false;
public void manage()
{
flag=true//some operatins
doSome();
}
public void doSome()
{
if(flag)
{
//do some thing...
}
}
2.Second approach
1.Approach1.
boolean flag=false;
public void manage()
{
if(flag)
{
doSome();
}
}
public void doSome()
{
//do some thing...
}
can u tell me the performance issues with both the approch...Question may seem silly..but I am eager to know..
Thanks..
A.Umar
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theoretically, putting the check before the method call will be faster.
Practically, you probably wouldn't notice the difference. A modern Hotspot engine might even decide to inline the method call, so there wouldn't be any difference at all!
So, you shouldn't care about performance, but about maintainability!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!