Code Smell - Low-Level Addition
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
- Remove loops
- Simplify logic
- 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
Right
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!
Remember AI Assistants make lots of mistakes
Without proper instructions
ChatGPT Claude Perplexity Gemini
With specific instructions
ChatGPT Claude Perplexity 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.
Relations

Code Smell 53 - Explicit Iteration
Maxi Contieri ・ Jan 5 '21

Code Smell 123 - Mixed 'What' and 'How'
Maxi Contieri ・ Mar 22 '22
Disclaimer
Code Smells are my opinion.
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

Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.

How to Find the Stinky parts of your Code
Maxi Contieri ・ May 21 '21
The above is the detailed content of Code Smell - Low-Level Addition. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
