是“温度”吗?有一个好的变量名吗?

发布于 2024-10-21 18:54:46 字数 163 浏览 1 评论 0原文

有时,当我意识到为这个小循环付出努力并不值得时,我花了几分钟时间试图想出一个好的变量名称。是否有任何情况可以将变量称为“temp”?

更新:我应该更清楚,因为你们都是程序员!我不是在寻找“temp”实际上描述变量功能的情况,而是在寻找它完全没有任何意义的情况。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

寄与心 2024-10-28 18:54:46

当然,如果你按价值交换的话;

int temp = a;
a = b;
b = temp;

或者,如果您使用变量符号(例如 calc 进行计算等),则应该使用 temp 表示温度;)

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

半窗疏影 2024-10-28 18:54:46

是的;如果该变量没有真正的语义含义,那么我想说,在您描述的情况下,这是完全可以接受的。

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

青芜 2024-10-28 18:54:46

当然。当建模天气时:

pressure = 3
temp = 21
pressure_tommorow, temp_tommorow = model(pressure, temp, 1.day.from.now)

但是,如果我不幸使用一种不支持此功能的语言进行编程,并且它完全是一次性的(例如暂时的),那么为什么不呢:

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

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
謌踐踏愛綪 2024-10-28 18:54:46

每当您需要一个中间变量(旨在在操作其他内容期间临时保存数据)时,IMO 最好的名称是temp - 它清楚地描述了变量的功能。

样品:

char Temp[32]; sprintf(Temp, ...); RealVar += Temp;

int temp = a; a = b; b = temp;

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;
谈情不如逗狗 2024-10-28 18:54:46

是的,我认为有几个:

var temp = "35°C";
var temp = Path.GetTempPath();

Yes I think there are a couple:

var temp = "35°C";
var temp = Path.GetTempPath();
风透绣罗衣 2024-10-28 18:54:46

如果这是您能想到的最具描述性的名称。有时您可以将其扩展为“temp<something>”,在这种情况下,“<something>”也是一个名字的候选者。

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.

一梦浮鱼 2024-10-28 18:54:46

对于短函数体,您只能使用其含义的第一个字母,在这种情况下,考虑到获取/设置分钟的函数名称具有自我描述的名称,“m”会很好理解,例如,

var m = currentDate.Minute;

正如其他人所描述的,名称“temp”很好用于交换变量值或可能作为临时文件路径的名称,仅此而已。

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.

西瓜 2024-10-28 18:54:46

不,不是。

恕我直言:

tempPerson

tempLoopVariable

tempWhatever

或者甚至

temporaryWhatever

更好。

No, it isn't.

IMHO:

tempPerson

tempLoopVariable

tempWhatever

or even

temporaryWhatever

are better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文