Code Smell 269 – Low-Level Addition

When summing a collection, you might manually loop through the elements and add each one to a variable. This approach works but adds unnecessary lines of code and makes it harder to follow. Using language high-level functions, you can make your code shorter, clearer, and less error-prone.


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri

Don't care how you do things. Care about what you do

TL;DR: Ditch the Loops: Write Cleaner Code with Declarative Style

Problems

  • Verbose logic
  • Repeated patterns
  • Readability
  • Maintainability

Solutions

  1. Remove loops
  2. Simplify logic
  3. Write declarative and high-level code

Context

When summing a collection, you might manually loop through the elements and add each one to a variable.

This approach works but adds unnecessary lines of code and makes it harder to follow.

Using language high-level functions, you can make your code shorter, clearer, and less error-prone.

It tells you exactly what the code is doing and not how it is doing it.

Sample Code

Wrong

transaction_values = [10.0, -5.21, 101.32, 1.11, -0.38]
balance = 0
for transaction_value in transaction_values:
    balance += transaction_value
transactions_values = [10.0, -5.21, 101.32, 1.11, -0.38]
balance = sum(transactions_values)

Detection

  • [x] Semi-Automatic

You can detect this smell when you see explicit loops accumulating a result, especially in simple operations like summing values.

Tags

  • Declarative

Level

  • [x] Beginner

AI Generation

AI generators can sometimes produce this smell by writing verbose loops instead of using functions like sum().

If you don't specify the need for cleaner, declarative solutions, they might opt for more lines of code.

AI Detection

AI systems can easily detect and simplify this smell when you ask them to reduce code complexity with simple instructions to "optimize" or "simplify," most AI tools suggest using sum() in this case.

\ Try Them!

But Remember: AI Assistants make lots of mistakes

\

| Without Proper Instruction | With Specific Instructions | |----|----| | ChatGPT | ChatGPT | | Claude | Claude | | Perplexity | Perplexity | | Gemini | Gemini |

\

Conclusion

Favoring declarative functions like sum() improves readability and reduces potential errors.

You reduce the need for manual loops and make it easier to maintain. It shows exactly what the code is doing with minimal syntax and clutter.

Related reading

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xi-sit35t1

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxv

\

:::warning Disclaimer: Code Smells are my opinion.

:::

:::info Credits: Photo by Kati Hoehl on Unsplash

:::


The most important property of a program is whether it accomplishes the intention of its user.

C.A.R. Hoare

https://hackernoon.com/400-thought-provoking-software-engineering-quotes?embedable=true


:::tip This article is part of the CodeSmell Series on HackerNoon.

:::

\


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2024-09-12T12:39:29+00:00) Code Smell 269 – Low-Level Addition. Retrieved from https://www.scien.cx/2024/09/12/code-smell-269-low-level-addition-2/

MLA
" » Code Smell 269 – Low-Level Addition." Maximiliano Contieri | Sciencx - Thursday September 12, 2024, https://www.scien.cx/2024/09/12/code-smell-269-low-level-addition-2/
HARVARD
Maximiliano Contieri | Sciencx Thursday September 12, 2024 » Code Smell 269 – Low-Level Addition., viewed ,<https://www.scien.cx/2024/09/12/code-smell-269-low-level-addition-2/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Code Smell 269 – Low-Level Addition. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/12/code-smell-269-low-level-addition-2/
CHICAGO
" » Code Smell 269 – Low-Level Addition." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2024/09/12/code-smell-269-low-level-addition-2/
IEEE
" » Code Smell 269 – Low-Level Addition." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2024/09/12/code-smell-269-low-level-addition-2/. [Accessed: ]
rf:citation
» Code Smell 269 – Low-Level Addition | Maximiliano Contieri | Sciencx | https://www.scien.cx/2024/09/12/code-smell-269-low-level-addition-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.