QT編碼風格

2021-06-08 15:13:53 字數 2525 閱讀 6359

如果它使你的**看起來不好,你可以打破任何乙個規則

// wrong

int a, b;

char *c, *d;

// correct

int height;

int width;

char *nameofthis;

char *nameofthat;

// wrong

short cntr;

char item_delim = '/t';

// correct

short counter;

char itemdelimiter = '/t';

在qt例子編寫中,對變數名有如下建議:

void myclass::setcolor(const qcolor &color;)

void myclass::setcolor(const qcolor &newcolor;)

避免使用(意義不明確的字元):

void myclass::setcolor(const qcolor &c)

注意:在建構函式中,會遇到同樣的問題。但無論你信與不信,下面的可以工作

myclass::myclass(const qcolor &color;)

: color(color)

// wrong

if(foo)

// correct

if (foo)

char *x;

const qstring &mystring;

const char * const y = "hello";

// wrong

char* blockofmemory = (char* ) malloc(data.size());

// correct

char *blockofmemory = reinterpret_cast(malloc(data.size()));

//wrong

x = rect.x();

y = rect.y();

width = rect.width();

height = rect.height();

// wrong

if (codec)

// correct

if (codec)

class moo

;

// wrong

if (address.isempty())

// correct

if (address.isempty())

return false;

if (x)

// correct

if (address.isempty() || !isvalid()

|| !codec)

// wrong

if (address.isempty())

return false;

else

// correct

if (address.isempty()) else

// wrong

if (a)

if (b)

...else

...// correct

if (a)

// wrong

while (a);

// correct

while (a) {}

// wrong

if (a && b || c)

// correct

if ((a && b) || c)

// wrong

a + b & c

// correct

(a + b) & c

switch (myenum)
// correct

if (longexpression

+ otherlongexpression

+ otherotherlongexpression)

//wrong

if (dsfljfsfskjldsjkljklsjdk

&& fdsljsjdsdljklsjsjkdfs

&& dsfljkdfjkldksdfjdjkfdksfdkjld)

//correct

if (dsfljfsfskjldsjkljklsjdk

&& fdsljsjdsdljklsjsjkdfs

&& dsfljkdfjkldksdfjdjkfdksfdkjld)

對 whle 或else if,不存在這個問題:

while (dsfljfsfskjldsjkljklsjdk

&& fdsljsjdsdljklsjsjkdfs

&& dsfljkdfjkldksdfjdjkfdksfdkjld)

Qt編碼風格

如果它使你的 看起來不好,你可以打破任何乙個規則。wrong int a,b char c,d correct int height int width char nameofthis char nameofthat wrong short cntr char item delim t correc...

Qt編碼風格

如果它使你的 看起來不好,你可以打破任何乙個規則。wrong int a,b char c,d correct int height int width char nameofthis char nameofthat wrong short cntr char item delim t correc...

Qt編碼風格 轉在dbzhang008

分類 c c qt 2011 05 01 12 03 1723人閱讀收藏 舉報 如果它使你的 看起來不好,你可以打破任何乙個規則。wrong int a,b char c,d correct int height int width char nameofthis char nameofthat w...