rspec/rules/S1604/java/rule.adoc

42 lines
1.0 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
Before Java 8, the only way to partially support closures in Java was by using anonymous inner classes. But the syntax of anonymous classes may seem unwieldy and unclear.
With Java 8, most uses of anonymous inner classes should be replaced by lambdas to highly increase the readability of the source code.
*Note* that this rule is automatically disabled when the project's ``++sonar.java.source++`` is lower than ``++8++``.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
myCollection.stream().map(new Mapper<String,String>() {
public String map(String input) {
return new StringBuilder(input).reverse().toString();
}
});
Predicate<String> isEmpty = new Predicate<String> {
boolean test(String myString) {
return myString.isEmpty();
}
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
myCollection.stream().map(input -> new StringBuilder(input).reverse().toString());
Predicate<String> isEmpty = myString -> myString.isEmpty();
----
ifdef::rspecator-view[]
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::rspecator-view[]