This part of code is rejected by pmd in sonar:
public String getFoo() {
String foo = System.getProperty("foo");
if (foo == null) {
foo = System.getenv("foo");
} else if (foo == null) {
foo = "defaultFoo";
}
return foo;
}
It says "Avoid Literals In If Condition". Can someone tell me what's wrong with this or what this rule try to effect?
In simple words, Literals in Java is a synthetic representation of boolean, numeric, character, or string data. It is a medium of expressing particular values in the program, such as an integer variable named ''/count is assigned an integer value in the following statement. // Here 100 is a constant/literal.
Why don't you use:
public String getFoo() {
String foo = System.getProperty("foo", "defaultFoo");
return foo;
}
It will return "defaultFoo"
if no property is found.
http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperty(java.lang.String, java.lang.String)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With