I'm writing classes that "must be used in a specific way" (I guess all classes must...).
For example, I create the fooManager
class, which requires a call to, say, Initialize(string,string)
. And, to push the example a little further, the class would be useless if we don't listen to its ThisHappened
action.
My point is, the class I'm writing requires method calls. But it will compile just fine if you don't call those methods and will end up with an empty new FooManager. At some point, it will either not work, or maybe crash, depending on the class and what it does. The programmer that implements my class would obviously look inside it and realize "Oh, I didn't call Initialize!", and it'd be fine.
But I don't like that. What I would ideally want is the code to NOT compile if the method wasn't called; I'm guessing that's simply not possible. Or something that would immediately be visible and clear.
I find myself troubled by the current approach I have here, which is the following:
Add a private Boolean value in the class, and check everywhere necessary if the class is initialized ; if not, I will throw an exception saying "The class wasn't initialized, are you sure you're calling .Initialize(string,string)
?".
I'm kind of okay with that approach, but it leads to a lot of code that is compiled and, in the end, not necessary to the end user.
Also, it's sometimes even more code when there are more methods than just an Initiliaze
to call. I try to keep my classes with not too many public methods/actions, but that's not solving the problem, just keeping it reasonable.
What I'm looking for here is:
- Is my approach correct?
- Is there a better one?
- What do you guys do/advise?
- Am I trying to solve a non-issue? I've been told by colleagues it's the programmer's to check the class before trying to use it. I respectfully disagree, but that's another matter I believe.
Put it simply, I'm trying to figure out a way to never forget to implement calls when that class is reused later, or by someone else.
CLARIFICATIONS :
To clarify many questions here :
I'm definitely NOT only talking about the Initialisation part of a class, but rather it's whole lifetime. Prevent colleagues to call a method twice, making sure they call X before Y, etc. Anything that would end up being mandatory and in documentation, but that I would like in code and as simple and small as possible. I really liked the idea of Asserts, though I'm quite sure I'll need to mix some other ideas as Asserts will not always be possible.
I'm using the C# language ! How did I not mention that?! I'm in a Xamarin environment and building mobile apps usually using about 6 to 9 projects in a solution, including PCL's, iOS, Android and Windows projects. I've been a developer for about a year and a half (school and work combined), hence my sometimes ridiculous statements\questions. All that is probably irrelevant here, but too much information isn't always a bad thing.
I can't always put everything that is mandatory in the constructor, due to platform restrictions and the use of Dependency Injection, having parameters other than Interfaces is off the table. Or maybe my knowledge is not sufficient, which is highly possible. Most of the time it's not an Initialisation issue, but more
how can I make sure he registered to that event ?
how can I make sure he didn't forget to "stop the process at some point"
Here I remember an Ad fetching class. As long as the view where the Ad is visible is visible, the class would fetch a new ad every minute. That class needs a view when constructed where it can display the Ad, that could go in a parameter obviously. But once the view is gone, StopFetching() must be called. Otherwise the class would keep fetching ads for a view that isn't even there, and that's bad.
Also, that class has events that must bé listened to, like "AdClicked" for example. Everything works fine if not listened to, but we lose tracking of analytics there if taps aren't registered. The Ad still works though, so the user and developer won't see a difference, and analytics will just have wrong data. That needs to be, avoided, but I'm not sure how developer can know they must register to the tao event. That is a simplified example though, but the idea is there, "make sure he uses the public Action that is available" and at the right times of course!
initialize
be made late after the object creation ? Would the ctor be too "risky" in the sense it could throw exceptions and break the creation chain ?new
if the object isn't ready to be used. Relying on an initialization method is bound to come back to haunt you and should be avoided unless absolutely necessary, at least that's my experience.