Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-1612

Lambdas should be replaced with method references

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Replace this lambda with a method reference. [(sonar.java.source not set. Assuming 8 or greater.)]
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      Method/constructor references are commonly agreed to be, most of the time, more compact and readable than using lambdas, and are therefore preferred.
      In some rare cases, when it is not clear from the context what kind of function is being described and reference would not increase the clarity, it might be fine to keep the lambda.

      Similarly, null checks can be replaced with references to the Objects::isNull and Objects::nonNull methods, casts can be replaced with SomeClass.class::cast and instanceof can be replaced with SomeClass.class::isInstance.

      Note that this rule is automatically disabled when the project's sonar.java.source is lower than 8.

      Noncompliant Code Example

      class A {
        void process(List<A> list) {
          list.stream()
            .filter(a -> a instanceof B)
            .map(a -> (B) a)
            .map(b -> b.<String>getObject())
            .forEach(b -> { System.out.println(b); });
        }
      }
      
      class B extends A {
        <T> T getObject() {
          return null;
        }
      }
      

      Compliant Solution

      class A {
        void process(List<A> list) {
          list.stream()
            .filter(B.class::isInstance)
            .map(B.class::cast)
            .map(B::<String>getObject)
            .forEach(System.out::println);
        }
      }
      
      class B extends A {
        <T> T getObject() {
          return null;
        }
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              freddy.mallet Freddy Mallet (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: