C++核心准则​NL.7:使名称的长度与作用域的大小大致成比例

2020-11-26 11:15:43 浏览数 (1)

NL.7: Make the length of a name roughly proportional to the length of its scope

NL.7:使名称的长度与作用域的大小大致成比例

Rationale: The larger the scope the greater the chance of confusion and of an unintended name clash.

原理:范围越大,混淆和意外的名称冲突的机会就越大。

Example(示例)

代码语言:javascript复制
double sqrt(double x);   // return the square root of x; x must be non-negative

int length(const char* p);  // return the number of characters in a zero-terminated C-style string

int length_of_string(const char zero_terminated_array_of_char[])    // bad: verbose

int g;      // bad: global variable with a cryptic name

int open;   // bad: global variable with a short, popular name

The use of p for pointer and x for a floating-point variable is conventional and non-confusing in a restricted scope.

将p用于指针,将x用于浮点变量是常规的,并且在限定范围内不会混淆。

Enforcement(实施建议)

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#nl7-make-the-length-of-a-name-roughly-proportional-to-the-length-of-its-scope

0 人点赞