Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to refactor this if else code containing object constructors?

How to refactor this if else code containing object constructors?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 289 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • GuerrianG Offline
    GuerrianG Offline
    Guerrian
    wrote on last edited by
    #1

    QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
    {
    QLineEdit *lineEdit = new QLineEdit(parent);
    if (index.column() == 0)
    {
    QRegularExpression regExp(REG_EXP1);
    QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, parent);
    lineEdit->setValidator(validator);
    }
    else
    {
    QRegularExpression regExp(REG_EXP2);
    QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, parent);
    lineEdit->setValidator(validator);
    }
    return lineEdit;
    }

    There must be a way of refactoring this so that it is shorter.

    Linux Mint 18.3
    Qt 5.14.1
    Qt Creator 4.11.1

    Paul ColbyP 1 Reply Last reply
    0
    • GuerrianG Guerrian

      QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
      {
      QLineEdit *lineEdit = new QLineEdit(parent);
      if (index.column() == 0)
      {
      QRegularExpression regExp(REG_EXP1);
      QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, parent);
      lineEdit->setValidator(validator);
      }
      else
      {
      QRegularExpression regExp(REG_EXP2);
      QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, parent);
      lineEdit->setValidator(validator);
      }
      return lineEdit;
      }

      There must be a way of refactoring this so that it is shorter.

      Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by Paul Colby
      #2

      Hi @Guerrian,

      There must be a way of refactoring this so that it is shorter.

      Well, shorter is not necessarily always best (eg when it reduces readability), but in this case, I'd write it like:

      QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
      {
          QRegularExpression regExp((index.column() == 0) ? REG_EXP1 : REG_EXP2);
          QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, parent);
          QLineEdit * const lineEdit = new QLineEdit(parent);
          lineEdit->setValidator(validator);
          return lineEdit;
      }
      

      or possibly:

      QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
          QRegularExpression regExp((index.column() == 0) ? REG_EXP1 : REG_EXP2);
          QLineEdit *lineEdit = new QLineEdit(parent);
          lineEdit->setValidator(new QRegularExpressionValidator(regExp, parent));
          return lineEdit;
      }
      

      It is possible to make it even shorter, but I think readability begins to be impacted then.

      Cheers.

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved