Is it bad coding practice/design to make a class which will only be instantiated once?
I have some variables and functions that can be grouped together under a class to “look good” (for a lack of a better description) since they are somewhat related, but they can just be global variables and global functions.
(Btw, I am using JavaScript, AngularJS, Express, MongoDB.)
5
A single instance for a class makes sense if the object represents a single resource, like a ethernet connexion or the operating system task manager for instance.
Functions are put in a class only if they act on the variables of instances of that class, otherwise the maintainer will be confused about the intention of that class.
Usually, a good reason exists why your app has global variables. Try to find the common purpose of them and design a class around this purpose. It will not only make the design clear, but your mind as well.
1
There is no problem with writing a class that ends being instanced only once.
If putting some variables and functions together within a class makes sense, has semantic value or makes the code simpler to read and manipulate, then so be it, maintainers will be thankful.
What can be very wrong though, is to enforce uniqueness from within.
Many people tend to fire the singleton pattern as soon as they think they’ll only need one instance of something. See for example how you haven’t described your context in depth, yet most answers already suggest you to use a singleton. And then the design might be screwed because if you need another instance at some point for whatever reason, the class will prevent you from doing it without changing a lot of code.
So as a conclusion, one instance is fine, but make sure to know whether:
- you only need one instance, or
- you cannot have more than one instance (a very, very rare case in my experience).
13
Is it bad coding practice/design to make a class which will only be instantiated once?
No, even if only instantiated once, you need a class for it. But don’t suppose you will always only need one instance, now and in the future.
Suppose you make a game, and you have a player class:
class Player {
...
}
You will instantiate one player class in your game, and use always the same object. Nothing wrong with it.
However, don’t make a Singleton out of it! Today you design your game and think “Well, it will always be a one-player game. I will use a Singleton for my Player”. But in version 2, you may want to implement multiplayer – and then you are getting into trouble. You will have to rewrite and adapt a lot of code, because your design does not support instantiating multiple players.
The same is with caches. Or loggers. Or network interfaces. Today, you think you will only need one, always, for sure. But in a future version you need to add a second. Be aware of this situation.
You are asking two questions in one.
- No, there’s nothing bad having a class that will only have one instance.
- Grouping variables and methods in a class without a real reason for it is bad design.
The singleton pattern is common. It allows you to enforce single instance of class.
But for some it is too common, so it becomes an anti-pattern. The reason for this is because it is practically same as global state. And any global state is hard to mock and test and hard to debug. But at the same time, having to explicitly pass this one instance everywhere in your code can cause code to bloat up a lot. But this will make you think about proper architecture. If bunch of classes use this one singleton, you might thing they are related somehow and create common ancestor for them, that will encapsulate this one instance for them.
There are singleton classes and that is a common design pattern.
There are also classes that never get instantiated. If you have functions that DO NOT operate on any persistent state (e.g., they take the input and transform it), you can usually use a static method. In some languages (like Java) these static methods would be declared in a class, but the class itself will never be instantiated.
It’s bad to assume something is universally bad. In your case, maybe it would be a good idea, but given that you’re using a language that’s not OO at the core it’s more appropriate probably to just use loose methods and variables and put them in a common library that you include in your other work.