前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则​NL.7:使名称的长度与作用域的大小大致成比例

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

作者头像
面向对象思考
发布2020-11-26 11:15:43
3790
发布2020-11-26 11:15:43
举报
文章被收录于专栏:C++核心准则原文翻译

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-11-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • NL.7: Make the length of a name roughly proportional to the length of its scope
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档