April 8, 2011

kibsoft kibsoft
Lab Rat
66 posts

Clean constructors

 

Hi everyone. I saw such line in the Qt Essentials Curriculum Block: “Understand the importance of clean constructors” (Writing your own widgets).

So, what means “clean constructor”?

10 replies

April 8, 2011

Flux Flux
Lab Rat
141 posts

Good question. I think it means how to write constructors correctly. Like:

  1. explicit MyWidget(QWidget* parent, ...etc): BaseWidget(parent) {}

April 8, 2011

ZapB ZapB
Ant Farmer
1424 posts

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:

  1. ProgressBar* progress = new ProgressBar( this );
  2. progress->setRange( 0, 1000 );
  3. progress->setValue( 230 );

as opposed to

  1. 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.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

April 8, 2011

kibsoft kibsoft
Lab Rat
66 posts

I was inclined to

explicit MyWidget(QWidget* parent, …etc): BaseWidget(parent) {}

Maybe Trolls themselves will answer? What is correct?

April 8, 2011

ZapB ZapB
Ant Farmer
1424 posts

I was thinking of this article [doc.qt.nokia.com] when I wrote my reply.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

April 8, 2011

Andre Andre
Robot Herder
6393 posts

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

  1. MyWidget(int realImportantArgument, QWidget* parent = 0);

The article ZapB links to is insightful if you want to learn about API design.

June 8, 2011

VC15 VC15
Lab Rat
25 posts

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.

June 8, 2011

Andre Andre
Robot Herder
6393 posts

Would you mind giving it then? You seem in the know?

Note that this, IMHO, does not belong in the essentials curriculum at all. Perhaps in the C++ for Qt one though.

June 8, 2011

VC15 VC15
Lab Rat
25 posts

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”)

June 8, 2011

Andre Andre
Robot Herder
6393 posts

Here are some more links that provide guidelines:

June 8, 2011

VC15 VC15
Lab Rat
25 posts

Oh, great! These articles look to be very useful. Thanks a lot!

 
  ‹‹ [Split] Font color in QTextEdit      [solved] Please help me understand QThread ››

You must log in to post a reply. Not a member yet? Register here!