4: Introduction to Code Complexity#
What matters when you write code for a computer? Take a moment to come up with a few characteristics of code that are important. Can you settle on a most important characteristic?
If you said correctness, then good job.
Code that doesn’t work, that doesn’t solve the problem, that has errors in it, should not be used. After correctness, other properties of good code include readability and efficiency. I always waffle back and forth on which of these, readability or efficiency, is more important.
Readability is critical in software development since we need our people to read and modify our code. We need to make code that has been written by different people working together.
For most applications – non-real-time applications – efficiency may not seem as important because our computers run fast and have lots of memory. That is probably true, but when do you remember being happy about waiting while a computer calculates something? For some applications, particularly real-time applications such as airplane controllers, x-ray controllers, and firefighter helmets, efficiency is a part of correctness, because the application requires a fast run-time or needs to use only a small amount of memory, otherwise human life may be in danger. As a computer programmer, you must understand efficiency. Since so many libraries exist, and a common strategy is to import and use them, it is even more critical to understand issues that could arise in these libraries. An example is that you may not want to use an ArrayList
to store x-ray data if there is any chance that list could fill up while the machine is on. When the ArrayList
needs to grab extra memory, there is a substantial delay in program execution, and if the x-ray machine is on…let’s just say you must understand what efficiency is.
In this chapter, we concentrate on run-time efficiency and not on space efficiency. Space efficiencies will be important in our discussion on linked and array lists, coming up in future chapters.