ecoCode icon indicating copy to clipboard operation
ecoCode copied to clipboard

[EC75] [Python] Concatenate Strings in loop can be applied to Python as well

Open LordPatate opened this issue 1 year ago • 6 comments

(TO BE MEASURED) The Rule EC75 about String concatenation in a loop is said "Not applicable" for Python, but there actually is a way to generate strings that might be a better manner than concatenation in a loop: creating a list of string using a list comprehension then calling the join() built-in.

Non-compliant code example:

s = ""
for fruit in fruits:
  s += fruit.name

Compliant code example:

l = [fruit.name for fruit in fruits]
"".join(l)

LordPatate avatar Apr 05 '23 10:04 LordPatate

Hi @LordPatate, thank you for your issue and the new idea ! We aren't Python experts, and your contribution is a chance for us !

If we see the error message of EC75 rule, we can observe that the rule is specific for Java because we talk about StringBuilder. Any way, the idea of this rule, can maybe applied to other languages. If yes, we will must change the error message.

By the other hand, this rule is really good for java because the usage of StringBuilder avoid construction of many String objects in memory, thus environment impact Could you tell us how your idea can improve environment impact, with CPU or memory improvements ?

TO DISCUSS INSIDE CORE-TEAM : opinion about this new idea ?

dedece35 avatar Sep 22 '23 11:09 dedece35

Hi. I'm thinking that using generator expression are generally more efficient than traditional "for" loops in Python and that Python built-ins like the join function are often implemented directly in C when using the CPython interpreter, making it more efficient than equivalent Python code. Those are assumptions that needs to be verified through measurements.

LordPatate avatar Sep 22 '23 15:09 LordPatate

The benefits would likely be seen in term of CPU consumption

LordPatate avatar Sep 22 '23 15:09 LordPatate

@LordPatate, thank you for explanation ... do you have some references documentation or studies about this way of implementation and CPU consumption ? (to reference them in our documentation for future explanations)

dedece35 avatar Oct 06 '23 08:10 dedece35

I don't have any references sadly. It's just my intuition, which is why I said it has to be measured

LordPatate avatar Oct 06 '23 08:10 LordPatate

We are currently working on this subject (with Societe Generale company) Rules related to this subject should be committed soon.

(seen with @dedece35 and @jhertout )

glalloue avatar Oct 06 '23 12:10 glalloue