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

Fields should not be initialized to default values

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Message:
      Remove this initialization to "x", the compiler will do that for you.
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Targeted languages:
      PHP
    • Covered Languages:
      C#, Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Scope:
      Main Sources, Test Sources
    • Checkstyle:
      ExplicitInitialization
    • ReSharper:
       RedundantDefaultFieldInitializer

      Description

      The compiler automatically initializes class fields to their default values before setting them with any initialization values, so there is no need to explicitly set a field to its default value. Further, under the logic that cleaner code is better code, it's considered poor style to do so.

      Noncompliant Code Example

      public class MyClass {
      
        int count = 0;  // Noncompliant
        // ...
      
      }
      

      Compliant Solution

      public class MyClass {
      
        int count;
        // ...
      
      }
      

      Exceptions

      final fields are ignored.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              alexandre.gigleux Alexandre Gigleux
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated: