0
点赞
收藏
分享

微信扫一扫

C++核心准则边译边学-P.1:直接用代码表达想法


P.1: Express ideas directly in code(直接用代码表达想法)

Reason(原因)

Compilers don't read comments (or design documents) and neither do many programmers (consistently). What is expressed in code has defined semantics and can (in principle) be checked by compilers and other tools.

编译器不会读注释(或者设计文档),程序员也不会(一贯地)。通过代码表达的内容被语法定义而且可以(理论上)被编译器和其他工具检查。

Example

 

class Date {
public:
Month month() const; // do
int month(); // don't
// ...
};

The first declaration of ​​month​​​ is explicit about returning a ​​Month​​​ and about not modifying the state of the ​​Date​​ object. The second version leaves the reader guessing and opens more possibilities for uncaught bugs.

第一个month的声明明确表达了返回Montrh和不修改Data对象的状态这两件事。第二个声明则需要读者猜想而且留下了更多的发生未知错误的可能性。

Example, bad(反例)

This loop is a restricted form of ​​std::find​​:

循环只是std::find的限定形式。

 

void f(vector<string>& v)
{
string val;
cin >> val;
// ...
int index = -1; // bad, plus should use gsl::index
// 不好, 外加应该使用gsl::index
for (int i = 0; i < v.size(); ++i) {
if (v[i] == val) {
index = i;
break;
}
}
// ...
}

Example, good(正确示例)

A much clearer expression of intent would be:

意图的最明确表达是下面这个样子:

 

void f(vector<string>& v)
{
string val;
cin >> val;
// ...
auto p = find(begin(v), end(v), val); // better,较好
// ...
}

A well-designed library expresses intent (what is to be done, rather than just how something is being done) far better than direct use of language features.

经过良好设计的库会比直接使用语言功能的做法更好的表达意图(做什么而不是只是怎么做)

A C++ programmer should know the basics of the standard library, and use it where appropriate. Any programmer should know the basics of the foundation libraries of the project being worked on, and use them appropriately. Any programmer using these guidelines should know the guidelines support library, and use it appropriately.

C++程序员应该了解标准库的基本知识并且恰当的运用它。任何程序员都应该了解项目正在使用的基本库并能恰当的使用它。使用这个指南的任何程序员都应该知道规则支持库并且恰当的使用它。

Example(示例)

 

change_speed(double s);   // bad: what does s signify?
//不好:s的含义是什么?
// ...
change_speed(2.3)


 


A better approach is to be explicit about the meaning of the double (new speed or delta on old speed?) and the unit used:

比较好的做法是明确double的含义(是新速度还是相对于就速度的变化?)和单位。

 

change_speed(Speed s);    // better: the meaning of s is specified
// 较好:定义了s的含义
// ...
change_speed(2.3); // error: no unit 错误:没有单位
change_speed(23m / 10s); // meters per second 米每秒

We could have accepted a plain (unit-less) ​​double​​​ as a delta, but that would have been error-prone. If we wanted both absolute speed and deltas, we would have defined a ​​Delta​​ type.

我们可以接受一个无修饰的double值作为变化量,但是这容易引发错误。如果即想要绝对速度,又想要速度的变化量,我们需要定义一个Delta类型。

译者注:change_speed(23m/10s)的写法有些少见,我们明天文章中说明。

Enforcement(实施建议)

Very hard in general. 总的来说很难实施。

。use ​​const​​ consistently (check if member functions modify their object; check if functions modify arguments passed by pointer or reference)

坚持使用const(检查成员函数是否修改对象;检查函数是否修改通过指针或引用传递的参数)

。flag uses of casts (casts neuter the type system)

重视类型转换的用法(类型转换会使类型系统无法发挥作用)

。detect code that mimics the standard library (hard)

发现模仿标准库的代码(困难)

 

觉得本文有帮助,欢迎点赞并分享给更多的朋友。

举报

相关推荐

0 条评论