When to Use Arrays

Many people have a tendency to use the most complicated tools available to implement a solution. This is a bad tendency that stems largely from a desire to look or feel smart. Ironically, when we do this we really make ourselves look foolish, at least in the eyes of those who are wise enough to know better.

More sophisticated solutions cost more. They are harder to implement, consume more resources, are more prone to design and implementation errors, and more prone to failure.

As a simple example, consider the problem of reading a list and printing it back in forward order. This can be done with an array, but should it? Drawbacks of using an array:

In summary, the array version is bigger, slower, harder to write, and places more load on the computer running it. A programmer who uses an array to solve this problem is demonstrating poor judgment and a lack of understanding of the specifications. Good programmers always look for the simplest, most elegant solutions that minimize resource requirements.

The same argument can be made for a case where you need to add two vectors or matrices contained in files. We could inhale them into arrays, add corresponding elements, storing the results in a sum array, and finally dump the sum array to an output file. But this would be a foolish approach. We only need 3 scalar variables for this task. We can read one element at a time from each source file, add them, and immediately write the result to the sum file.

Whenever designing a solution to a problem, ask yourself if it is really necessary to retain large amounts of information. Retain only as much as necessary to make the solution correct and efficient.