I'm not sure if I'm overthinking this but should I do this
if (!searchList.isEmpty()) {
String search = searchList.get(0).getText();
return List.of(search.split("\n"));
} else {
return null;
}
or should I do this
if (!searchList.isEmpty()) {
String search = searchList.get(0).getText();
return List.of(search.split("\n"));
}
return null;
CodePudding user response:
Neither.
if (searchList.isEmpty()) {
return null;
}
String search = searchList.get(0).getText();
return List.of(search.split("\n"));
I didn't like the negative condition ("if not empty") when it wasn't really needed.
The way I think of what I wrote, it's "get rid of the edge case first, then deal with the main logic".
This is of course mere opinion. Eventually you'll develop your own taste for how to lay out code; and good taste is one of the more important attributes of a programmer.
CodePudding user response:
Some advice :
- You should never return a
null
value. It is a bad practice - You should test for
true
instead offalse
. It makes your code more readeable
Your should look like this :
if (searchList.isEmpty()) {
return Collections.emptyList();
}
String search = searchList.get(0).getText();
return List.of(search.split("\n"));