13

I am reading Clean Code by Uncle Bob. Because I am not a native-English speaker, I couldn't understand following statement:

Classes and objects should have noun or noun phrase names like Customer, WikiPage, Account, and AddressParser. Avoid words like Manager, Processor, Data, or Info in the name of a class. A class name should not be a verb.

As I know, none of the Manager, Processor, Data, and Info is a verb, isn't it? What is the actual point he want to emphasize?

  • maybe Uncle Bob is adopting RESTful thinking in extolling the virtues of resource-as-objects. – rwong May 9 '13 at 19:35
20

The three points are separate:

  • Class names should be nouns or noun phrases. This means that the name of the class should be something that would be the subject of a verb. In the case of object-oriented design, methods would be the verbs that take place on the thing that the class is a representation of.

  • Some words should be avoided. Manager indicates a possible god class. Info and Data may indicate a dummy data container. Words like this may indicate poor modeling of the problem space.

  • Verbs should never be class names. See the first point - classes model things, methods model actions.

  • 1
    Words like this may indicate poor modeling of the problem space. - Well, one would think, either the problem space is modelled poorly or not, independent of the names choosen. A good name doesn't help a poor design; and a good design is only marginally hurt by a poor name. – Ingo May 9 '13 at 22:15
  • And you could take this a step lower when considering how you actually build your class and how it interacts with the outside world - the class's properties would generally be nouns, while the class's methods would generally be verbs – PeteH May 10 '13 at 19:59
  • If your project has a class that you would consider calling "Manager" or "Info" or "Data" then it likely has a dozen such classes, which means you wouldn't use these names. You would have a ThisManager and ThatManager and AnotherManger and so on. – gnasher729 Aug 15 '15 at 22:06
  • He says verbs should never be in class names, but I was just rereading Adding Meaningful Context, and in his example on page 29, he names a class GuessStatisticMessage. Now I am confused on the rule, because I interpret his usage of Guess as a verb, as opposed to a noun. dictionary.com/browse/guess?s=t – Devin Gleason Lambert May 4 '17 at 20:28
  • 2
    @DevinGleasonLambert But it's not Guess. It's a Guess Statistic Message. A message is a noun. Guess Statistic just describes the type of message that it is - one that messages about guess statistics. – Thomas Owens May 4 '17 at 21:02
7

He's trying to draw a distinction between things (nouns) and actions (verbs). In conventional objected-oriented design, we think of classes as things, and their methods as the actions those things can perform. To manage is to take care of or coordinate, while manager is a person or thing that manages.

Introductory books usually boil this down to the simplest and most obvious terms possible like a class named Dog, with methods Bark and Bite. In real world classes, the distinction is often a little more subtle, but it's still there. I believe the point Uncle Bob is making, however, is that while manager is a noun, but it puts a lot of attention on the what the manager does, and not what it is-it's too vague of a word for describing exactly what is being managed or how.

  • 5
    Unfortunately, this kind of thinking can lead to wrong designs. One of the prime examples is the classic introductory example of a bank account. In almost all OO textbooks, Account is an object, balance is a field and transfer is method. But the correct design would be: Transfer is an object, account is a field and balance is a method. That's how banking systems are actually implemented and how banking actually worked before computers. – Jörg W Mittag May 10 '13 at 1:03
  • @JörgWMittag: that cautionary tale is well worth posting as an answer; that these are general guidelines, and can in certain rare cases lead to bad decomposition. – smci Mar 11 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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