Credence.Rule.NoStringConcatInLoop
(credence v0.2.0)
Copy Markdown
Performance rule: Detects string concatenation with <> inside looping
constructs (Enum.reduce, Enum.reduce_while, for comprehensions) or
recursive functions.
Each <> concatenation copies the entire accumulated binary, making
character-by-character string building O(n²). This is the string equivalent
of list ++ [element].
Bad
Enum.reduce(graphemes, "", fn char, acc ->
acc <> char
end)
Enum.reduce_while(chars, "", fn char, prefix ->
candidate = prefix <> char
...
end)Good
graphemes
|> Enum.reduce([], fn char, acc -> [char | acc] end)
|> Enum.reverse()
|> IO.iodata_to_binary()
# Or simply:
Enum.join(graphemes)