我正在替换我的#defines,例如#define NUM_SLIDER_POSITIONS 5用于常量变量。 我应该保持旧的命名,如:
const unsigned int NUM_SLIDER_POSITIONS = 5;或者我应该使用更像的东西:
const unsigned int kNumSliderPositions = 5;。
编辑 :帖子已被搁置,但无论如何我想总结你的答案:
其他选项是使用下划线作为使用小写字母的分隔符:
const unsigned int num_slider_positions = 5;常量标识符。
关于使用前缀作为识别常量的方法,最常见的选项是不使用它,因为它可能不会添加相关信息:
const unsigned int num_slider_positions = 5;在名称前使用“k”:
const unsigned int k_num_slider_positions = 5;或者在类或命名空间中声明变量,以避免污染全局范围并提供更加不言自明的名称:
namespace defaults // or "config", or "settings" or something like that { const unsigned int num_slider_positions = 5; }客户代码:
int slider_positions = defaults::num_slider_positions;I am replacing my #defines, for instance #define NUM_SLIDER_POSITIONS 5 for constant variables. Should I keep the old naming like:
const unsigned int NUM_SLIDER_POSITIONS = 5;Or should I use something more like:
const unsigned int kNumSliderPositions = 5;.
EDIT: The post has been put on hold, but anyway I'd like to sum up your answers:
Other option would be using underscores as a separators using lower case letters:
const unsigned int num_slider_positions = 5;Constant identifier.
Regarding the use of a prefix as a way of identifying constants , the most common options are not using it, as it may not add relevant information:
const unsigned int num_slider_positions = 5;Use a "k" before the name:
const unsigned int k_num_slider_positions = 5;Or declaring the variable inside a class or namespace, in order to avoid polluting the global scope and providing a more self-explanatory name:
namespace defaults // or "config", or "settings" or something like that { const unsigned int num_slider_positions = 5; }Client code:
int slider_positions = defaults::num_slider_positions;最满意答案
我正在替换我的#defines用于常量变量。
奖励! :)
我应该保留旧的命名,如:[all-caps]
如果项目的编码约定将常量指定为全部大写,那么您应该(因为它可以省力)。 否则,你不应该 (因为以后会因为维护而混淆)。
或者我应该使用更像的东西:[bastardized hungarian convention]
这取决于你。 我个人不喜欢为我的常量添加奇怪的字母,因为在阅读代码时 - 或者编写它 - 我并不关心它们是不变的(如果我尝试写入它们,编译器会让我知道)。
我的(个人)选择是使用命名空间来提供上下文(而不是前缀),沿着这些方向:
namespace defaults // or "config", or "settings" or something like that { const unsigned int num_slider_positions = 5; }客户代码:
int slider_positions = defaults::num_slider_positions;我发现这是一个更好的选择,因为上下文更加不言自明(比它前面的“k”,或“g”或其他任何东西)。
I am replacing my #defines for constant variables.
Kudos! :)
Should I keep the old naming like: [all-caps]
If the coding conventions of your project designate constants to be in all-caps, you should (as it spares you an effort). Otherwise, you should not (because it will be confusing later, for maintenance).
Or should I use something more like: [bastardized hungarian convention]
This is up to you. Personally I do not like to add weird letters for my constants, because when reading the code - or writing it - I do not care much that they are constant (and if I try to write into them, the compiler will let me know).
My (personal) choice would be to use a namespace for providing context (instead of a prefix), along these lines:
namespace defaults // or "config", or "settings" or something like that { const unsigned int num_slider_positions = 5; }Client code:
int slider_positions = defaults::num_slider_positions;I find this to be a superior alternative, because the context is more self-explanatory (than a "k" in front of it, or a "g" or a whatever else).
更多推荐
发布评论