• Post Reply Bookmark Topic Watch Topic
  • New Topic

in IF statement, which is better? - using methods that return boolean value or boolean variables  RSS feed

 
Venny Tank
Greenhorn
Posts: 7
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm writing a program that displays first 100 emirp numbers (emirp is a nonpalindromic prime number whose reversal is also a prime). In my code I have an IF statement that invokes methods that return boolean values, like this:

The same result can be accomplished if these boolean values are assigned to boolean variables, like the example below:

My questions are:
Invoking methods in statements or assigning their values to a variable first?
Which is better from a real life programming prospective, what is the usual practice?

Thank you!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Venny Tank wrote:My questions are:
Invoking methods in statements or assigning their values to a variable first?
Which is better from a real life programming prospective, what is the usual practice?

As usual, the answer is "it depends".

It depends what you're doing, whether you're likely to re-use the boolean value and, at the end of the day, your own personal taste.

Personally, I'm quite happy with methods in logical expressions, so for any condition I was only going to use once I'd probably just leave it as the method call. For any that you're going to use more than once, you should probably put it in a variable (it probably runs marginally faster too).

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree, but it also depends on how good your method naming convention is adhered to. What you show is fine:but if you are stuck with something like this:I would say you NEED to have
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me just point out that in the first case (where you assign the values of the three methods to three variables), all three methods are evaluated. Whereas in the second case (where the methods are evaluated in the if-statement), the second and third methods will not be evaluated if the first method returns false (in this case).

That makes no difference if the three methods run quickly, like for example if they were just getters for hidden variables. But a method named "isPrime"... that might take some time to run, and you might want to avoid that time if you can.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say, Fred, that in the case of a method called checkIt, you should refactor the method to give it a decent name
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would say, Fred, that in the case of a method called checkIt, you should refactor the method to give it a decent name

Obviously, if the method name is under your control. That's not always the case.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would say, Fred, that in the case of a method called checkIt, you should refactor the method to give it a decent name

I agree, if possible. But you don't always own the code. It is possible checkIt is a method created by a third party vendor...
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Let me just point out that in the first case (where you assign the values of the three methods to three variables), all three methods are evaluated. Whereas in the second case (where the methods are evaluated in the if-statement), the second and third methods will not be evaluated if the first method returns false (in this case).

That makes no difference if the three methods run quickly, like for example if they were just getters for hidden variables. But a method named "isPrime"... that might take some time to run, and you might want to avoid that time if you can.


I think this is the most important thing that needs to be considered, because the way (first evaluating the methods to boolean then passing these booleans to if block / calling direct methods in the if block) may depends upon the personal programming taste. but placing methods (those are time consuming )to directly with "&&" might save the time of excecution.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj Kumar Jain wrote:
Paul Clapham wrote:Let me just point out that in the first case (where you assign the values of the three methods to three variables), all three methods are evaluated. Whereas in the second case (where the methods are evaluated in the if-statement), the second and third methods will not be evaluated if the first method returns false (in this case).

That makes no difference if the three methods run quickly, like for example if they were just getters for hidden variables. But a method named "isPrime"... that might take some time to run, and you might want to avoid that time if you can.


I think this is the most important thing that needs to be considered, because the way (first evaluating the methods to boolean then passing these booleans to if block / calling direct methods in the if block) may depends upon the personal programming taste. but placing methods (those are time consuming )to directly with "&&" might save the time of excecution.


No, it's not the most important thing. Most of the time, the methods will execute quickly enough that it won't matter. In this particular case, as Paul points out, the method isPrime() sounds like one that could be expensive to execute. In general though, a better approach would be to write the code in the way that is most readable unless you have a solid reason to believe that executing the methods will be unusually expensive.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:. . . It is possible checkIt is a method created by a third party vendor...
I hope they are paying you to use their code
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:. . . a better approach would be to write the code in the way that is most readable unless you have a solid reason to believe that executing the methods will be unusually expensive.
Agree. But if a method is expensive to execute, can you write your own code which will be any faster?
Compare that to the && and !! operators which probably can be executed in a single clock cycle.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Jeff Verdegan wrote:. . . a better approach would be to write the code in the way that is most readable unless you have a solid reason to believe that executing the methods will be unusually expensive.
Agree. But if a method is expensive to execute, can you write your own code which will be any faster?
Compare that to the && and !! operators which probably can be executed in a single clock cycle.


Sorry, not sure what you're getting at here.

My point, as applied to the code in question in this discussion, is just not to prefer


over



for performance reasons, unless you know that y() is expensive to execute, in which case you can avoid wasting cycles simply by taking advantage of the short-circuit nature of the && and || operators. No need to try to write your own faster method.

I'm also not saying don't use the first approach--just don't choose it for performance reasons unless you have a solid reason to think it will actually matter.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I think I misunderstood your original post. Sorry.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No worries. I'm just happy to have one less thing to be confused about.
 
Daniel Val
Ranch Hand
Posts: 44
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Venny Tank wrote:My questions are:
Personally, I'm quite happy with methods in logical expressions, so for any condition I was only going to use once I'd probably just leave it as the method call. For any that you're going to use more than once, you should probably put it in a variable (it probably runs marginally faster too).

Winston


There are two things to consider:

- If you use the values a couple of times in the sequence, then if you store them first in some variables will prevent the method to be called multiple times: you use the variable. On the other hand, if the method is simply an accessor, you don't gain big deal, but still, sometimes you don't know: if the boolean method does some hardcore thing, calling it the smallest amount of times pays;

- The code is much more readable with variables rather than having 1 km line with method calls in it.

So it is a win win situation: use variables.

D
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!