意外的类型int_C++核心准则?NL.5:避免在名称中包含类型信息

b8ef4916ee5372bc646812c53efbb2cb.png

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

void print_int(int i);void print_string(const char*);print_int(1);          // repetitive, manual type matchingprint_string("xyzzy"); // repetitive, manual type matching

Example, good(范例)

void print(int i);void print(string_view);    // also works on any string-like sequenceprint(1);              // clear, automatic type matchingprint("xyzzy");        // clear, automatic type matching

Note(注意)

Names with types encoded are either verbose or cryptic.

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

printS  // print a std::stringprints  // print a C-style stringprinti  // 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.

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

auto p = new User();auto p = make_unique();// 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.

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

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 ++一样,某些风格将类型与非类型区分开。例如,通过大写类型名称,而不是函数和变量的名称。

typenameclass HashTable {   // maps string to T    // ...};HashTable 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

新书介绍

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

01a20608e4864f21086e0ac22b1866e9.png

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

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


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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值