首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >C++核心准则​NL.5:避免在名称中包含类型信息

C++核心准则​NL.5:避免在名称中包含类型信息

作者头像
面向对象思考
发布于 2020-11-25 09:05:27
发布于 2020-11-25 09:05:27
92900
代码可运行
举报
运行总次数:0
代码可运行

NL.5: Avoid encoding type information in names

NL.5:避免在名称中包含类型信息

Rationale(基本原理)

If names reflect types rather than functionality, it becomes hard to change the types used to provide that functionality. Also, if the type of a variable is changed, code using it will have to be modified. Minimize unintentional conversions.

如果名称反映类型而不是功能,则很难更改用于提供该功能的类型。同样,如果更改了变量的类型,则必须修改使用该变量的代码。最小化意外转换。

Example, bad(反面示例)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
void print_int(int i);
void print_string(const char*);

print_int(1);          // repetitive, manual type matching
print_string("xyzzy"); // repetitive, manual type matching
Example, good(范例)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
void print(int i);
void print(string_view);    // also works on any string-like sequence

print(1);              // clear, automatic type matching
print("xyzzy");        // clear, automatic type matching
Note(注意)

Names with types encoded are either verbose or cryptic.

包含类型的名称是冗长的或隐秘的。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
printS  // print a std::string
prints  // print a C-style string
printi  // print an int

Requiring techniques like Hungarian notation to encode a type has been used in untyped languages, but is generally unnecessary and actively harmful in a strongly statically-typed language like C++, because the annotations get out of date (the warts are just like comments and rot just like them) and they interfere with good use of the language (use the same name and overload resolution instead).

在非类型化语言中已经使用了像匈牙利命名方法这样的技术在变量名中包含类型,但是在像C ++这样的强静态类型化语言中,这通常是不必要的甚至是有害的,因为注释已经过时了(注释就像疣一样,也会像它们一样腐烂),并且会干扰语言的良好使用(改用相同的名称并使用重载方式)。

Note(注意)

Some styles use very general (not type-specific) prefixes to denote the general use of a variable.

一些样式使用非常通用的(不是特定于类型的)前缀来表示变量的通用用法。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
auto p = new User();
auto p = make_unique<User>();
// note: "p" is not being used to say "raw pointer to type User,"
//       just generally to say "this is an indirection"

auto cntHits = calc_total_of_hits(/*...*/);
// note: "cnt" is not being used to encode a type,
//       just generally to say "this is a count of something"

This is not harmful and does not fall under this guideline because it does not encode type information.

这是无害的,并且不受该准则约束,因为它表达的不是类型信息。

Note(注意)

Some styles distinguish members from local variable, and/or from global variable.

一些风格将成员与局部变量和/或全局变量区分开。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
struct S {
    int m_;
    S(int m) : m_{abs(m)} { }
};

This is not harmful and does not fall under this guideline because it does not encode type information.

这是无害的,不受该准则约束,因为它没有表达类型信息。

Note(注意)

Like C++, some styles distinguish types from non-types. For example, by capitalizing type names, but not the names of functions and variables.

像C ++一样,某些风格将类型与非类型区分开。例如,通过大写类型名称,而不是函数和变量的名称。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
typename<typename T>
class HashTable {   // maps string to T
    // ...
};

HashTable<int> index;

This is not harmful and does not fall under this guideline because it does not encode type information.

这是无害的,不受该准则约束,因为它没有表达类型信息。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#nl5-avoid-encoding-type-information-in-names

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

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

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

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
C++核心准则C.162:将大致相同的操作设计为重载函数​
Having different names for logically equivalent operations on different argument types is confusing, leads to encoding type information in function names, and inhibits generic programming.
面向对象思考
2020/03/25
3130
C++核心准则​NL.10:首选下划线风格名称
The use of underscores to separate parts of a name is the original C and C++ style and used in the C++ Standard Library.
面向对象思考
2020/11/26
4780
C++核心准则​NL.17:使用K&R风格派生的布局
This is the original C and C++ layout. It preserves vertical space well. It distinguishes different language constructs (such as functions and classes) well.
面向对象思考
2020/12/15
7320
C++核心准则​NL.8:使用一致的命名方式
Rationale: Consistence in naming and naming style increases readability.
面向对象思考
2020/11/26
3670
C++核心准则​NL.9:全字母大写仅用于宏名称
To avoid confusing macros with names that obey scope and type rules.
面向对象思考
2020/11/26
5110
C++核心准则​NL.19:避免容易被误读的名称
Readability. Not everyone has screens and printers that make it easy to distinguish all characters. We easily confuse similarly spelled and slightly misspelled words.
面向对象思考
2020/12/15
5080
C++核心准则​NL.7:使名称的长度与作用域的大小大致成比例
Rationale: The larger the scope the greater the chance of confusion and of an unintended name clash.
面向对象思考
2020/11/26
4410
C.43:保证(值类型)可拷贝类有默认构造函数
Many language and library facilities rely on default constructors to initialize their elements, e.g. T a[10] and std::vector<T> v(10). A default constructor often simplifies the task of defining a suitable moved-from state for a type that is also copyable.
面向对象思考
2020/03/25
5400
C++核心准则​NL.18:使用C ++风格的声明符布局
The C-style layout emphasizes use in expressions and grammar, whereas the C++-style emphasizes types. The use in expressions argument doesn't hold for references.
面向对象思考
2020/12/15
4770
C++核心准则T.47:避免使用通用名称的高度不受限模板
An unconstrained template argument is a perfect match for anything so such a template can be preferred over more specific types that require minor conversions. This is particularly annoying/dangerous when ADL is used. Common names make this problem more likely.
面向对象思考
2020/09/10
5040
C++核心准则​NL.25:不要将void用作参数类型
It's verbose and only needed where C compatibility matters.
面向对象思考
2020/12/15
4240
C++核心准则​NL.26:使用传统的常量记法
Conventional notation is more familiar to more programmers. Consistency in large code bases.
面向对象思考
2020/12/15
6050
C++核心准则​Pro.safety:类型安全准则群组
This profile makes it easier to construct code that uses types correctly and avoids inadvertent type punning. It does so by focusing on removing the primary sources of type violations, including unsafe uses of casts and unions.
面向对象思考
2020/11/10
6320
C++核心准则​Pro.safety:类型安全准则群组
C++核心准则ES.12:不要在嵌套作用域中重复使用同样的名称
It is easy to get confused about which variable is used. Can cause maintenance problems.
面向对象思考
2020/04/24
1.1K0
C++核心准则ES.12:不要在嵌套作用域中重复使用同样的名称
C++核心准则ES.20: 保证所有对象被初始化
Avoid used-before-set errors and their associated undefined behavior. Avoid problems with comprehension of complex initialization. Simplify refactoring.
面向对象思考
2020/04/26
5290
C++核心准则R.2: 只在接口中表示单独对象使用原始指针​
R.2: In interfaces, use raw pointers to denote individual objects (only)
面向对象思考
2020/03/25
4870
C++核心准则ES.49:如果必须进行类型转换,使用命名转换
Readability. Error avoidance. Named casts are more specific than a C-style or functional cast, allowing the compiler to catch some errors.
面向对象思考
2020/05/25
7950
C++核心准则ES.49:如果必须进行类型转换,使用命名转换
C++核心准则​SL.str.3:使用zstring或czstring引用C风格0结尾的字符串序列
Readability. Statement of intent. A plain char* can be a pointer to a single character, a pointer to an array of characters, a pointer to a C-style (zero-terminated) string, or even to a small integer. Distinguishing these alternatives prevents misunderstandings and bugs.
面向对象思考
2020/10/30
8270
C++核心准则​SL.str.3:使用zstring或czstring引用C风格0结尾的字符串序列
C++核心准则​SF.10:避免依赖隐式包含的名称
Avoid surprises. Avoid having to change #includes if an #included header changes. Avoid accidentally becoming dependent on implementation details and logically separate entities included in a header.
面向对象思考
2020/10/30
6650
C++核心准则​SF.10:避免依赖隐式包含的名称
C++核心准则ES.48:避免使用类型转换
Casts are a well-known source of errors. Make some optimizations unreliable.
面向对象思考
2020/05/25
6970
C++核心准则ES.48:避免使用类型转换
推荐阅读
相关推荐
C++核心准则C.162:将大致相同的操作设计为重载函数​
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档