TC++PL's Advices(1)

全为自己翻译, 如有不妥, 望请斧正.

Programming In C++:
[1]  Represent ideas directly in code.
[2]  Represent relationships among ideas directly in code (e.g., hierarchical, parametric, and
ownership relationships).
[3]  Represent independent ideas independently in code.
[4]  Keep simple things simple (without making complex things impossible).
     以上的意思是, 代码要更简单的表达思维, 而不是相反
[5]  Prefer statically type-checked solutions (when applicable).
     尽量使用静态类型检查
[6]  Keep information local (e.g., avoid global variables, minimize the use of pointers).
     尽量使用局部变量, 尽可能少使用指针
[7]  Don’t overabstract (i.e., don’t generalize, introduce class hierarchies, or parameterize
     beyond obvious needs and experience).
     不要过度抽象


To C++ers:
[1]  Use constructors to establish invariants.
     使用构造器创建常量
[2]  Use constructor/destructor pairs to simplify resource management.
     成对使用构造函数与析构函数以简便地管理资源
[3]  Avoid ‘‘naked’’ new and delete.
     避免使用裸指针
[4]  Use containers and algorithms rather than built-in arrays and ad hoc code.
     使用 STL 的容器与算法, 而不是内置的数组
[5]  Prefer standard-library facilities to locally developed code.
     尽量使用标准库, 除非你有自信更好
[6]  Use exceptions, rather than error codes, to report errors that cannot be handled locally.
     使用 exception 机制, 而不是放任错误的代码, 报告本地难以解决的问题
[7]  Use move semantics to avoid copying large objects.
     移动较大的对象, 而不是拷贝之
[8]  Use unique_ptr to reference objects of polymorphic type.
     使用 unique_ptr 去引用实现多态的对象
[9]  Use shared_ptr to reference shared objects, that is, objects without a single owner that is
     responsible for their destruction.
     使用 shared_ptr 引用需要被分享的对象
[10] Use templates to maintain static type safety (eliminate casts) and avoid unnecessary use
     of class hierarchies.
     使用模版实现静态类型安全, 避免不必要的继承


To C-ers
[1]  Don’t think of C++ as C with a few features added. C++ can be used that way, but only
     suboptimally. To get really major advantages from C++ as compared to C, you need to
     apply different design and implementation styles.
     别觉得 C++ 就是 C 加了点东西, 可以把 C++ 照着 C 这么用, 但不一定是最优选择, 新语言, 新思维
[2]  Don’t write C in C++; that is often seriously suboptimal for both maintenance and perfor-
     mance.
     别把 C++ 和 C 混着用, 这会影响双方的可维护性与性能
[3]  Use the C++ standard library as a teacher of new techniques and programming styles.
     Note the difference from the C standard library (e.g., = rather than strcpy() for copying
     and == rather than strcmp() for comparing).
     以 C++ 标准库为师, 注意 C++ 与 C 的异同
[4]  Macro substitution is almost never necessary in C++. Use const, constexpr, enum or enum class  to      define manifest constants, inline to avoid function-calling overhead, templates to specify families      of functionsand types, and namespaces to avoid name clashes.
     宏在 C++ 里不是必要的, 我设计 inline, enum, const, 模版命名空间啥的就是想替换它的


[5]  Don’t declare a variable before you need it, and initialize it immediately. A declaration
     can occur anywhere a statement can, in for -statement initializers, and in conditions.
     尽可能延后变量声明时间
[6]  Don’t use malloc() . The new operator does the same job better, and instead of
     realloc() , try a vector. Don’t just replace malloc() and free() with ''naked'' new and delete .
     用 new 与 delete 取代 malloc 与 free, 但也别直接替换, 想要可变数组请使用 vector 而不是 realloc
[7]  Avoid void∗ , unions, and casts, except deep within the implementation of some function
     or class. Their use limits the support you can get from the type system and can harm per-
     formance. In most cases, a cast is an indication of a design error. If you must use an
     explicit type conversion, try using one of the named casts (e.g., static_cast) for a
     more precise statement of what you are trying to do.
     避免使用 void* union 与 cast, 如果非要显式转换, 请用 static_cast 什么的
[8]  Minimize the use of arrays and C-style strings. C++ standard-library strings, arrays, 
     and vectors can often be used to write simpler and more maintainable code compared to the      traditional C style. In general, try not to build yourself what has
     already been provided by the standard library.
     避免使用 C 风格的数组与字符串, vector 可以更简单的满足你; 多用标准库, 除非你更牛
[9]  Avoid pointer arithmetic except in very specialized code (such as a memory manager) and
     for simple array traversal (e.g., ++p ).
     避免指针的四则运算, 除非是遍历数组
[10] Do not assume that something laboriously written in C style (avoiding C++ features such
     as classes, templates, and exceptions) is more efficient than a shorter alternative (e.g.,
     using standard-library facilities). Often (but of course not always), the opposite is true.
     你自己搞的东西真不一定有标准库性能好, 别想多了

你可能感兴趣的:(advice)