Is "temp" ever a good variable name?
Sometimes I've been trying to come up with a good variable name for minutes, when I realize that it isn't worth the effort for t开发者_JS百科his tiny loop. Is there any situation where it would be justified to call a variable "temp"?
Update: I should have been more clear since you are all programmers! I'm not looking for cases where "temp" actually describes the function of the variable, but cases where it means absolutely nothing at all.
Sure, if you do swapping by value;
int temp = a;
a = b;
b = temp;
Or if you use variable notation such as calc for calculation etc you should use temp for temperature ;)
Yes; if the variable has no real semantic meaning, then I would say that in the situation you describe, it's perfectly acceptable.
Sure. When modeling weather:
pressure = 3
temp = 21
pressure_tommorow, temp_tommorow = model(pressure, temp, 1.day.from.now)
However if I had the misfortune of programming in a language that would not support this and it was totally one off (e.g. temporary) then why not:
WeatherModel temp = MyNotSoAbstractModellingClass.giveMeTheDamnModel(TimeManager.getTommorow());
PressureEstimate tommorowsEstimatedPressure = temp.getPressure();
TemperatureWhyMakeThisPointleslyShortEstimate tommorowsEstimatedTemperature = temp.getTemperature();
// like someone can pretend that it's not painfully evident what's going on
Whenever you need an intermediate variable, intended to temporarily hold data during manipulation of something else, IMO the best name is temp
- it clearly describes the variables function.
Samples:
char Temp[32]; sprintf(Temp, ...); RealVar += Temp;
int temp = a; a = b; b = temp;
Yes I think there are a couple:
var temp = "35°C";
var temp = Path.GetTempPath();
If that's the most descriptive name you can think of. Sometimes you can extend it to "temp<something>", in which case the "<something>" is also a candidate for a name.
You can use only first letter of it's meaning for a short function body, in this case "m" would be well understood considering function name which gets/sets minutes has a self describing name eg
var m = currentDate.Minute;
As someone else described, name "temp" is good for swapping variable values or probably as a name for temporary file path, nothing more.
No, it isn't.
IMHO:
tempPerson
tempLoopVariable
tempWhatever
or even
temporaryWhatever
are better.
精彩评论