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

Extensions should be in separate namespaces

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Either move this extension to another namespace or move the method inside the class itself.
    • Highlighting:
      Hide

      Extension declaration

      Show
      Extension declaration
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Covered Languages:
      C#
    • Irrelevant for Languages:
      ABAP, C, C++, Cobol, CSS, Flex, HTML, Java, JavaScript, Objective-C, PHP, PL/I, PL/SQL, Python, RPG, Swift, T-SQL, TypeScript, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      It makes little sense to create an extension method when it is possible to just add that method to the class itself.
      This rule raises an issue when an extension is declared in the same namespace as the class it is extending.

      Noncompliant Code Example

      namespace MyLibrary
      {
          public class Foo
          {
              // ...
          }
      
          public static class MyExtensions
          {
              public static void Bar(this Foo a) // Noncompliant
              {
                  // ...
              }
          }
      }
      

      Compliant Solution

      Using separate namespace:

      namespace MyLibrary
      {
          public class Foo
          {
              // ...
          }
      }
      
      namespace Helpers
      {
          public static class MyExtensions
          {
              public static void Bar(this Foo a)
              {
                  // ...
              }
          }
      }
      

      Merging the method in the class:

      namespace MyLibrary
      {
          public class Foo
          {
              // ...
              public void Bar()
              {
                  // ...
              }
          }
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              jeanchristophe.collet Jean-Christophe Collet (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: