PSR
PSR

Reputation: 40338

How I can make this code better

I have fields like

variable1,variable2,variable3....etc

and my conditions are

if(variable1 != null && variable1 != 0 )
  myobject.setFirstValue(variable1);

if(variable2 != null && variable2 != 0 )
 myobject.setSecondValue(variable2);

if(variable3 != null && variable3 != 0 )
 myobject.setThirdValue(variable3);

Like this I have nearly 15 to consitions. Is there any way to write all these conditions in a simple way.The variable names are not exactly same what I posted . They are different from what I posted.

Upvotes: 1

Views: 186

Answers (6)

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41220

you can create a method isNotNullorequalsZero which checks not null and not equals zero

private boolean isNotNullorequalsZero(Integer value){
     return value != null && value != 0;
}

and you use that -

if(isNotNullorequalsZero(variable1) )
  myobject.setFirstValue(variable1);

if(isNotNullorequalsZero(variable2) )
 myobject.setFirstValue(variable2);

if(isNotNullorequalsZero(variable3) )
 myobject.setFirstValue(variable3);

Upvotes: 5

Pierre Henry
Pierre Henry

Reputation: 17487

If this is something that really occurs frequently in your code and you want to encapsulate this behaviour and avoid to repeat it, I would do something like that :

In a tools or utils class, create a static method :

public static void setPropertyIfNonNullNorZero(Object target, String propertyName, Integer value){

    //use some reflection tooling here, or write the whole thing yourself... I am  using commons.beanutils here

    if(value != null && value != 0){
        BeanUtils.setProperty(target, propertyName, value);
    }

}

This code assumes that your target object (myObject) respects the bean conventions (basically consitent getters and setters naming) and the the value is always an Integer.

Then use it like this :

Tools.setPropertyIfNonNullNorZero(myobject, "thirdValue", variable3);

Upvotes: 0

Raman
Raman

Reputation: 1376

You can put all the variables in an array and use a for loop to check the condition and set the values in myobject.setFirstValue(array[index]);

Upvotes: 0

Ankit
Ankit

Reputation: 6622

either modify setFirstValue method as

void setFirstValue(Integer var){
if(var !=null && var !=0) {
//current logic of setFirstValue
}
}

or if not possible to modify it, create a wrapper call as (return type boolean to notify user if call fails)

boolean checkAndSetFirstValue(Integer var) {
    if(var !=null && var !=0){
        setFirstValue(var);
        return true;
    }
    return false;
}

Upvotes: 0

mthmulders
mthmulders

Reputation: 9705

You could make a method called something like copyIfNonZero, and it would probably look like this:

public void copyIfNonZero(MyObject myObject, Integer value) {
    if (value != null && value != 0) {
        myObject.setFirstValue(value);
    }
}

And current code would be reduced to

copyIfNonZero(myobject, variable1);
copyIfNonZero(myobject, variable2);
copyIfNonZero(myobject, variable3);
...

Or even put the variables in a Collection and iterate over that:

Collection<Integer> values = .....
for(Integer value : values) {
    copyIfNonZero(myObject, value);
}

Upvotes: 0

blank
blank

Reputation: 18170

You could start with ...

if (notNullOrZer0(variable1)) {
   ...
}

private boolean notNullOrZero(Integer x) {
    return x != null && x != 0;
}

Then maybe put all these variables in a List<Integer> ... it depends on how the rest of your code is structured ...

Upvotes: 5

Related Questions