My preference is one of these:
private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
if ( (kreditJeAktivanKod != null) && kreditJeAktivanKod.equals("Y") ) {
return VrstaProizvoda.STEP.value();
} else if ( (idArmPlana != null) && !idArmPlana.isEmpty() && !idArmPlana.equals("0000") ) {
return VrstaProizvoda.ARM.value();
} else {
return null;
}
}
private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
if ( strEquals(kreditJeAktivanKod, "Y") ) {
return VrstaProizvoda.STEP.value();
} else if ( !strIsEmpty(idArmPlana) && !strEquals(idArmPlana, "0000") ) {
return VrstaProizvoda.ARM.value();
} else {
return null;
}
}
Here are a number of rewrites to show a range of alternatives, and to show how to reach the above with incremental adjustments:
Rewritten with more spaces and parenthesis. This makes it easier to pick out the long variable names, and relieves the reader of all need to organize the expression logic:
private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
String vrstaProizvoda = null;
if ( (kreditJeAktivanKod != null) && kreditJeAktivanKod.equals("Y") ) {
vrstaProizvoda = VrstaProizvoda.STEP.value();
} else if ( (idArmPlana != null) && !idArmPlana.isEmpty() && !idArmPlana.equals("0000") ) {
vrstaProizvoda = VrstaProizvoda.ARM.value();
}
return vrstaProizvoda;
}
Rewritten to remove the default 'null' value. Having such a value is problematic. Consider if the logic were much more complex. Having a default value takes away the opportunity for the compiler to detect unhandled cases.
private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
String vrstaProizvoda;
if ( (kreditJeAktivanKod != null) && kreditJeAktivanKod.equals("Y") ) {
vrstaProizvoda = VrstaProizvoda.STEP.value();
} else if ( (idArmPlana != null) && !idArmPlana.isEmpty() && !idArmPlana.equals("0000") ) {
vrstaProizvoda = VrstaProizvoda.ARM.value();
} else {
vrstaProizvoda = null;
}
return vrstaProizvoda;
}
Rewritten with multiple return values. This is my preference, but some prefer a single return statement, as was present in the original method.
private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
if ( (kreditJeAktivanKod != null) && kreditJeAktivanKod.equals("Y") ) {
return VrstaProizvoda.STEP.value();
} else if ( (idArmPlana != null) && !idArmPlana.isEmpty() && !idArmPlana.equals("0000") ) {
return VrstaProizvoda.ARM.value();
} else {
return null;
}
}
Rewritten, with helper methods (see below). This is a little clearer, but at a cost of obscuring the test logic. Splitting code into a lot of small methods, while often encouraged, is not always preferred in practice.
private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
if ( strEquals(kreditJeAktivanKod, "Y") ) {
return VrstaProizvoda.STEP.value();
} else if ( !strIsEmpty(idArmPlana) && !strEquals(idArmPlana, "0000") ) {
return VrstaProizvoda.STEP.value();
} else {
return null;
}
}
Helper methods:
// Test that two strings are equal. Handle null values.
private boolean strEquals(String value1, String value2) {
if ( value1 == null ) {
return ( value2 == null );
} else if ( value2 == null ) {
return false;
} else {
return value1.equals(value2);
}
}
// Test that two strings are equal. Handle null values.
private boolean strEquals(String value1, String value2) {
boolean result;
if ( value1 == null ) {
result = ( value2 == null );
} else if ( value2 == null ) {
result = false;
} else {
result = value1.equals(value2);
}
return result;
}
// Test if a string is neither null nor empty.
private boolean strIsNotEmpty(String value) {
return ( (value != null) && !value.isEmpty() );
}