Version - 9.8 Community (build 63668)
Deployment mode - I’m not sure, I didn’t set it up, but it’s a daily code scanner that’s in the cloud and checks our development repo
My agency is using SonarQube to enforce coding standards for some enterprise software me and some other folks are developing. I’ve found that we continually get “please log or re-throw this exception” errors popping up every time that I use lambda syntax in Java, when the functionally identical equivalent using for loops or manually overriding interface functions does not trigger the issue. I wouldn’t think much of it, but Java 8 turns 10 years old next week, and this isn’t a new language feature. Here’s a code example of what I’m talking about:
//example
JButton button = new JButton("Click me");
//lambda syntax, more concise
button.addActionListener( ae -> {
try {
//doing things
} catch (Exception ex) {
Logger.log(ex); //SonarQube will yell at you for this.
}
});
//verbose, functionally identical syntax
button.addActionListener( new ActionListener () {
@Override
public void actionPerformed(ActionEvent ae) {
try {
//doing things
} catch (Exception ex) {
Logger.log(ex); //SonarQube accepts this, no fuss...but why do I have to type out the boilerplote to keep the Qube happy?
}
}
});
//example 2
//demonstrating two nearly identical loops, one of which SonarQube will yell at you for
//this example is contrived...this becomes much more bundensome when you're using
//the java.util.stream library and adding a .forEach at the end of some mapping
//transformation...in which case doing it with the traditional "for" syntax
// would require an intermediate step of collecting the stream into
// some kind of java.util.Collection<T> object...inefficient and unnecessarily verbose.
List<String> things = Arrays.asList("a","b","c","d");
//lambda syntax
things.forEach(thing -> {
try {
doAThingToThing(thing);
} catch (Exception ex) {
Logger.log(ex); //SonarQube doesn't like this
}
});
//traditional for loop, does the same thing
for (Thing thing : things) {
try {
doAThingToThing(thing);
} catch (Exception ex) {
Logger.log(ex); //SonarQube is fine with this.
}
}