2

我想制作一个变量,即客户想要的调味品。

我认为“condimentCustomerWants”还可以

但我永远不会在其他代码中看到包含相对代词的变量名。

所以我问了我的朋友,他推荐了'customerWantsCondiment',这是句子。

嗯..哪个名字是正确的,好的,可读的?

4

4 回答 4

6

我会desiredCondiments加入其中。

于 2012-08-04T20:13:15.427 回答
3

真的取决于每个人的编码风格。我会做

requestedCondiment
于 2012-08-04T20:14:28.910 回答
3

desiredCondiment PreferredCondiment condimentForCustomer PreferredCondimentForCustomer WantCondiment

等等...

于 2012-08-04T20:15:25.013 回答
1

如何命名变量完全取决于您,但是它们应该始终反映变量实际应该做什么。

如果是:“客户是否想要调味品”,您需要: CustomerWantsCondiment(真/假值,可能是布尔值)

如果是:“客户想要哪种调味品?”,您需要:CondimentCustomerWants(例如一个 int 值)

它们听起来相似,但都有不同的含义。什么最适合你,真的。

您可能还希望遵守变量名称约定,以表示变量类型的字母开头变量名称。这样,您将一眼就知道变量的类型,而无需查找实际定义。请注意,介绍字母总是小写。

例如:bool bCustomerWantsCondiment;诠释 iCustomerWantsCondiment;char *sCustomerWantsCondiment; 等等

有关匈牙利符号的更多信息,请查看此处,例如: http ://en.wikipedia.org/wiki/Hungarian_notation

此外,为了便于阅读,您应该使用“CamelCase”约定。这意味着,每次您在变量名中开始一个新单词时,都以大写字母开头。

于 2012-08-04T20:14:43.283 回答