Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8657153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:27:08+00:00 2026-06-12T15:27:08+00:00

The scenario is that I need to access a value at least twice. I.e.

  • 0

The scenario is that I need to access a value at least twice. I.e. I am using logger to keep track of what is happening in the application. I want to log the name of the object, that function is working on, and later on do something with the very same name(i.e check if it contains some string or put it into an array).

Storing the name in variable:

foo(Bar bar){
    String name = bar.getName();
    logger.info("I am working with "+name);
    this.doSomethingWith(name);
}

Or calling getName() twice:

foo(Bar bar){
    logger.info("I am working with "+bar.getName());
    this.doSomethingWith(bar.getName());
}

I understand, that in the first scenario, I will create a new String, assign a value to it and then retrieve this value twice. This way I am using more memory resources, correct?

In second scenario, am I accessing object bar twice and then accessing it’s name twice. I suppose this is not a DRY approach. But on the other hand I am not repeating myself in the memory, correct?

Which approach is better?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:27:09+00:00Added an answer on June 12, 2026 at 3:27 pm

    Personally, I prefer the second approach.
    Indeed, I usually create temporary variable solely when necessary.

    Martin Fowler ( http://en.wikipedia.org/wiki/Martin_Fowler ) also follows this guideline. He relates it within his book that I’ve read:

    http://www.amazon.fr/Refactoring-Improving-Design-Existing-Code/dp/0201485672

    A free extract of its book concerning this topic is here:

    http://sourcemaking.com/refactoring/replace-temp-with-query

    Some people would argue removing temporary variables could lead to performance issue.

    As Martin Fowler says:

    You may be concerned about performance in this case. As with other
    performance issues, let it slide for the moment. Nine times out of
    ten, it won’t matter. When it does matter, you will fix the problem
    during optimization. With your code better factored, you will often
    find more powerful optimizations, which you would have missed without
    refactoring. If worse comes to worse, it’s very easy to put the temp
    back.

    But anyway, it’s a matter of taste. Some people find more readable the first approach, others the second. I really prefer the second because I hate temporary variables adding lines for no real values 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scenario that when I try to access a hash key using
I am scratching my head about this. My scenario are that I need to
i need to implement a messaging scenario that consumes messages from throusands of destinations
I have a scenario that, I am creating dynamic html content and I need
I have a scenario that looks like this: #include <algorithm> using namespace std; //
I came in to scenario that I want to trigger service on specific time.
Here is the scenario: There are 5 websites (different domain names) that need to
I'm considering using GIT or our family photos. The scenario is that both me
Lets assume a scenario that i have a remote, a developer1 and a developer2.
I have a scenario that my load balancer translates port 80 from outside into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.