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 8012847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:27:37+00:00 2026-06-04T19:27:37+00:00

Often in code I get to scenarios I need a temporary variable – example:

  • 0

Often in code I get to scenarios I need a temporary variable – example:

String tempUserName = input.getUserName();
tempUserName = sanitzie(tempUserName);
validate(tempUserName);
// ... Do something else with tempUserName

String tempUserNickName = input.getUserNickName();
tempUserNickName = sanitzie(tempUserNickName);
validate(tempUserNickName);
// ... Do something else with tempUserNickName

...
...

I know that some people use a single temp variable for these kind of things:

String temp = input.getUserName();
temp = sanitzie(temp);
validate(temp);
// ... Do something else with temp

temp = input.getUserNickName();
temp = sanitzie(temp);
validate(temp);
// ... Do something else with temp

...
...

In my opinion – the way I write it is clearer and less error-prone.

My question is – are there any benefits for using the single temp variable from:

  1. Memory consumption perspective
  2. Performance perspective
  3. Any other perspective

Lets limit the scope of the question to String variables only – but I’ll be happy to get a more general feedback.

  • 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-04T19:27:38+00:00Added an answer on June 4, 2026 at 7:27 pm

    My main answer to this would be if you have a series of those sorts of statements, I’d break the groupings out into a separate function you call. (Which would have just the one temp variable.) E.g.:

    private String handleInput(String val) {
        val = sanitzie(val);
        validate(val);
        // ... Do something else with val
    
        return val;
    }
    

    Usage:

    String finalPlaceForUserName     = handleInput(input.getUserName());
    String finalPlaceForUserNickName = handleInput(input.getUserNickName());
    

    …where handleInput might return null for invalid or dangerous input, etc. You may well have to parameterize handleInput more than above.

    Answer the perspectives you raised:

    Memory consumption perspective

    I suppose technically, if there were only one temp variable, the memory from earlier on would be eligible for garbage collection earlier (e.g., while the method was running rather than afterward). But I doubt it makes any real-world difference.

    Performance perspective

    I can’t imagine so.

    Any other perspective

    We probably come down to style here, which is mostly a matter of personal preference. Again, I’d split it off into a function, which is kind of an end-run around the question. 🙂

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

Sidebar

Related Questions

I get this error in xml file very often. here is the code in
I'm often having code written as follows try: self.title = item.title().content.string except AttributeError, e:
Often I see javascript code where event handlers (like onmousemove) are assigned dynamically. Example:
Often, I come across code where the Getter method is repeatedly used/abused to get
I often write code like that: MyObject property; MyObject Property { get { return
I often see the code which uses mock in Rspec, like this: describe GET
I find myself often writing such code: Map < String, Set < String >
Django code samples involving post data often shows code similar to this: if request.method
I often see code like int hashCode(){ return a^b; } Why XOR?
I often do code review in the following way: Open the SVN log Select

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.