Results 1 to 8 of 8

Thread: Need a second opinion on that piece of code...

  1. #1
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Need a second opinion on that piece of code...

    Hello!

    I wrote a class to send mail using Qxt, it works but I am sure lots of things could be done better. Any suggestions?
    Qt Code:
    1. Mailsender::Mailsender()
    2. {
    3. ...
    4. connect(smtp, SIGNAL(mailSent (int)), this, SLOT(sendingMailSucceeded ( int )));
    5. connect(smtp, SIGNAL(mailFailed (int,int)), this, SLOT(sendingMailFailed ( int , int )));
    6. ...
    7. int rv = smtp->send(*message); //send mail
    8.  
    9. rvMailSend = 1; //private variable to set mail status
    10.  
    11. //private QProgressBar*
    12. progress = new QProgressDialog("Sending mail...","abort" ,0 , 0, this);
    13. progress->setWindowModality(Qt::ApplicationModal);
    14. progress->setCancelButton (0); //do not show
    15.  
    16. //private QTimer*
    17. Timer1 = new QTimer(this);
    18. Timer1->start(1000);
    19. connect(Timer1, SIGNAL(timeout()), this, SLOT(mailSendingLoop()));
    20.  
    21. Timer2 = new QTimer(this);
    22. Timer2->setSingleShot(true);
    23. Timer2->setInterval(10000);
    24. Timer2->start();
    25. connect(Timer2, SIGNAL(timeout()), this, SLOT(mailSendingTimeout()));
    26.  
    27. progress->exec(); //show progressbar
    28.  
    29. } //end of constructor
    30.  
    31.  
    32. void MailSender::sendingMailFailed ( int mailID, int errorCode )
    33. {
    34. rvMailSend = 1; //set via signal if mail failed
    35. }
    36.  
    37. void MailSender::sendingMailSucceeded ( int mailID )
    38. {
    39. rvMailSend = 0; //set via signal if mail succeeded
    40. }
    41.  
    42.  
    43. int MailSender::mailSendingLoop() //called regular by a QTimer
    44. {
    45. if(rvMailSend == 0)
    46. {
    47. progress->close();
    48. QMessageBox msgBox;
    49. msgBox.setText("Mail send!");
    50. msgBox.setStandardButtons(QMessageBox::Ok);
    51. msgBox.setDefaultButton( QMessageBox::Ok);
    52. Timer1->stop();
    53. Timer2->stop();
    54. int ret = msgBox.exec();
    55.  
    56. if (ret == QMessageBox::Ok)
    57. {
    58. this->close();
    59. return 0;
    60. }
    61.  
    62. }
    63.  
    64.  
    65. }
    66.  
    67. int MailSender:: mailSendingTimeout() //Timeout, called on Timer2
    68. {
    69. progress->close();
    70. QMessageBox msgBox;
    71. msgBox.setText("Sending mail failed!");
    72. msgBox.setStandardButtons(QMessageBox::Ok);
    73. msgBox.setDefaultButton( QMessageBox::Ok);
    74. Timer1->stop();
    75. Timer2->stop();
    76. int ret = msgBox.exec();
    77.  
    78. if (ret == QMessageBox::Ok)
    79. {
    80. this->close();
    81. return 1;
    82. }
    83.  
    84. }
    To copy to clipboard, switch view to plain text mode 
    Kind regards,
    HomeR[/CODE]

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Need a second opinion on that piece of code...

    From design perspective, you are doing a lot of actual work in the constructor - its not the place for it - constructors are for initialization.
    You say it works, so I wont argue, but your constructor is sending a message, without taking the message as parameter - so do you always send a hard coded message?
    Also, you are using a progress bar, but never update it, you only look if the message is sent or not (by the way you need to correct "send" to "sent" when you use past tense) - so what is the progress bar for?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: Need a second opinion on that piece of code...

    Yes, true, the constructor is a bit overloaded, I often do this first and split the stuff into functions later, bad habbit

    The progress bar is just used a busy indicator, this is possible by setting setMinimum and setMaxium to 0:

    Qt Code:
    1. progress = new QProgressDialog("Sending mail...","abort" ,0 , 0, this);
    To copy to clipboard, switch view to plain text mode 

    but your constructor is sending a message, without taking the message as parameter - so do you always send a hard coded message?
    I don't know what you mean, could you please explain?

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Need a second opinion on that piece of code...

    Having some kind of timeout mechanism is sounds sensible to me, but what is the purpose of checking if a value is 0 every second ? Can't you stop the timers and close progress when you receive "mailSent" signal ? Code could be clearer with just timeout timer.
    Another thing is that there is no error reporting at all, if sending fails you'll have no clue why, maybe it was not your fault at all ( some kind of server problems for example ).
    Last thing I'd change is the message box code:
    Qt Code:
    1. QMessageBox msgBox;
    2. msgBox.setText("Mail send!");
    3. msgBox.setStandardButtons(QMessageBox::Ok);
    4. msgBox.setDefaultButton( QMessageBox::Ok);
    5. Timer1->stop();
    6. Timer2->stop();
    7. int ret = msgBox.exec();
    8.  
    9. if (ret == QMessageBox::Ok)
    10. {
    11. this->close();
    12. return 0;
    13. }
    To copy to clipboard, switch view to plain text mode 
    could be simpler by using:
    Qt Code:
    1. QMessageBox::information(this,"",tr("Mail sent!"));
    2. Timer1->stop();
    3. Timer2->stop();
    4. this->close();
    5. return 0;
    To copy to clipboard, switch view to plain text mode 
    Same for timeout message box (maybe with QMessageBox::warning or critical).
    I hope that it helps you somehow

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Need a second opinion on that piece of code...

    I don't know what you mean, could you please explain?
    See line 7 in your code above.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: Need a second opinion on that piece of code...

    Thanks for the good hints so far, some way to go

    but your constructor is sending a message, without taking the message as parameter - so do you always send a hard coded message?
    Yes, this was my original plan, to check this return value and go on from there, but as this does only return the "message number" and does not show if the message was send or not. This is the funtion:

    Qt Code:
    1. int QxtSmtp::send(const QxtMailMessage& message)
    2. {
    3. int messageID = ++qxt_d().nextID;
    4. qxt_d().pending.append(qMakePair(messageID, message));
    5.  
    6. if (qxt_d().state == QxtSmtpPrivate::Waiting)
    7. qxt_d().sendNext();
    8.  
    9. return messageID;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Can't you stop the timers and close progress when you receive "mailSent" signal ? Code could be clearer with just timeout timer.
    Yes, I thought of this, but since I am not so familiar with Qt yet, I don't know how long the life cycle of the MailSender-object is. So I thought to keep it alive with a QTimer, does this sound stupid?
    Ah, I guess I get what you mean - let just the singleShot-QTimer run, and wait for the signal?

    Another thing is that there is no error reporting at all, if sending fails you'll have no clue why
    Yes, this will be done for sure. And I will change the MessageBoxes, too.

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Need a second opinion on that piece of code...

    Ah, I guess I get what you mean - let just the singleShot-QTimer run, and wait for the signal?
    Yes, leave just the "timeout" timer, and move the "finalizing" code from mailSendingLoop to sendingMailSucceeded method.
    I don't know how long the life cycle of the MailSender-object is
    Well, its up to you to make sure that smtp pointer will remain valid as long as you need it Btw is it a member of MailSender class ? It should be IMHO.

  8. #8
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: Need a second opinion on that piece of code...

    Btw is it a member of MailSender class ? It should be IMHO
    Yes, it is.

    Thanks for all the good hints!

Similar Threads

  1. execute a small piece of javascript code ?
    By cornucopia in forum Qt Webkit
    Replies: 2
    Last Post: 28th February 2012, 14:00
  2. will this piece of code related to phonon works?
    By Lakshmi.Bollavaram in forum Installation and Deployment
    Replies: 1
    Last Post: 20th November 2009, 08:57
  3. How to execute a small piece of javascript code ?
    By cornucopia in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2009, 10:03
  4. I need a project opinion.
    By hgedek in forum General Discussion
    Replies: 6
    Last Post: 25th September 2007, 18:54
  5. Your opinion about Google Web Toolkit UI
    By patrik08 in forum General Programming
    Replies: 1
    Last Post: 21st May 2007, 05:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.