首页 文章详情

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

面向对象思考 | 320 2020-11-23 18:15 0 0 0
UniSMS (合一短信)

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(示例)

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


新书介绍

《实战Python设计模式》是作者最近出版的新书,拜托多多关注!

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。




觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!



good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter