user9362338
user9362338

Reputation:

Refactor constructor with similar arguments

I would like to refactor the following code:

public Credito(String numero, String titular, LocalDate fechacaducidad, double credito, int marcainternacional,
            String nombreentidad, int ccv) {
        mNumero = numero;
        mTitular = titular;
        mFechaDeCaducidad = fechacaducidad;
        mCredito = credito;
        mMovimientos = new Vector<Movimiento>();
        mMarcaInternacional = marcainternacional;
        setmNombreEntidad(nombreentidad);
        setmCCV(ccv);
    }

public Credito(String numero, String titular, LocalDate fechacaducidad, int tipo, int marcainternacional,
        String nombreentidad, int ccv) {
    mNumero = numero;
    mTitular = titular;
    mFechaDeCaducidad = fechacaducidad;
    mTipo = tipo;
    mCredito = calcularCredito(mTipo);
    mMovimientos = new Vector<Movimiento>();
    mMarcaInternacional = marcainternacional;
    setmNombreEntidad(nombreentidad);
    setmCCV(ccv);
}

How can I have both constructors and dont ducplicate code?

Upvotes: 0

Views: 171

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1075755

How can I have both constructors and dont ducplicate code?

A couple of ways:

  1. Have a private constructor they both chain to with the common parts, and then have each do its own thing after calling it. For example:

    private Credito(String numero, String titular, LocalDate fechacaducidad, int marcainternacional,
            String nombreentidad, int ccv) {
        mNumero = numero;
        mTitular = titular;
        mFechaDeCaducidad = fechacaducidad;
        mMovimientos = new Vector<Movimiento>();
        mMarcaInternacional = marcainternacional;
        setmNombreEntidad(nombreentidad); // *** See warning
        setmCCV(ccv);                     // *** See warning
    }
    
    public Credito(String numero, String titular, LocalDate fechacaducidad, double credito, int marcainternacional,
            String nombreentidad, int ccv) {
        this(numero, titular, fechacaducidad, marcainternacional, nombreentidad, ccv);
        mCredito = credito;
    }
    
    public Credito(String numero, String titular, LocalDate fechacaducidad, int tipo, int marcainternacional,
            String nombreentidad, int ccv) {
        this(numero, titular, fechacaducidad, marcainternacional, nombreentidad, ccv);
        mTipo = tipo;
        mCredito = calcularCredito(mTipo);
    }
    

    Re See warning - in general, it's problematic to call methods from constructors, for various reasons; see this question's answers for more.

  2. Use the builder pattern, which could well be useful anyway given how many parameters you have there. More in this answer and (less so) this question's answers.

Upvotes: 6

Naresh Bharadwaj
Naresh Bharadwaj

Reputation: 302

You can use something like this.

public Credito(String numero, String titular, LocalDate fechacaducidad, double credito, int marcainternacional,
        String nombreentidad, int ccv) {
    mCredito = credito;
    setmNombreEntidad(nombreentidad);
    commonFunction(numero, titular, fechacaducidad,  marcainternacional, nombreentidad, ccv);
}

public Credito(String numero, String titular, LocalDate fechacaducidad, int tipo, int marcainternacional,
        String nombreentidad, int ccv) {
    mTipo = tipo;
    mCredito = calcularCredito(mTipo);
    setmNombreEntidad(nombreentidad);
    commonFunction(numero, titular, fechacaducidad,  marcainternacional, nombreentidad, ccv);
}

private void commonFunction(String numero, String titular, LocalDate fechacaducidad,  int marcainternacional,
        String nombreentidad, int ccv) {
    mNumero = numero;
    mTitular = titular;
    mFechaDeCaducidad = fechacaducidad;
    mMovimientos = new Vector<Movimiento>();
    mMarcaInternacional = marcainternacional;
    setmCCV(ccv);
}

Upvotes: 0

rmlan
rmlan

Reputation: 4657

The second constructor can simply become:

public Credito(String numero, String titular, LocalDate fechacaducidad, int tipo, int marcainternacional,
        String nombreentidad, int ccv) {
    this(numero, titular, fechacaducidad, calcularCredito(mTipo), marcainternacional, nombreentidad, ccv); 
}

Upvotes: 1

Related Questions