Clean constructors
-
Good question. I think it means how to write constructors correctly. Like:
@
explicit MyWidget(QWidget* parent, ...etc): BaseWidget(parent) {}
@ -
My understanding of it is to just have constructors that are simple and only take a very small number of arguments (if any).
For example it is better to have code like:
@
ProgressBar* progress = new ProgressBar( this );
progress->setRange( 0, 1000 );
progress->setValue( 230 );
@as opposed to
@
ProgressBar* progress = new ProgressBar( 0, 1000, 230, this );
@as you just end up with a bunch of meaningless numbers in your code. With the former you can see the numbers in the context of the member function which gives a clue as to their meaning.
-
I was thinking of this "article":http://doc.qt.nokia.com/qq/qq13-apis.html#theconveniencetrap when I wrote my reply.
-
In the Qt API, it is standard that the parent argument should be last, and should have a default value of 0. So it would be
@
MyWidget(int realImportantArgument, QWidget* parent = 0);
@The article ZapB links to is insightful if you want to learn about API design.
-
Looks like that Qt Essentials Curriculum Block is not perfect. I see that certified specialists don't give an exact answer but explain only their own understanding of the term "clean constructor".
Therefore I realize that there isn't a documented definition of this term. -
Unfortunately, I also don't know the proper definition. I found this topic searching on the Internet to find it.
And as you can see at http://qt.nokia.com/developer/learning/certification/exams/preparation-prerequisites/qt-curriculum/qt-essentials clean constructors are in the Essentials block (See section "Writing your own widgets") -
Here are some more links that provide guidelines: