Complexity
Contrary to what one might think at first, a good program will usually be less complex than a bad program. Many people think that the best programmer is the one who writes the most difficult and counter-intuitive code.
In fact, the good programmer is the one who solves complex problems as simply as possible. This is because when the developer is more experienced, he learns to solve the problem directly, without unnecessary "loops".
Another maturity factor of a developer is the question of first solving the problem, then implementing the solution. The younger ones usually go out programming without really knowing where they’re going. The code ends up "working", but with gambiarras and completely unnecessary parts.
Reading suggestion: Good programmers write complex code?
Of course some algorithms are inherently complex, but the truth is that in general we never get an optimal solution
There is a whole theory about the complexity of algorithms, but what in general affects even the complexity is the amount of loops and comparisons performed.
Memory vs. Performance
Memory and performance are usually not proportional.
A good program can use more memory to cache and fine optimizations, a more elaborate structure of classes and more. A bad program can do all operations on disk or directly in the database and contain fewer classes, but without separation of responsibilities.
Therefore, I think it is better to analyze these items separately...
Memoriam
Except in the cases I mentioned, where we purposely use more memory, a well-made program will occupy less memory when the developer correctly uses the data structures and language Apis.
In languages they have Garbage Collector it is essential to pay attention to memory Leaks in static attributes, such as maps that never have their items removed.
Another problem is reading too much data in memory without need. Some people read database and entire file in memory to then process the information, and you could do this record by record or line by line.
Performance
Performance can also be greatly affected by how a system is implemented.
This can start with how the database is modeled, how the system reads and writes data, and implemented algorithms.
Reading and writing in a database should be carefully thought out, avoiding select *
and make updates mass when possible instead of updating records one by one.
For files it is interesting to use streams for reading and writing, unlike the common practice of always loading everything to a String in memory.
Performance, memory and complexity
Decreasing the complexity of a program can help decrease memory usage and improve performance.
The amount of loops and comparisons can be reduced using appropriate data structures. For example, in many situations we can use a *hash* based map to retrieve key-based elements instead of using indexOf
in lists and vectors. In the best case, a map allows retrieval of an item without iterations, in a single step, while in the lists it may be necessary to scan all elements to find an object inside.
You can also save memory by avoiding the use of unnecessary variables and data structures. Or even avoid generating values in memory unnecessarily, using StringBuffer
s or equivalents instead of concatenation of strings, for example.
Readable code and optimizations
In general, a programmer tries to write simple and readable code, after all maintenance is a very important factor in most systems.
However, in specific cases, it is possible that it "breaks" certain rules by prioritizing some other quality issue: performance, availability, efficient use of memory or some other resource.
An example of this is in "real time" systems, where it is necessary to extract the maximum performance at the cost of having a code very difficult to read.
Or when it is decided to use UDP socket communication with a proprietary protocol instead of a REST web service due to data volume.
Considerations
Anyway, all this was to say that, yes, complex code usually impairs performance and memory usage.
I tried on the above topics to consider some fairly generic points, but which are relevant to the discussion.
Guy excellent explanation, at first I wonder only if the complexity of the code could affect the performance and use of ram server memory, you went further and gave an excellent response. I have been using clean code practice, always reducing the complexity of a year here, but I have this project already 3 years, which was created when I did not know about code complexity. Thank you.
– Erico Souza