10

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
  • 2
    Perhaps you need a Singleton class. However it is bad idea to bind in a class, things that are not related properly.
    – NeonGlow
    Commented Oct 4, 2013 at 3:38
  • 2
    Singleton pattern is bad. Single instance sometimes is the correct solution.
    – Bryan Chen
    Commented Oct 4, 2013 at 4:13
  • 4
    Neither "Singleton" nor "Single instance" is a good solution for grouping "just somewhat related" global things together - when I hear such a description, I take this always as a warning signal. But to give you a serious answer, you should tell us some examples of the variables and functions you are going to group together (with their real names and meaning).
    – Doc Brown
    Commented Oct 4, 2013 at 5:53
  • 2
    The Singleton pattern is not bad necessarily. It's only that it tends to be overused.
    – Stephen
    Commented Oct 4, 2013 at 6:12
  • Is it even technically possible to enforce a Singleton in JavaScript? Commented Oct 5, 2013 at 15:37

7 Answers 7

11

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
  • 2
    +1, but I would like to add: "if the only common purpose is that those variables are global, better leave them in different classes."
    – Doc Brown
    Commented Oct 4, 2013 at 8:45
14

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:

  1. you only need one instance, or
  2. you cannot have more than one instance (a very, very rare case in my experience).
13
  • 1
    re: #2, I don't think this is ever valid. Even if your object is a NuclearPowerStationCentralCommandController and you will only ever own one nuclear power station, you're not seriously telling me you don't have a MockNuclearPowerStationCentralCommandController for testing, are you?
    – Phoshi
    Commented Oct 4, 2013 at 14:18
  • A more serious but less obvious problem is if you need to have a slightly different implementation of the Singleton, you can't in most languages, because the way you get the Singleton is to go directly to it, rather than have the instance passed in. Commented Oct 5, 2013 at 15:35
  • @Phoshi: If only one of something can ever exist, then code can use it without having to hold a reference. If multiple instances are allowed to exist, then consumers will have to hold references to them. Not only does this add to the object's storage requirement, but consumers will no longer be able to assume that other consumers will hold a reference to the same object. This adds considerable complexity. If the complexity will end up being necessary, it's better to add it sooner rather than later, but if it's not necessary, it may be best avoided.
    – supercat
    Commented Mar 15, 2014 at 22:00
  • @supercat: Adding one reference's worth of memory usage is utterly irrelevant for the overwhelming majority of cases, I don't think that's at all a good reason to avoid DI. You are right that you can't assume everyone has the same instance any more--you shouldn't, that shouldn't matter. It doesn't matter if everyone has a different instance so long as the semantics of the interface hold. Anyway, like I said, you're never going to have only one instance. You have at least one more for testing, and do you really want to risk the assumption your requirements won't ever change?
    – Phoshi
    Commented Mar 17, 2014 at 9:42
  • @Phoshi: The memory usage isn't the issue; the issue is that one adds another way in which objects can differ. Consider, for example, what it means for the items in two instances of Dictionary<TKey,KTValue> to be distinct. If all instances of a Dictionary<TKey,KTValue> instance use the same IEqualityComparer, there's no ambiguity, but if they can use different comparators that define different equivalence relations, I don't know that it's possible to sensibly define a.ContentsDistinctFrom(b) to always equal b.ContentsDistinctFrom(a).
    – supercat
    Commented Mar 17, 2014 at 13:29
4

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.

3

You are asking two questions in one.

  1. No, there's nothing bad having a class that will only have one instance.
  2. Grouping variables and methods in a class without a real reason for it is bad design.
2

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.

1

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.

1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.