版本4和5间的区别
于2008-11-17 14:10:48修订的的版本4
大小: 1637
编辑: virtuos
备注:
于2008-11-17 14:11:23修订的的版本5
大小: 1675
编辑: virtuos
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 32: 行号 32:
 * 注意Singleton模式的实现。

some tips on writing c++ codes to avoid potential bugs:

  • 先写注释再写代码,规范化的注释。按照doxygen的标准给类、函数、变量写注释。比如,类似的注释一点用处都没有:

       1 //----------------------------------------------------------------------------
       2 // @ SimDoor::SimDoor()
       3 // ---------------------------------------------------------------------------
       4 // Constructor
       5 //----------------------------------------------------------------------------
       6 SimDoor::SimDoor( const RSString& name )
    

    再比如

    比较好的写法应该类似这样:

       1         /// \brief check the block is saved or not
       2         /// \author Chen Zhongke
       3         /// \param filename the filename of the block data file.
       4         /// \result return -1 if filename doesn't exist, otherwise return the index of the file in buffer.
       5         int SimulateIsSaved(RSString filename);
    
    对于函数的注释应该写在头文件的声明中,而不是定义中。
  • 避免重复代码,使用函数。
  • 避免使用预编译

       1 #define kMaxFriends             2
       2 
    

    应该定义成

    static const int kMaxFriends = 2;
    并写在用到它的类中,而不是一个全局的常量。
  • 避免switch,用数据来简化代码
  • 注意代码统计质量,函数不可太长,方法不可太多,不可有太多子类,不可有太多父类
  • 避免指针,避免数组,避免自己写常用的数据结构,用stl
  • 避免用C++实现OO design,注意对象的owner
  • 善用svn,大的修改应该建分支
  • 速度至关重要时,用查表代替计算,避免浮点数,
  • 注意Singleton模式的实现。

C++编程技巧 (2008-11-18 09:51:13由virtuos编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.