C++核心准则ES.8:避免看起来差不多的名称
ES.8: Avoid similar-looking names
ES.8:避免看起来差不多的名称
Reason(原因)
Code clarity and readability. Too-similar names slow down combiiaoprehension and increase the likelihood of error.
代码整洁性和可读性。过于相似的名称会减缓理解进程并增加出错的可能性。
Example, bad(反面示例)
if (readable(i1 + l1 + ol + o1 + o0 + ol + o1 + I0 + l0)) surprise();
Example, bad(反面示例)
Do not declare a non-type with the same name as a type in the same scope. This removes the need to disambiguate with a keyword such as struct or enum. It also removes a source of errors, as struct X can implicitly declare X if lookup fails.
不要用一个名称定义类型之后,在同一个作用域中又使用这个名称定义非类型。这种做法使消除名称和像struct或enum那样的关键词之间的歧义不再必要。同时也减少了一个错误的源头,例如如果名称检索失败,struct X可以隐性声明X类型。
struct foo { int n; };
struct foo foo(); // BAD, foo is a type already in scope
struct foo x = foo(); // requires disambiguation
Exception(例外)
Antique header files might declare non-types and types with the same name in the same scope.
特别早期的头文件可能会使用同一个名称声明类型和非类型。
Enforcement(实施建议)
Check names against a list of known confusing letter and digit combinations.
使用一个已知的容易混淆的字母和数字的列表检查名称。
Flag a declaration of a variable, function, or enumerator that hides a class or enumeration declared in the same scope.
标记同一作用域中可能隐藏类或枚举类型声明的变量、函数、枚举类型的声明。
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es8-avoid-similar-looking-names
觉得本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!