-3

I have a boolean to initialize depending on conditions. I am wondering what is the best way to do it, if there is one, according to code optimization, or if it is only a matter of personal taste.

Way #1:

boolean conditionIsTrue;
if(object == whatItShould){
    conditionIsTrue = true;
} else {
    conditionIsTrue = false;
}

Way #2:

boolean conditionIsTrue = false;
if(object == whatItShould){
    conditionIsTrue = true;
} 

edit: I am not looking for an alternative to change the code with object.equals or other, this condition was an example and usually they are more complex. I would like to do what is the best way only between #1 and #2.

George
  • 769
  • 4
  • 11
  • 31
  • 4
    Way 3: `conditionIsTrue = (object.equals(whatItShould));`. This is the way. – Luiggi Mendoza May 20 '14 at 12:49
  • `conditionIsTrue = object == whatItShould ? true : false;` – user184994 May 20 '14 at 12:50
  • 1
    @user184994 `object == whatItShould` already returns a `boolean`. Also, you should compare object references using `equals` rather than `==`. – Luiggi Mendoza May 20 '14 at 12:51
  • `conditionIsTrue = (object == whatItShould);` – Tripp Kinetics May 20 '14 at 12:51
  • 1
    I think it does not answer to his question. How to assign a value to a object in an If Else approach. Personally, I use the first way. It's clear, and you're sure to only achieve one assignment, so your variable is final. – lpratlong May 20 '14 at 12:52
  • @lpratlong for this case, you can avoid using `if-else`. – Luiggi Mendoza May 20 '14 at 12:53
  • @LuiggiMendoza : yes of course. But in an other case (and I think he asked also for that), you can choose between his two approaches. – lpratlong May 20 '14 at 12:55
  • thank you @lpratlong for understanding the question. – George May 20 '14 at 12:56
  • @user184994 I feel that this would be very hard to read when the check of the condition is more complex. – George May 20 '14 at 12:57
  • @user184994 Thats just as redundant as the existing if statement. You could remove everything after the `?` and the program would be unaffected – Richard Tingle May 20 '14 at 13:13
  • 1
    Incidently you mention optimisation; when I'm optimising I try to remove if statements whereever possible (even reasonable ones); they are branching instructions which can slow down the processor because the pipeline doesn't know ahead of time which branch the code will go down ([it does try to guess](http://stackoverflow.com/q/11227809/2187042) but it often gets it wrong). Assuming you care about code efficiency (aka this is in a bottleneck) and code optimization means speed the 'redundant if' will really slow you down – Richard Tingle May 20 '14 at 13:31

4 Answers4

3

Assuming object and whatItShould are primitive types and can be compared using ==, you should use ==:

conditionIsTrue = (object == whatItShould);

Assuming object and whatItShould are object references, you should use equals method:

conditionIsTrue = (object.equals(whatItShould));

Note that equals method, if not overridden, will end using == for comparing the references.


From your edit:

I am not looking for an alternative to change the code with object.equals or other, this condition was an example and usually they are more complex. I would like to do what is the best way only between #1 and #2.

You should post what you're looking for exactly, so there won't be any double interpretation for a specific programming problem.

Anyway, you may initialize the variable with a default value and define the real value after several conditions (using an int variable because boolean is misleading):

int myVariable = 0;
if (<condition1> && <condition2> &&
        (<condition3> || <condition4>) ...) {
    myVariable = //real value
}

This way, you avoid compiler problems in case not all the paths initialize the variable accordingly, which may happen when using your first approach.

But still, if your conditions are in one single line, it would be better using Ternary Operator:

int myVariable = (condition1> && <condition2> &&
                  (<condition3> || <condition4>) ...) val1 : val2;

Also, you can nest the ternary operators in case to have multiple paths:

int myVariable = (<condition1>) ?
    val1 : (<condition2> && <condition3>) ?
        val2 : val3;
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • @George read the update on my answer. And for future posts, explain your real problem (or something closer to it) instead of posting just a sample of what you're not looking for. – Luiggi Mendoza May 20 '14 at 13:01
  • sorry, I tried to make the question clear. I do wish to initialize a boolean and not an int, with an if-else statement. The if condition is often complex, but the else could be avoided by initializing the opposite. I don't know which way is better or if it matters. – George May 20 '14 at 13:02
  • @George post your *complex* real question then. I can bet it is not *that* complex. – Luiggi Mendoza May 20 '14 at 13:03
1

The first way look better for me. The condition and its consequences are clear, and you're sure to only make one assignment. So your variable is final also. But, it's not a panacea for performance.

lpratlong
  • 1,421
  • 9
  • 17
  • I'll accept this as it is the only one asking the question. Thank you for your input. I am afraid to wait for more answers as people seem to be quite aggressive today. – George May 20 '14 at 13:14
  • 1
    @George I think people are frustrated because they don't understand why you're restricted to those two (seemingly terrible) options. The more complex the condition becomes the more nasty the redundant if statement looks hanging off the end of it. You could convert it to a nice version by only deleting characters – Richard Tingle May 20 '14 at 13:16
  • Yes that's what I can see. I will sure try to be clearer next time to not offend anybody trying to help. – George May 20 '14 at 13:18
0

The correct way to do this would be using object.equals(anotherobject);. You can easily test why you should do this with Strings, using "A" == "A" and String a = "A"; a.equals("A");

  • Thank you but does not answer the question asked. See my edit. – George May 20 '14 at 12:55
  • @George it does answer the first question asked, and Luiggi's answer does too. We do not have a crystal ball to know what you want to ask, we answer the question that is asked. –  May 20 '14 at 12:57
  • Question is what is better between #1 and #2, only judging by the differences between the two snippets. – George May 20 '14 at 12:58
0

I think you are trying to avoid this construction (the optimum version of this), which can be a little confusing at first

boolean conditionIsTrue = object == whatItShould; //usual disclamer about this testing reference equality not value equality for objects

This, I accept looks a little....odd on first viewing, with the == and = hanging around. However, you are free to modify this so it looks more pleasing:

boolean conditionIsTrue= (object == whatItShould);

Furthermore for more complex conditions you can use newlines as you see fit

boolean conditionIsTrue= 
         object == whatItShould ||
         object == alternative ||
         (useBadAlternative && object == badAlternative);

The more complex the condition becomes the more pleasing it is to not have an if statement hanging off the back of it.

Notes on optimisation

You mention optimisation; when I'm optimising I try to remove if statements whereever possible (even reasonable ones); they are branching instructions which can slow down the processor because the pipeline doesn't know ahead of time which branch the code will go down (it does try to guess but it often gets it wrong). Assuming you care about code efficiency (aka this is in a bottleneck) and code optimization means speed the 'redundant if' will really slow you down (assuming a smart compiler doesn't remove it)

Community
  • 1
  • 1
Richard Tingle
  • 16,906
  • 5
  • 52
  • 77