
Episode 20 · Season 14
Constructors And Initializers In Object-Oriented Design
About this episode
When we create an instance of a class, we often need to do some initialization. Constructors and initializers achieve this goal. These are two different ways to get our data into an instance and are an essential part of our design. In this episode, we examine the design decisions we need to make as we decide how to use these common methods.
Constructors And Initializers - The Distinction
A constructor and an initializer method can be thought of in very similar ways. However, there is one particular trait that differs between the two approaches. A constructor returns an instance of a class, whereas an initializer sets values on an instance. This may seem to be a minor difference. It is not. There are situations where we need to create many instances of a class, and a "fat" constructor can lead to performance issues. The creation of an instance is an important operation. Thus, it is best to complete it in as short a time as possible.
That is where an initializer comes in. We can create an instance quickly and then take our time populating that instance (i.e., initializing it).
A No-Args Constructor
Many languages include a default constructor for classes that takes zero arguments. This provides a quick way to get an instance. I personally find these important and highly useful in extending classes. The exception to providing a default constructor is when you need to provide limited instances or some level of instance management.
Adding Parameters
Once you start adding parameters to your instance creation, you should consider an initializer. That provides a cleaner way to create an instance and then populate it. You also give the developer the ability to "lazy load" data, for that instance. That is an important option in large systems and large classes. A good initialization routine also provides an opportunity for a mirroring cleanup method that makes it easy for a developer to control resource usage within classes.
A few core parameters for an instance may be convenient, but once that list gets long, it can slow the process. It is better first to get a valid instance and then do something with it.
More from Develpreneur: Become a Better Developer and Entrepreneur
A handful of people love your MVP. Their suggestions keep coming, and AI helps you turn those requests into working features quickly. The product is growing. Whether it is growing in the right directi…
Your team can generate an interface faster than ever. Meanwhile, customers are still staring at confusing error messages, waiting for pages to load, and trying to figure out what happens after they cl…
After a year of deadlines, long hours, and projects that seemed determined to last forever, Thanksgiving offers a chance to pause and enjoy what all that effort has made possible. In this Thanksgiving…
The holiday is almost here, but your brain is still at work. There is something to finish, something to check, and probably something you could squeeze in before everyone arrives. Closing the laptop s…
Building a successful business requires more than developing a great product. As companies grow, they face new challenges involving customer expectations, operational efficiency, financial decisions,…