Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an Initialize method a code smell?

I'm coding a bunch of systems right now. They do not derive from a common interface.

Some example systems: MusicSystem, PhysicsSystem, InputSystem, et cetera.

Currently, MusicSystem loads a lot of audio files in its constructor and as a result, there may be some brief lag when the object is first created.

Because of this, should this code loading all the audio files be placed in an Initialize() method instead? This allows the programmer to determine when he wants to load the audio files but then if he forgets to call Initialize() the program will crash.

Because not all systems need an Initialize() method the programmer has to look through every system to see if the class has an Initialize() method and if so, invoke it. This is a bit cumbersome.

Which approach is preferable in terms of general design principles?

like image 390
user1022677 Avatar asked Oct 31 '11 20:10

user1022677


People also ask

What is initialize method?

The initialize method is useful when we want to initialize some class variables at the time of object creation. The initialize method is part of the object-creation process in Ruby and it allows us to set the initial values for an object. Below are some points about Initialize : We can define default argument.

What does Init method do in Java?

Init method is a predefined method to initialize an object after its creation. Init method is a life cycle method for servlets for java. It is started by the browser when java program is loaded and run by the browser. Init method is a predefine method to initialize an object after its creation.

How do you call an init method in Java?

suppose you just create a new MainWindow object in main method and call the init() method. Show activity on this post. You can also run the function by making it static. Like python "static method".


2 Answers

Think about other APIs which you have written code against. When was the last time that an API required the programmer to know to call an init method, otherwise crashing at runtime?

As a consumer of your API, it would drive me nuts if I had to know to call an init method after constructing an object. I would recommend an alternative that I have seen and used firsthand: document expensive object instantiation. What's the point of deferring an expensive initialization if it's required for the program to not crash?

like image 67
Matt Ball Avatar answered Sep 18 '22 14:09

Matt Ball


Moving heavy initialization out of the constructor isn't a code smell.

However, relying on an external caller to invoke that initialization is a smell - it's called Temporal Coupling.

Create the Initialize() method on your class, but make it private (or protected if you must).

Also write an EnsureInitialized() method that triggers initialization if required, but only once per instance.

Then, each public entry point of your class should call EnsureInitialized() at the start - you initialization gets deferred to the point of first use.

With this in place, and provided you're comfortable with locks, you can spin off a call to EnsureInitalized() into a background thread to do the work in the background, with minimal impact on the foreground.

like image 20
Bevan Avatar answered Sep 20 '22 14:09

Bevan