31

Recently I came across a moderately large python codebase with lots of MyClassAbstractFactory, MyClassManager, MyClassProxy, MyClassAdapter etc. classes.

While on the one hand those names pointed me to research and learn the corresponding patterns, they were not very descriptive of what the class does.

Also, they seem to fall within the forbidden list of words in programming: variable, process_available_information, data, amount, compute: overly broad names, that don't tell us anything about the function when used by themselves.

So should there be CommunicationManager or rather PortListener? Or maybe I do not understand the problem at all...?

4
  • if you are familiar with what the pattern does then the pattern name is a decent description, however just the pattern name is a bad idea, it's better to have a MyClassFactory, a FooAdapter, etc. Sep 16, 2013 at 9:50
  • Edited the question to indicate that the classes were not called just "AbstractFactory", but some descriptive words were there too.
    – Vorac
    Sep 16, 2013 at 9:59
  • 1
    ...did they seriously call it a Fctory instead of a Factory, or is that just a typo?
    – Izkata
    Sep 16, 2013 at 17:22
  • @Izkata, lol, my bad. However, there were Adaptor and Adapter!
    – Vorac
    Sep 17, 2013 at 7:49

4 Answers 4

50
  • AbstractFactory is indeed a poor choice for a name. There is no way to know what is created by this factory, and when you'll look for an entity which creates Animals, you'll never find the corresponding factory by name.

  • AnimalAbstractFactory is not a wise choice neither, since in most languages, it would be redundant with the abstract keyword in the signature.

    This being said, there are several good reasons, highlighted by the comments, to actually include Abstract in the name: not only there are several contexts where you don't have the full signature, but just the name, but also, keeping AnimalFactory for an interface may be a wise choice (unless, unfortunately, the convention of the language/framework is to prefix interfaces with I).

  • AnimalCreationUtility would also be a bad choice: if it's a factory, make things easier for people who will read code, and call it a factory.

  • abstract AnimalFactory is ok. It doesn't have redundancy, and is clear that it is an abstract factory which delegates the creation of animals to its children.

So yes, including the name of the design pattern is a good idea, but it should be only a part of the name, and shouldn't be redundant with the other parts of the signature.

7
  • 2
    Why is this better than writing a comment in a prominent place "In this module, we implement MVC. Reasons: ... Models:... Views:... Controllers:... Structure map:... API:... ".
    – Vorac
    Sep 16, 2013 at 10:21
  • 39
    @Vorac: Having a clear name is always better than relying on comments. Sep 16, 2013 at 10:32
  • 2
    @Vorac sooner or later someone will add a new class without updating that prominent comment (or even knowing about its existence). While it's much harder to overlook a naming convention consistently used across the entire app. Sep 16, 2013 at 12:38
  • 2
    While you are browsing through your project solution, will you open each class file to find what it does? No. That is why it's always a better idea to have descriptive name classes/files.
    – matrix
    Sep 16, 2013 at 15:29
  • 2
    I would like to kindly disagree with the second point: I think that AnimalAbstractFactory is a good choice, because even though it is redundant in the class declaration, it would be very helpful in the child class declaration: LionFactory extends AnimalAbstractFactory which, I think, is a nice piece of information. Sep 17, 2013 at 12:58
11

Depends on the specific example. The Builder pattern is almost always best served by naming your class *Builder, while a Singleton doesn't usually need to be named as such.

If you don't put the pattern name in your class name, and maybe even if you do, you should generally put a comment in the class which explains that it implements a specific pattern.

1
  • 3
    Consistency is crucial here, because once only some factories are called ...Factory, it becomes quite a mental bump to realize a class is a factory if its naming breaks that convention. Sep 16, 2013 at 12:40
10

The whole point of using pattern names in classes is to make it easy to understand what the class does. If you name the class AnimalFactory it's obvious that the class creates Animal instances. If the name of your class includes a name of a pattern and it does not describe what it does you've either chosen a wrong pattern or implemented it incorrectly.

1

I think it can work really well. For example:

// Command for retrying card entry with CVN.
public class RetryCardEntryWithCVNCommand { ... }

// Query for getting expired accounts
public class GetExpiredAccountsQuery { ... }

// Decorator for logging exception. Implies that it's an additional 
//mechanism for logging exceptions.
public class LogExceptionToDbDecorator { ... }

// Factory for creating account filters
public class AccountFilterFactory { ... }
2
  • 1
    how does this answer the question asked? Per my reading, your "examples" only show useless duplication of class names and code comments
    – gnat
    Sep 16, 2013 at 13:54
  • Comments are to justify purpose of each class in case class name is not apparent to some. By looking at these I instantly know that I have command that does something, query that returns data, decorator that adds additional behaviour to existing exception logging mechanism and a factory that creates account filters. In my opinion the more descriptive you are with your class names, the easier it becomes for others to read your code. If you are using a design pattern, then say so - at the end of the day the whole purpose of having design patterns is to make it easier for others to read your code
    – CodeART
    Sep 16, 2013 at 14:49

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