4

When running a SonarQube scan all case class and object scala files are flagged with a weird issue: Method com.org.package.ExampleCaseClass$.<static initializer for >() uses a Side Effect Constructor

If I convert it to a normal class the issue goes away oddly enough. What is it that is different for the case classes/objects?

Decompiling the Scala into Java shows that there does exist a static block:

   public static final ExampleCaseClass$ MODULE$;

   static {
      new ExampleCaseClass$();
   }

Example case class with the issue:


case class ExampleCaseClass(var1: String,
                       var2: String,
                       var3: String,
                       var4: String,
                       var5: String,
                       var6: String,
                       var7: String,
                       var8: String)

Sonar expands on the issue saying:

  • This method creates an object but does not assign this object to any variable or field. This implies that the class operates through side effects in the constructor, which is a bad pattern to use, as it adds unnecessary coupling. Consider pulling the side effect out of the constructor, into a separate method, or into the calling method.

However, considering this class just takes strings in the constructor I'm not sure what the side effect could be. Is this a false positive?

1 Answers1

1

Eventually it was decided to mark the Sonar issue as a false positive and move on about our lives.