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.

RonK
  • 9,472
  • 8
  • 51
  • 87
  • 1
    Probably doesn't matter at all in the larger scheme of things. I doubt that you or anyone else could produce data to demonstrate either way. Use the one that reads best and most clearly. Unfortunately for you, that might be a subjective decision. Not everyone will go along with your way. – duffymo May 29 '12 at 10:03

5 Answers5

2

Memory consumption and performance are irrelevant, but every non-final var makes the code much harder to read. You can never see at a glance exactly where it is being reassigned and if there's a bug that you're looking for, this gives you a hard time. My suggestion, and what I routinely do in my code, is to extract to a method so you don't even need the local var at the calling side. Always tend to write code in terms of function composition.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
2

I also prefer not reusing local variables, and performance aspects shouldn't matter here, because you would gain or lose such a tiny time that it's negligible. And the JIT could generate the same native code for both anyway.

What matters is readability and maintainability. And what I see in your code is that there should be an additional method, so that your original code becomes:

sanitizeAndValidate(input.getUserName());
sanitizeAndValidate(input.getUserNickName());

This makes the code clearer, more testable, avoids repetitions, and makes your initial question irrelevant, since your temp variable is only defined once, in the sanitizeAndValidate() method.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks - the code was just a sample - in real life the logic is not identical - so extracting a method might not be worth while - but all the answers provided here basically say the same thing - maintainability in this scenario is the major factor - the rest seems negligible. I wonder if these answers would change if I ask the same for `C++`. – RonK May 29 '12 at 10:11
1

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. :-)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

The performance difference (memory wise) is to small to be taken in consideration.

What's more important is to make your code readable, and optimize things where it matters.

Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118
0

Those repetitions occur though they are not nice style. I often do:

{
    String s = ...
    something with s;
}
{
    String s = ...
    something with s;
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138