I have the following class
export class Agent {
constructor(modelName: string) {
this.shape = app.gallery.getModel(modelName);
...
}
randomMethod() {
... // using app for other stuff....
}
}
Which I find is very bad software design. My reasoning is as follows:
- Access of a static var inside an object (constructor) - it should be passed in. Why bad:
- Dependent of the existence of "app" and that "app" has an object gallery
- As such this code cannot be tested
- Instead of passing in a string, you could pass in the finished object (model in this case)
- Global var "app".
My problem is, that it seems, I'm alone with this opinion inside the team. Other members stated that they want to construct an instance of agent
simply by:
const agent = new Agent("chicken");
Insteaed of using Factory/Factory Method or something similar.
My first simple suggestion was this:
new Agent(app.gallery.getModel("chicken"), app);
Since the class Agent
uses app
through its code, I would pass it in, instead of accessing it via global var. This first suggestion was denied by the team stating that:
- The API is to complex (see constructor string example above).
- Tied deadline, cannot discuss this minor details
Given the context, I have the following questions:
Is my reasoning for disliking this kind of code style (accessing a static inside constructor, using a static throughout the class instead of using a field) correct?
What alternative are there to simplifying the constructor process?
If my intuition is right, what kind of paper/argument can I bring forth?
app.gallery
is so-called an "static asset store", hard-coding it might not be an issue. This kind of usage is very commonly seen in mobile apps and app store apps. The belief is that each app will only have one such static asset manager, and that the content of that static asset manager can be edited in the same software development environment.Agent
to fetch something from two or more different implementations ofapp.gallery.getModel
within the same version and platform of the software?app.gallery
is a static asset store. What about other access toapp
insideAgent
? What about unit testing? Ok, unit testing graphical aspects may not be feasible but.. still? And to answer your other question: Yes and no, it may be possible in the (far) future, that users could provide own assets - in such a scenario the implementation would have to change.