Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Object, which is going to be constructed, has several constant. What of ways to place constants is better and why?

  1. The first way is function for each member:

    BarcodeObject::BarcodeObject() : WareRelatedObject("BARCODE")
    {
        createBarcodeItem();
        createFactorItem();
    }
    
    void BarcodeObject::createBarcodeItem()
    {
        QString caption = tr("Штрихкод");
        QString sqlbind = "BARCODE.BARCODE";
        mBarcode = new StringItem(mainGroup(), caption, sqlbind);
    
        LineEditConstraint maxLength(40);
        mBarcode->setConstraint(QVariant::fromValue(maxLength));
    }
    
    void BarcodeObject::createFactorItem()
    {
        QString sqlbind = "BARCODE.FACTOR";
        QString caption = tr("Коэффициент");
        mFactor = new ValueNumericItem(mainGroup(), caption, sqlbind);
    
        PosNumeric defaultValue("1");
        NumericSpinBoxConstraint constraint = NumericSpinBoxConstraint::valueConstraint(defaultValue);
        mFactor->setConstraint(QVariant::fromValue(constraint));
    }
    
  2. The second way is to place all constants to namespace:

    namespace {
        const QString barcodeCaption = QObject::trUtf8("Штрихкод");
        const QString barcodeSqlBind = "BARCODE.BARCODE";
        const LineEditConstraint barcodeConstraint(40);
    
        const QString factorCaption = QObject::trUtf8("Коэффициент");
        const QString factorSqlBind = "BARCODE.FACTOR";
        const NumericSpinBoxConstraint factorConstraint = NumericSpinBoxConstraint::valueConstraint(PosNumeric("1"));
    }
    
    BarcodeObject::BarcodeObject() : WareRelatedObject("BARCODE")
    {
        mBarcode = new StringItem(mainGroup(), barcodeCaption, barcodeSqlBind);
        mBarcode->setConstraint(QVariant::fromValue(barcodeConstraint));
    
        mFactor = new ValueNumericItem(mainGroup(), factorCaption, factorSqlBind);
        mFactor->setConstraint(QVariant::fromValue(factorConstraint));
    }
    
share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.