Don't care how you do things. Care about what you do
TL;DR: Ditch the Loops: Write Cleaner Code with Declarative Style
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.
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)
[X] Semi-Automatic
You can detect this smell when you see explicit loops accumulating a result, especially in simple operations like summing values.
[X] Beginner
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 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.
Remember AI Assistants make lots of mistakes
ChatGPT Claude Perplexity Gemini
ChatGPT Claude Perplexity Gemini
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.
Code Smells are my opinion.
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
This article is part of the CodeSmell Series.
Atas ialah kandungan terperinci Bau Kod - Penambahan Aras Rendah. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!