The More Objects, the Better?
◀ Programming Misconceptions▶ The More Functions, the Better? Amazon
If there are two ways that serve the same purpose and one uses one object and the other uses ten, which way would you prefer? Let’s not go into memory management and other technical issues and focus on the efficiency of using one object versus many objects.
For example, suppose we need to keep track of however many points on a 2-D space a user enters. He may enter 10, 100, or even 1000 as the number of points. Programmer A writes a class that represents a point and declares as many objects as the user enters.
Programmer B writes a class that encapsulates all points and declares only one object that stores all points.
Programmer B could obtain many advantages over programmer A such as neater organization of code, smaller code size, easier manipulation of data, higher efficiency, and less confusion.
That said, how does programmer B manage to use only one object to store everything? He can simply use pointers as instance variables of the class and allocate space for them dynamically via
new or
malloc().
Experienced programmers should have no problem seeing that a large number of objects can be confusing and awkward and the manipulation of which presents pitfalls for bugs and errors. Therefore be modest in the number of objects you create in a program!
◀ Programming Misconceptions▶ The More Functions, the Better?