what is the best way to not use many if?

3

I have a combo that is filled with a list, which has 29 elements, and each element by selecting it in the combo displays more fields, then I have the following conditions, what it does is validate which document you selected and render the input corresponding to that selected document, but I use many if, and I feel that it is not good practice, how can I improve it or can I implement it?

//Dependiendo el tipo de docuemnto de nacionalidad, se mostraran campos
public void obtenerDocumentosNacionalidad() {

    if (systiposprobatorios.getIdSysTipoProbatorio() == 1) {
        muestraDetalleDocuemnto = true;

    } else {
        muestraDetalleDocuemnto = false;
    }
    if (systiposprobatorios.getIdSysTipoProbatorio() == 2) {
        muestraDetalleActa = true;

    } else {
        muestraDetalleActa = false;
    }
    if (systiposprobatorios.getIdSysTipoProbatorio() == 3) {
        muestraDetalleDeclaratoria = true;

    } else {
        muestraDetalleDeclaratoria = false;
    }
    if (systiposprobatorios.getIdSysTipoProbatorio() == 4) {
        muestraDetalleCertificado = true;

    } else {
        muestraDetalleCertificado = false;
    }
    if (systiposprobatorios.getIdSysTipoProbatorio() == 5) {
        muestraDetalleCedula = true;

    } else {
        muestraDetalleCedula = false;
    }
    if (systiposprobatorios.getIdSysTipoProbatorio() == 6) {
        muestraCertifiMatricula = true;

    } else {
        muestraCertifiMatricula = false;
    }
}
    
asked by Root93 12.01.2018 в 16:46
source

3 answers

6

Since the variables are Boolean, doing a check is unnecessary:

public void obtenerDocumentosNacionalidad() {
    int valor=systiposprobatorios.getIdSysTipoProbatorio();

    muestraDetalleDocumento=(valor==1);
    muestraDetalleActa=(valor==2);
    muestraDetalleDeclaratoria=(valor==3);
    muestraDetalleCertificado=(valor==4);
    muestraDetalleCedula=(valor==5);
    muestraCertifiMatricula=(valor==6);
 }
    
answered by 12.01.2018 / 17:09
source
0

In this case instead of reading inside the method the value of systiposprobatorios.getIdSysTipoProbatorio() , you can send it as a parameter:

obtenerDocumentosNacionalidad(systiposprobatorios.getIdSysTipoProbatorio());

and your method would remain this way, to assign the correct values in the variables:

public void obtenerDocumentosNacionalidad(int tipo) {
    muestraDetalleDocumento = (tipo==1);
    muestraDetalleActa = (tipo==2);
    muestraDetalleDeclaratoria = (tipo==3);
    muestraDetalleCertificado = (tipo==4);
    muestraDetalleCedula = (tipo==5);
    muestraCertifiMatricula = (tipo==6);
 }
    
answered by 12.01.2018 в 17:36
0

From what I see in your code "sampleDetailDocument" is true provided that "systiposprobatorios.getIdSysTipoProbatorio ()" is an integer between 1 and 6 and is false in any other case. I think what suits you is something like this:

if (systiposprobatorios.getIdSysTipoProbatorio()>= 1 && systiposprobatorios.getIdSysTipoProbatorio()<=6) {
        muestraDetalleDocumento = true;  }
else
{  muestraDetalleDocumento = false;  }

That is, if the conditional function reads a number between 1 and 6 "sampleDetailDocument" will be true, otherwise it will be false.

    
answered by 12.01.2018 в 22:51