Best practice for variable saving

saurabh agarwal

There are two code code 1:

if(isApplicable() || isGood()) {
   //something
}

private boolean isApplicable() {
}

private boolean isGood() {
}

Code 2:

boolean applicable = isApplicable();
boolean good = isGood();

if(applicable || good) {
   //something
}

private boolean isApplicable() {
}

private boolean isGood() {
}

Which of the the approach is good java practice ? To me code1 seams more clean and code 2 seams to have extra code. code2 can make remote debugging easy.

slim

To generalise your question, you're asking about the two forms:

// local variable form
Foo foo = methodReturningFoo();
Bar bar = methodTakingFoo(foo);

// inlined form
Bar bar = methodTakingFoo(methodReturningFoo());

Most modern IDEs have a shortcut to refactor between these at a keystroke: "inline" and "extract local variable". The fact that both refactorings exist is an indicator that both are appropriate, in different circumstances.

Inlining to a single statement makes the code more compact and sometimes more readable. You can see everything that's happening without having to read up to find out where a variable was set.

Here's a good candidate for inlining:

String name = customer.getName();
String greeting = createGreeting(name);

// ... becomes ...
String greeting = createGreeting(customer.getName());

Extracting a local variable turns what may be a long statement into two (or more) shorter statements. It may also allow you to re-use a value rather than calculate it twice.

Here's an example where we just break a statement into smaller chunks.

String greeting = createGreeting(greetingFactory.get(customer.getTitle()), customer.getName());

// ... becomes ...
Title title = customer.getTitle();
String name = customer.getName();
String greeting = createGreeting(greetingFactory.get(title), name));

... here's an example where we reuse a calculated value.

// doing the work twice
CustomerCategory category = findCategory(totalOrderValues(
     customer.getOrders(currentMonth)));
List<Promotion> eligiblePromotions = findEligiblePromotions(totalOrderValues(
     customer.getOrders(currentMonth)));

// ... becomes ...
BigInteger totalOrderValues = totalOrderValues(
    customer.getOrders(currentMonth))
CustomerCategory category = findCategory(totalOrderValues);
List<Promotion> eligiblePromotions = findEligiblePromotions(totalOrderValues);

Generally, prefer the inlined version, until you see that the line is too long and complicated. Then extract a local variable (or extract a method) to make it neater. If it makes sense to store a value to avoid repeating an expensive calculation, then do so.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Saving XML Parsed data to NSMutableDictionary Best Practice

From Dev

What is the best practice for saving iPhone video & pictures?

From Dev

Best practice for saving long text in Android

From Dev

What is the best practice for saving and retrieving a java object?

From Dev

Best Practice for saving list of connection settings

From Dev

Which is the best practice for saving a price column on PostgreSQL?

From Dev

Best practice for declaring an empty variable

From Dev

Best practice of checking if variable is undefined

From Dev

Variable Storage Location - Best Practice?

From Dev

Python Variable Mutation Best Practice

From Dev

best practice for global variable in python

From Dev

What's the best practice for saving initial data in JHipster?

From Dev

Best practice to maintain a persistent variable in a Flask app

From Dev

NodeJS with MongoDB - best practice to instantiate "db" variable

From Java

Which of these is the best practice for accessing a variable in a class?

From Dev

Node JS, Static Function / Variable Best Practice

From Dev

Best practice for shared variable in javascript object

From Dev

What's the best practice to declare a variable in Dart?

From Dev

Lua best practice close variable with popen()

From Dev

Laravel blade best practice to assign variable

From Dev

What is the best practice of propagating variable value in Yii

From Dev

Updating variable value from dictionary and best practice

From Dev

declaring for-loop variable best practice

From Dev

Is this best practice? Checking if variable defined or assign null

From Dev

awk : best practice for loop variable naming

From Dev

Laravel Best Practice for Unique Fields Based on Variable

From Dev

Best practice to call a variable from ViewModel

From Dev

Initialize final variable in Flutter widget - best practice

From Mysql

What is the best practice for saving user details and resources when user deletes account?

Related Related

  1. 1

    Saving XML Parsed data to NSMutableDictionary Best Practice

  2. 2

    What is the best practice for saving iPhone video & pictures?

  3. 3

    Best practice for saving long text in Android

  4. 4

    What is the best practice for saving and retrieving a java object?

  5. 5

    Best Practice for saving list of connection settings

  6. 6

    Which is the best practice for saving a price column on PostgreSQL?

  7. 7

    Best practice for declaring an empty variable

  8. 8

    Best practice of checking if variable is undefined

  9. 9

    Variable Storage Location - Best Practice?

  10. 10

    Python Variable Mutation Best Practice

  11. 11

    best practice for global variable in python

  12. 12

    What's the best practice for saving initial data in JHipster?

  13. 13

    Best practice to maintain a persistent variable in a Flask app

  14. 14

    NodeJS with MongoDB - best practice to instantiate "db" variable

  15. 15

    Which of these is the best practice for accessing a variable in a class?

  16. 16

    Node JS, Static Function / Variable Best Practice

  17. 17

    Best practice for shared variable in javascript object

  18. 18

    What's the best practice to declare a variable in Dart?

  19. 19

    Lua best practice close variable with popen()

  20. 20

    Laravel blade best practice to assign variable

  21. 21

    What is the best practice of propagating variable value in Yii

  22. 22

    Updating variable value from dictionary and best practice

  23. 23

    declaring for-loop variable best practice

  24. 24

    Is this best practice? Checking if variable defined or assign null

  25. 25

    awk : best practice for loop variable naming

  26. 26

    Laravel Best Practice for Unique Fields Based on Variable

  27. 27

    Best practice to call a variable from ViewModel

  28. 28

    Initialize final variable in Flutter widget - best practice

  29. 29

    What is the best practice for saving user details and resources when user deletes account?

HotTag

Archive