CodeQL queries 1.25
Skip to end of metadata
Go to start of metadata

Name: Self containedness of files

Description: Files that do not include source code for most of the types that they depend on are difficult to port to new platforms.

ID: cs/source-dependency-ratio-per-file

Kind: metric

Query: FSelfContainedness.ql
/**
 * @name Self containedness of files
 * @description Files that do not include source code for most of the types that they depend on are difficult to port to new platforms.
 * @kind treemap
 * @treemap.warnOn lowValues
 * @metricType file
 * @metricAggregate avg max
 * @tags portability
 *       modularity
 * @id cs/source-dependency-ratio-per-file
 */

import csharp
import semmle.code.csharp.metrics.Coupling

// Self-containedness on file level
from File f, float selfContaindness, int efferentSourceCoupling, int efferentCoupling
where
  efferentSourceCoupling =
    count(File g |
      exists(RefType c |
        c.fromSource() and
        c.getFile() = g and
        exists(RefType d | d.fromSource() and d.getFile() = f and depends(d, c))
      )
    ) and
  efferentCoupling =
    count(File g |
      exists(RefType c |
        c.getFile() = g and
        exists(RefType d | d.fromSource() and d.getFile() = f and depends(d, c))
      )
    ) and
  if efferentCoupling = 0
  then selfContaindness = 100
  else selfContaindness = 100 * efferentSourceCoupling.(float) / efferentCoupling
select f, selfContaindness order by selfContaindness desc

This metric measures the percentage of types on which the file depends for which the build process built from source. The availability of source code is one of the key factors that affects how easy a project will be to build for different versions of the .NET framework. Files with low self-containedness are also more affected by changes to their dependencies.

Recommendation

Depending on your project, self-containedness may or may not be an issue for you. If you decide that it should be addressed then there are a few things you can do to easily increase self-containedness. One way of increasing self-containedness is by creating wrappers for any external classes. If the external class is changed then only your wrapper needs to be updated. You should also try to use libraries with source code available.

References