Naming Conventions – Disadvantages of Alphabetical Naming

naming

Let me give you an example of how I name my classes alphabetically:

  • Car
  • CarHonda (subclass of Car)
  • CarHondaAccord (subclass of CarHonda)

There are two reasons why I put the type of the class earlier in its name:

  1. When browsing your files alphabetically in a file explorer, related items appear grouped together and parent classes appear above child classes.
  2. I can progressively refine auto-complete in my IDE. First I type the major type, then the secondary type, and so on without having to memorize what exactly I named the last part of the thing.

My question is why do I hardly see any other programmers do this? They usually name things in reverse or some random order. Take the iOS SDK for example:

What are the disadvantages of my naming convention and the advantages of their convention?

Best Answer

If iOS followed the naming convention that you propose, the classes you mention would be named:

  • UIObjectResponderViewcontroller

  • UIObjectResponderViewcontrollerTable

There would also be classes with names like:

  • UIObjectResponderView

  • UIObjectResponderViewControl

  • UIObjectResponderViewControlButton

  • UIObjectResponderViewScroll

  • UIObjectResponderViewScrollTable

The disadvantages of this naming convention become more and more apparent as this size of the framework grows:

  1. Very long class names.

  2. Most classes start with the same long prefix, like "UIObjectResponderView..."

  3. The sorted order of source files matches the inheritance hierarchy, which isn't nearly as useful as having them groups according to functionality. That is, it's more useful to have XYDetailView and XYDetailViewController end up close to each other than to have XYObjectResponderViewDetail grouped with all the views, and XYObjectResponderViewcontrollerDetail grouped with all the view controllers.

  4. Naming classes according to their inheritance chain violates the "Don't Repeat Yourself" (DRY) principle.

  5. Makes name completion almost useless -- you have to type most of a very long name before the list of possible completions is useful. In Cocoa Touch, nearly every class is derived from NSObject, and a great many classes are derived from NSResponder, and the number of views is fairly large. So every time you want to type the name of a view, you'd have to type at least "UIObjectResponderView" just to get to the (long) list of view classes.

  6. Doesn't work with prefixes. In Objective-C, name prefixes like "NS", "UI", "CF", and "MK" are used to avoid name collisions. UIKit classes all start with "UI", but all those classes are actually derived from NSObject.

  7. Doesn't work well with class names made up of multiple words. A view controller is a controller that manages a view; it is not a view. Using your convention, capitalizing both those words would make "UIObjectResponderViewController" appear to be a subclass of "UIObjectResponderView."

The advantages of more conventional naming schemes are pretty much the inverse of the disadvantages listed above:

  1. Allows much shorter names, like "UIButton" instead of "UIObjectResponderViewControlButton."

  2. Avoids the tediously long common prefixes. Class names are actually readable: "UITextField," "UISlider", "NSManagedObjectContext" and so on.

  3. Source files sort into more useful groups.

  4. Doesn't violate DRY.

  5. You can type just a few characters of a name and get a useful completion list.

  6. & 7. Works fine with different prefixes and with multi-word names.

Related Topic