C++ Coding Standards Item 5 : Give One entity one cohesive responsibility

博客围绕C++编码标准展开,强调同一时间只关注一件事,每个程序实体应只有一个明确职责。当实体扩展时,职责范围可扩大,但不应有不同发展方向。还通过标准C的realloc函数和标准C++的basic_string类举例,说明多职责实体设计的弊端。

http://spaces.msn.com/members/spiritauding/Blog/cns!1psm74keJLzaQ6CnZ_EB1mAw!120.entry

C++ Coding Standards Item 5 : Give One entity one cohesive responsibility
Summary

Focus on one thing at a time: Prefer to give each entity (variable, class, function, namespace, module, library) one well-defined responsibility. As an entity grows, its scope of responsibility naturally increases, but its responsibility should not diverge.

同一时间只关注一件事情:最好给每一个实体(variable, class, function, namespace, module, library)一个明确的职责。当实体增长(扩展,怎么翻译好一点?)的时候,它的职责范围自然的扩大了,但是它的职责不应该有不同的发展方向。


Discussion
A good business idea, they say, can be explained in one sentence. Similarly, each program entity should have one clear purpose.

一个好的商业计划可以用一句话说明。类似的,一个好的程序实体应该有一个清楚地目的。


这条规则主要讲有很多功能(职责)的实体,它们通常很难设计,用起来也很困难,所以,还是让我们的实体(class,function……)目的单一些,单纯一点好。


Prefer to build higher-level abstractions from smaller lower-level abstractions. Avoid collecting several low-level abstractions into a larger low-level conglomerate. Implementing a complex behavior out of several simple ones is easier than the reverse.

最好在比较小的低级别的抽象基础上建立高级别的抽象。避免把许多小的低级别抽象聚合到一个大的低级别的抽象中。相对而言,通过几个简单的实体实现一个复杂的行为,要比通过一个实体来实现简单的多。(这句话好难讲!!)



Examples
Example 1: realloc. In Standard C, realloc is an infamous example of bad design. It has to do too many things: allocate memory if passed NULL, free it if passed a zero size, reallocate it in place if it can, or move memory around if it cannot. It is not easily extensible. It is widely viewed as a shortsighted design failure.

Example 1 讲的是标准C语言中的realloc函数,realloc必须要做太多的事情了:传入NULL的话就分配内存,传入0值得话就释放内存,如果在那个地方(程序执行点)可以realloc的话,就正常的工作,否则就要做内存移动……。而且不能很容易的扩展。基本上都认为它(realloc)的设计是很失败的。

Example 2: basic_string. In Standard C++, std::basic_string is an equally infamous example of monolithic class design. Too many "nice-to-have" features were added to a bloated class that tries to be a container but isn't quite, is undecided on iteration vs. indexing, and gratuitously duplicates many standard algorithms while leaving little space for extensibility. (See Item 44's Example.)

Example 2:basic_string.又一个在单一功能的类的设计中的反面例子,它的“罪过”我就不逐句翻译了,大家读原文也能清楚。


这章讲述的内容比较简单,以前我总是注意不到,或者说在刻意的违反,总是希望一个类可以做这个也可以做那个,呵呵,记得我曾经把几个功能函数全部写在一个 CPP文件中,无论从程序设计角度还是文学欣赏的角度看,那个文件都极度的不具备可读性,所以它也从我的历史中永久的消失了……


Copy Left (C) Scorpio Auding
### C++ 编程的代码规范、规则、指南和最佳实践 C++ 是一种复杂且功能强大的编程语言,其编码规范对于确保代码的可读性、可维护性和性能至关重要。以下是关于 C++ 编码标准的详细说明,涵盖从基本规则到高级实践的内容。 #### 3.1 命名约定 在 C++ 中,清晰的命名约定有助于提高代码的可读性和理解性。变量、函数和类的命名应具有描述性,并遵循一致的风格。例如,使用驼峰命名法(camelCase)或下划线分隔(snake_case)[^2]。 ```cpp // 示例:使用下划线分隔命名 int student_count = 0; // 示例:使用驼峰命名法 void calculateTotalScore() {} ``` #### 3.2 操作符重载 操作符重载是 C++ 的一项强大功能,但必须谨慎使用以避免混淆。递增和递减操作符的前缀和后缀形式应与内置类型的行为保持一致。实现时,优先调用前缀版本以减少冗余代码[^1]。 ```cpp class Counter { public: Counter& operator++() { // 前缀形式 ++value; return *this; } Counter operator++(int) { // 后缀形式 Counter old(*this); ++(*this); // 调用前缀版本 return old; } private: int value; }; ``` #### 3.3 内存管理 内存管理是 C++ 编程中的关键部分。为了防止内存泄漏和悬挂指针问题,推荐使用智能指针(如 `std::unique_ptr` 和 `std::shared_ptr`)。这些工具可以自动管理动态分配的对象生命周期[^3]。 ```cpp #include <memory> std::unique_ptr<int> createInt() { return std::make_unique<int>(42); // 使用 make_unique 创建对象 } ``` #### 3.4 单元测试 单元测试是验证代码正确性的有效方法。通过编写自动化测试用例,可以确保代码在修改后仍然按预期工作。建议使用成熟的测试框架(如 Google Test)来简化测试过程[^3]。 ```cpp #include <gtest/gtest.h> TEST(ArithmeticTest, Addition) { EXPECT_EQ(2 + 2, 4); // 简单加法测试 } ``` #### 3.5 遵循编码标准 遵循既定的编码标准可以显著提高团队协作效率。例如,《C++ Coding Standards: 101 Rules, Guidelines, and Best Practices》提供了许多实用的规则和指南,涵盖了从命名约定到资源管理等多个方面[^2]。 #### 3.6 性能优化 性能优化是 C++ 开发中的重要环节。合理利用右值引用和移动语义可以减少不必要的拷贝操作,从而提升程序性能[^2]。 ```cpp std::vector<int> generateData() { return std::vector<int>(1000, 42); // 返回临时对象 } void process(std::vector<int>&& data) { // 移动语义:data 的内容被转移而不是复制 } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值