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

Property names should not match get methods

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Change either the name of the property "XXX" or the name of the method "GetXXX" to make them distinguishable.
    • Highlighting:
      Hide

      Primary: Property declaration
      Secondary: Getxxx method

      Show
      Primary: Property declaration Secondary: Getxxx method
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • 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:
      2min
    • Analysis Scope:
      Main Sources, Test Sources
    • FxCop:
      PropertyNamesShouldNotMatchGetMethods, CA1721

      Description

      Properties and Get method should have names that makes them clearly distinguishable.
      This rule raises an issue when the name of a public or protected member starts with 'Get' and otherwise matches the name of a public or protected property.

      Noncompliant Code Example

      using System;
      
      namespace MyLibrary
      {
          public class Foo
          {
              public DateTime Date
              {
                  get { return DateTime.Today; }
              }
      
              public string GetDate() // Noncompliant
              {
                  return this.Date.ToString();
              }
          }
      }
      

      Compliant Solution

      using System;
      
      namespace MyLibrary
      {
          public class Foo
          {
              public DateTime Date
              {
                  get { return DateTime.Today; }
              }
      
              public string GetDateAsString()
              {
                  return this.Date.ToString();
              }
          }
      }
      

        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: