简体   繁体   English

我如何使这段代码更好

[英]How I can make this code better

I have fields like 我有像

variable1,variable2,variable3....etc 变量1,变量2,变量3 ...等

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. 像这样,我有将近15个条件。 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. 它们与我发布的内容不同。

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 ... 然后也许将所有这些变量放在List<Integer> ...这取决于其余代码的结构方式...

you can create a method isNotNullorequalsZero which checks not null and not equals zero 您可以创建一个isNotNullorequalsZero方法,该方法检查不为null且不等于零

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);

您可以将所有变量放入数组中,并使用for循环检查条件并在myobject.setFirstValue(array[index]);设置值myobject.setFirstValue(array[index]);

either modify setFirstValue method as 要么将setFirstValue方法修改为

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;
}

You could make a method called something like copyIfNonZero , and it would probably look like this: 您可以创建一个名为copyIfNonZero类的方法,它可能看起来像这样:

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并对其进行迭代:

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

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 : 在工具或utils类中,创建一个静态方法:

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. 此代码假定您的目标对象(myObject)遵守Bean约定(基本上是有名的getter和setter命名),并且值始终是Integer。

Then use it like this : 然后像这样使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM