user1022677 user1022677 - 1 month ago 3x
C# Question

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:

, et cetera.

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
method instead? This allows the programmer to determine when he wants to load the audio files but then if he forgets to call
the program will crash.

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

Which approach is preferable in terms of general design principles?


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?