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

".length" should be used to test for the emptiness of StringBuffers

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Message:
      Test "xxx.length()" against 0 to see if "xxx" is empty.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • PMD:
      UseStringBufferLength

      Description

      Assembling a StringBuilder or StringBuffer into a String merely to see if it's empty is a waste of cycles. Instead, jump right to the heart of the matter and get its .length() instead.

      Noncompliant Code Example

      StringBuilder sb = new StringBuilder();
      // ...
      if ("".equals(sb.toString()) { // Noncompliant
        // ...
      }
      

      Compliant Solution

      StringBuilder sb = new StringBuilder();
      // ...
      if (sb.length() == 0) {
        // ...
      }
      

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated: