Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1437

Remove redundant if statements in StringUtils.join

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.6
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      In the various join methods in StringUtils, the for-loop contains an if (i > startIndex) check (for an example see here, but it's present in all the join methods that take an array).
      That is unnecessary - for the first iteration, we know the condition is false, while for the other iterations, we know the condition is true. So instead, we can do this:

      • append the first element (at position startIndex)
      • for every other element (starting at position startIndex + 1), append the separator, then the element.

      It's basically the same tactic used in the Iterable version.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                aft90 Andrei Filip Troie
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 40m
                  40m