Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Class Coupling jump when I use LINQ?

I have a method that takes three parameters: List<Class1> source, List<Class2) results, and DateTime endDate. I can see why the class coupling is four. However, it jumps to ten when I add this statement:

var warnings = from s in source
    join r in results on s.Field1 equals r.Field1 into joined
    from j in joined.DefaultIfEmpty( )
    where j == null
    select string.Format( "{0}{1}", A_CONSTANT, s.Field2 );

My questions:

  1. What are the six new classes that were introduced by the LINQ statement?
  2. And since ten is the upper limit of "good code," does this indicate that LINQ is not a good choice here?
like image 298
Kelly Cline Avatar asked Apr 16 '15 14:04

Kelly Cline


1 Answers

The six additional classes are possibly:

  • IEnumerable<string> - the result of your query
  • IEnumerable<Class1> - the left collection
  • IEnumerable<Class2> - the right collection
  • Func<Class1, int> - the left part of the join expression
  • Func<Class2, int> - the right part of the join expression
  • Func<Class1, Class2, string> - the projection

It's also possible that it's counting the Enumerable class since the query is translated to static extension method calls.

In either case, the code analysis does not seem to ignore transient classes used by Linq (whether it should or not is debatable). My advice is to either ignore it (perhaps manually counting the coupling and noting the difference) or finding a better analysis tool.

Another question would be: does it increase your coupling overall? I suspect that several of these classes are used throughout your app, so it may not make a significant difference on your overall coupling.

like image 106
D Stanley Avatar answered Sep 28 '22 09:09

D Stanley