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

Redundant "null" checks should not be made

    XMLWordPrintable

    Details

    • Type: Finding Detection
    • Status: Closed
    • Resolution: Won't Fix
    • Labels:
    • Message:
      Remove this redundant "null" check
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Targeted languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      20min
    • Implementation details:

      Description

      Once a value is known to be null or non-null, there's no reason to re-check it unless it has been changed (or potentially changed) in the interim. Doing so anyway may may just be an over-abundance of caution, but it could indicate a bug.

      Noncompliant Code Example

      public void assignCoach(Team team, Person person) {
      
        if (team.hasCoach()) {  // team is dereferenced
          return;
        }
      
        if (team != null) {  // Noncompliant; if we got this far, team is not null
          //...
        }
      
        if (person != null) {
          // ...
          if (disqualified) {
            person = getAlternate();
          }
        }
      
        if (person != null) { // Compliant; person may have changed since last check
          team.setCoach(person);
        }
      
        if (person != null) {  // Noncompliant; no changes to person since last check
          // ...
       
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ann.campbell.2 Ann Campbell
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: