前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则​NL.5:避免在名称中包含类型信息

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

作者头像
面向对象思考
发布2020-11-25 17:05:27
7070
发布2020-11-25 17:05:27
举报

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
复制
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
复制
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
复制
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
复制
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
复制
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
复制
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 删除。

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • NL.5: Avoid encoding type information in names
  • Rationale(基本原理)
    • Example, good(范例)
      • Note(注意)
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档