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:
- Memory consumption perspective
- Performance perspective
- 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.