You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Andrei Filip Troie (JIRA)" <ji...@apache.org> on 2019/03/04 21:18:00 UTC
[jira] [Updated] (LANG-1437) Remove redundant if statements in
StringUtils.join
[ https://issues.apache.org/jira/browse/LANG-1437?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Andrei Filip Troie updated LANG-1437:
-------------------------------------
Description:
In the various {{join}} methods in {{StringUtils}}, the for-loop contains an {{if (i > startIndex)}} check (for an example see [here|https://github.com/apache/commons-lang/blob/eb5b11a25c9e61f9b25a540682816ebb103b735c/src/main/java/org/apache/commons/lang3/StringUtils.java#L4203], 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.
was:
In the various {{join}} methods in {{StringUtils}}, the for-loop contains an {{if (i > startIndex)}} (for an example see [here|https://github.com/apache/commons-lang/blob/eb5b11a25c9e61f9b25a540682816ebb103b735c/src/main/java/org/apache/commons/lang3/StringUtils.java#L4203], 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.
> Remove redundant if statements in StringUtils.join
> --------------------------------------------------
>
> Key: LANG-1437
> URL: https://issues.apache.org/jira/browse/LANG-1437
> Project: Commons Lang
> Issue Type: Improvement
> Reporter: Andrei Filip Troie
> Priority: Minor
> Time Spent: 10m
> Remaining Estimate: 0h
>
> In the various {{join}} methods in {{StringUtils}}, the for-loop contains an {{if (i > startIndex)}} check (for an example see [here|https://github.com/apache/commons-lang/blob/eb5b11a25c9e61f9b25a540682816ebb103b735c/src/main/java/org/apache/commons/lang3/StringUtils.java#L4203], 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.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)