9

Over the course of a few projects, I have written a decent amount of class factory methods for accessing a Singleton instance of a class. I have always used some variation on the + (id)sharedSomething; naming convention.

Apple, on the other hand, has a variety of naming conventions. For example:

// NSNotificationCenter
+ (id)defaultCenter;

// NSUserDefaults
+ (NSUserDefaults *)standardUserDefaults;

// UIApplication
+ (UIApplication *)sharedApplication;

Is there any rhyme or reason to the adjective placed before the noun in those names that I should be aware of when naming my own methods? I originally thought it might have something to do with "flexible" vs "strict" singleton designs, but NSFileManager and NSNotificationCenter both follow the + (id)defaultSomething convention, yet NSFileManager supports the allocation of other instances while NSNotificationCenter does not. I'm stumped.

EDIT: I was wrong in thinking NSNotificationCenter does not support the instantiation of new centers. It's just not terribly common, so the original hypothesis is not necessarily invalidated.

3 Answers 3

6

In general, the shared… methods are for true singletons (NSApplication, NSWorkspace, etc.) while default… or standard… denotes a class that can be usefully instantiated, but where most clients will be happy just working with a single global instance. But there isn't any public written standard, and it seems primarily to have been a decision made whenever such a class was written.

3
  • The "usefully instantiated" vs "true singletons" is what I was thinking when I said "flexible" and "strict", respectively. I originally discounted that theory under the (incorrect?) belief the NSNotifactionCenter could not be usefully instantiated (due to a lack of init method in the docs). Commented Feb 16, 2011 at 23:53
  • @Matt Wilding: init is inherited from NSObject. It would only be mentioned if NSNotificationCenter overrode it to do something interesting. The docs don't state that there's only supposed to be one and init isn't documented as failing if an instance exists, so it isn't really a singleton, though it is pretty much always used that way.
    – Chuck
    Commented Feb 16, 2011 at 23:55
  • Indeed. It was a rather spontaneous and not-well-thought out decision that NSNotificationCenter's lack of init in the docs meant it can't be instantiated. I guess the combination of that, and the fact that I've never instantiated one, were just too much for me. But, that means that there is at least some pattern to the naming conventions, which is nice even if unofficial. Commented Feb 17, 2011 at 0:04
2

I found, by complete happenstance, this position on the topic from the book iPhone App Development by Craig Hockenberry:

Unfortunately, there's no one standard for naming singletons. Older classes, such as those in the foundation, use the default prefix on the method name. Newer classes tend to use shared as a prefix.

I'll cite it as an alternative to the answer posted by @Chuck. The summary seems to be that there is no real pattern, while Chuck points to a rough correlation between true singletons and objects that are just commonly used as singletons, and Mr. Hockenberry points to a rough correlation in age.

1

It has nothing to do with "flexible" vs "strict" singleton designs. NSNotificationCenter and NSUserDefaults are not even Singletons. Do you really think flexibleSingleNotificationCenter is a better name than defaultCenter? Is there any single way that it helps the user of the method to know how it is implemented?

NSDeteriministicFiniteStateMachineToggleButton anyone?

5
  • You sound a bit like you're ranting about something, but I honestly have no idea what you're saying. Commented Feb 16, 2011 at 23:58
  • Honestly? I can't believe that honestly can't understand what i'm saying.
    – hooleyhoop
    Commented Feb 17, 2011 at 9:47
  • I think there are 2 Singletons in Cocoa. There may be another, but what does it matter how they are named? It's an implementation detail that shouldn't concern you. You shouldn't count on any class being or not being a Singleton in the future. That you are concerned about it suggest you are approaching the framework incorrectly.
    – hooleyhoop
    Commented Feb 17, 2011 at 9:56
  • Maybe you just misunderstand the purpose of the question. I make singletons. I'm curious how Apple uses naming conventions for the accessor, because following Apple naming conventions is good form. I don't care how many "true singletons" there are. I'm not saying they should be named anything else. I don't care if they are or are not singletons in the future. Would you say the difference between the names of "arrayWithArray" and "initWithArray" are "implementation details that don't concern me"? Clearly not: their names denote different use patterns. Naming conventions often carry significance Commented Feb 17, 2011 at 16:22
  • Yes, maybe, i did misunderstand. But remember, if there are 2 Singleton Classes out of roughly 2000 Classes in Cocoa, and you are 'making Singletons' and wondering what the naming convention is - that should tell you something.
    – hooleyhoop
    Commented Feb 17, 2011 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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