從小就寫字很挫,因此受夠了被人鄙視的感受,今天有個coder忽然跟我說,你的代碼怎麼像小孩寫的同樣,頓時心情沮喪的極點。愈來愈發現一致的編程風格的重要性,因而把Google的C++編程風格指南看了一遍, 這裏記錄下於本身有益的rules。當規則有多個選擇時,這裏只記錄我的習慣的用法, 並不表明它是惟一的用法。編程
命名管理是最重要的一致性規則,所以我把它放在最前面。函數
int num_errors; int num_completed_connections;
my_useful_class.cc
my_useful_class.cc
my_useful_class.h
// classes and structs class UrlTable { ... class UrlTableTester { ... struct UrlTableProperties { ... // typedefs typedef hash_map<UrlTableProperties *, string> PropertiesMap; // enums enum UrlTableErrors { ...
my_exciting_local_variable
my_exciting_member_variable_
struct UrlTableProperties { string name; int num_entries; }
bool gInvalid = false;
const int kDaysInAWeek = 7;
AddTableEntry()
DeleteUrl()
class MyClass { public: ... int num_entries() const { return num_entries_; } void set_num_entries(int num_entries) { num_entries_ = num_entries; } private: int num_entries_; };
void swap(int &a, int &b); int max(int a, int b); bool cmp(Type t1, Type t2);
namespace google_awesome_project { ... }
enum UrlTableErrors { kOK = 0, kErrorOutOfMemory, kErrorMalformedInput, };
#define ROUND(x) ... #define PI_ROUNDED 3.0