8

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 this 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.

Tim
  • 13,904
  • 10
  • 69
  • 101
  • 4
    Nah, if you're going to use a name as meaningless as `temp`, you might as well use `tmp` ;-) – Steve Jessop Mar 12 '11 at 21:59
  • 1
    If the variable is not actually something temporary, or it isn't a temperature, than no. In my opinion, of course. – Mike Lentini Apr 01 '11 at 10:21
  • Possible duplicate of [Why using a variable name "temp" considered a bad practice?](http://stackoverflow.com/questions/24850123/why-using-a-variable-name-temp-considered-a-bad-practice) –  Nov 07 '15 at 16:04

8 Answers8

10

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 ;)

stefan
  • 2,886
  • 21
  • 27
  • +1 This is the only counter-case I could think of. I was about to post an answer arguing for `swap` or `prev` instead of `temp` but ... I use `temp` in this case (and that's all that I can recall) so I'd just be playing the advocate :p –  Mar 12 '11 at 20:10
  • 4
    Indeed. But if you do this in a language that supports parallel assignment (`a, b = b, a`), you should be stabbed in the face (figuratively speaking). –  Mar 12 '11 at 20:11
  • @Jakub Hampl Not in a number of "modern" languages, unfortunately. –  Mar 12 '11 at 20:11
  • It's certainly a good example, since "temp" describes the function of the variable. I was looking for a case where "temp" doesn't actually mean anything. – Tim Mar 12 '11 at 20:24
  • There's always `a^=b; b^=a; a^=b;` and then you don't need `temp`. – David Heffernan Mar 13 '11 at 13:59
3

Yes; if the variable has no real semantic meaning, then I would say that in the situation you describe, it's perfectly acceptable.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Coming up with an example is difficult; do you have one? – Tim Mar 12 '11 at 20:24
  • 2
    @Tim: Any time I'm implementing, say, a complex equation, I'll typically break it into multiple stages on multiple lines. Those intermediate results don't necessarily have any meaning on their own. – Oliver Charlesworth Mar 12 '11 at 22:24
  • 1
    I think thats a bad example. You should always try to find out a sensible meaning of the splitted equation and i've never seen a case where its not possible. However if you as programmer have no idea what the algorithm does then I totally agree with you! – stefan Mar 12 '11 at 22:50
3

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
Jakub Hampl
  • 39,863
  • 10
  • 77
  • 106
  • 3
    You should use temeperature here, to avoid confusion. – user unknown Mar 12 '11 at 22:21
  • 1
    What confusion are you referring to particularly? I presume you would be less confused why your code doesn't compile because you have `temperature` in one place and `temeperature` in another? – Jakub Hampl Mar 12 '11 at 22:52
1

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;
Erik
  • 88,732
  • 13
  • 198
  • 189
  • 1
    I prefer `tmpFoo` if the temporary variable holding a `Foo`. I like eliminating the need for pointless temporary variables even more. –  Mar 12 '11 at 20:13
1

Yes I think there are a couple:

var temp = "35°C";
var temp = Path.GetTempPath();
OJ.
  • 28,944
  • 5
  • 56
  • 71
0

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.

outis
  • 75,655
  • 22
  • 151
  • 221
0

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.

too
  • 3,009
  • 4
  • 37
  • 51
-1

No, it isn't.

IMHO:

tempPerson

tempLoopVariable

tempWhatever

or even

temporaryWhatever

are better.

Michał Kuliński
  • 1,928
  • 3
  • 27
  • 51