0

I have a java private method and I am trying to find the best way to create an object inside the method. Following are two different approaches that I am trying:

Approach A:

 private void createObject() {
     Object A;
     if (conditionA) {
         A = new Object();
         //do some Action X with object A
     } else {
         //Do something without object A
         if (conditionB) {
             return;
         }
         A = new Object();
         //do some Action Y with object A
     }
     return;
 }

Approach B:

 private void createObject() {
     Object A = new Object()
     if (conditionA) {
         //do some action X with Object A
     } else {
         //Do something without Object A
         if (conditionB) {
             return;
         }
         //do some action Y with Object A
     }
     return;
 }

Obviously both the code snippets are working and without any issues. I am just trying to find out the better coding style and standard.

1
  • 4
    Tell us more about your intended use-case and the problem you're trying to solve instead. Perhaps you've overly complicated the matter. Commented Jan 24, 2019 at 8:58

4 Answers 4

1

Let's write the truth table

  A  |   B   |      Expected Outcome               
-----+-------+-------------------------------
True | True  | do X with object A               
True | False | do X with object A               
False| False | do Y with object A               
False| True  | do something without object A 
-----+-------+-------------------------------

I think this translates to:

boolean doXWithA = conditionA;
boolean doYWithA = !conditionA && !conditionB;
boolean doSomethingWithA = doXWithA || doYWithA;
if(doSomethingWithA)
    Object a = new Object();
    if (doXWithA) {
        // do X with object A 
    } else if (doXWithB) {
        // do X with object B 
    }
} else {
    // do something without object A 
}

Good practice is to reduce the scope of variables as much as possible. And if the instantiation is expensive, you also want to reduce the number of time you instantiate it. Here Object a is only initialised when necessary and its scope is as small as possible (only one if block)

Sign up to request clarification or add additional context in comments.

1 Comment

The do something are two different things. Edited above. My bad
0

I believe that if you want to initialize object with default value you should do this in the place of declaration

Object A = new Object();
if (conditionA) {
    ...
}

Comments

0

Approach A is better.

In that, Consider "if" condition is not satisfied and it goes in "else".

There if conditionB is satisfied, then it will return.So memory is not allocated to A.

In Approach B, unnecessarily memory allocation is done which is of no use in above scenario.

Comments

0

I would go with approach A, as it is only necessary to initialize the object when needed.

If the initialization of that object is rather complicated, in order to avoid repeating yourself, you either define a static helper method in order to initialize the object or you go via a Supplier<>:

Supplier<Type> objSupplier = () -> new Type(a, b, c, d, whatever)

and then, at the two places where the object is supposed to be created, you do

A = objSupplier.get();

(note that variables are usually written in lowercase/camelCase).

1 Comment

I think this is way out of scope of the question, OP specifically defines his problems and possible solutions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.