During coding for Google Summer of Code, Grilles Caulier help me fix my codes. He shows me many tips that should be pay attention when coding. I make a summary and list them here.
Note:red lines are my code, green lines is modified codes by Grilles.
1.one space is enough between "class" and class name:
class ImageCloneWidget::ImageCloneWidgetPriv |
class ImageCloneWidget::ImageCloneWidgetPriv |
2.Code annotations of adjacent code lines should be aligned.
QPoint startPoint; // the first point of a stroke QPoint dis;//dis means the distance between startPoit and centerPoint, the value can be negative // Current spot position in preview coordinates.
|
QPoint startPoint; // the first point of a stroke QPoint dis; //dis means the distance between startPoit and centerPoint, // the value can be negative.
// Current spot position in preview coordinates. |
3.Use "Enter" appropriately.
QPixmap brushmap = d->m_settings.brush.getPixmap().scaled(QSize(d->m_settings.mainDia,d- >m_settings.mainDia),Qt::KeepAspectRatio); |
QPixmap brushmap = d->settings.brush.getPixmap().scaled(QSize(d->settings.mainDia, d----->settings.mainDia), Qt::KeepAspectRatio); |
4.Align code lines at the beginning and codes follow "//" without spaces:
// DImg* getOrigImage(); DImg* getMaskImg(); //DImg* getPreview(); DImg* getPreviewMask();
|
|
//DImg* getOrigImage(); DImg* getMaskImg(); //DImg* getPreview(); DImg* getPreviewMask();
|
5.Use const ref when pass class arguments
QPoint getDis(); QPoint getOriDis();
|
QPoint getDis() const; QPoint getOriDis() const;
|
6.No spaces between "(" and function parameters. Use a space after "," between two parameters.
bool inimage( DImg *img,const int x,const int y);
|
bool inimage(DImg* img, const int x, const int y);
|
7.Put "*" next to close to class name other than variable name, don't forget "&" when use a const variable as function parameter.
void TreateAsBordor(DImg *image,const int x,const int y);
void addToMask(const QPoint point);
|
void TreateAsBordor(DImg* image, const int x, const int y);
void addToMask(const QPoint& point);
|
8.If widget is based on QObject, do not include header, but moc file instead at top level of file.
#include "imageclonewidget.h" |
#include "imageclonewidget.moc" |
9.With slots declaration in header, you must specify public/protected/private with QT macro.
Q_SLOTS: void setPreview();
|
public Q_SLOTS: void setPreview();
|
10.No space between last function parameter and ")".
explicit ImageCloneWidget(QWidget* parent=0,const CloneContainer& settings =CloneContainer() ); |
explicit ImageCloneWidget(QWidget* parent=0, const CloneContainer& settings=CloneContainer()); |
11.Put private class member of a class at the bottom.
private: class ImageCloneWidgetPriv; ImageCloneWidgetPriv* const d; private: bool timerEvent(QTimerEvent*); |
private: bool timerEvent(QTimerEvent*);
private: class ImageCloneWidgetPriv; ImageCloneWidgetPriv* const d; };
|
12.Align variable names in adjacent lines.
QMap<int,CloneBrush> brushMap; int brushID; bool selectMode; bool drawMode; bool drawEnable; |
QMap<int, CloneBrush> brushMap; int brushID; bool selectMode; bool drawMode; bool drawEnable;
|
13.Never return NULL for a QPixmap().
QPixmap CloneBrush::getPixmap() { if(!brushMap.isNull()) return brushMap;
else return NULL; }
|
QPixmap CloneBrush::getPixmap() { if(!brushMap.isNull())
return brushMap; else return QPixmap(); }
|
14.Never user tabs in source code. use 4 spaces instead.