tags:

views:

923

answers:

15

I was inspired to ask this here based on this blog post. Do you always try to use nouns for class names?

+22  A: 
MattW.
The pure beauty of this answer impresses me. Upvote for that.
OregonGhost
Short and to-the-point. The perfect answer!
Keithius
What about function objects? It is okay to call it ComputeLength, or similar? Sounds better than LengthComputer, or LengthCalculator
LengthCalculator.Length() sounds better to me.
Camilo Martin
+2  A: 

Singluar nouns, yes. Verbs are for methods because they are performing an action.

Forgotten Semicolon
+1  A: 

Yes, although I will usually use an adjective for Interfaces.

chris
I'm not seeing how that would work out...can you provide a (real?) example of an interface that has an adjective name and classes that have implement that interface that have a noun name? Thanks.
Thomas Owens
Just look at the Java and C# interfaces:Comparable, Iterable
chris
Oh. Heh. Those are adjectives, aren't they. English fail on my part.
Thomas Owens
+2  A: 

Yes, unless it's an anonymous class.

If it's a class that only exists to perform some action then name it accordingly (ActionListener, for example).

Bill the Lizard
Which, again is a noun. :)
lowglider
:( Lowglider beat me to it. Anyway, xListener is a noun...
Thomas Owens
Yes, that was my point. Nounify the verb. :)
Bill the Lizard
Listener is a noun. Listen is a verb.
Baddie
A: 

Nouns or nouns with adjectives - always.

17 of 26
I tend to reserve adjectives for interfaces, like ISerializable. A similar class I would name CSerializableImpl or a similar noun.
Jeff Yates
A: 

Depends on whether the class represents a noun or not and whether you consider only the class itself or also its ancestor class(es).

For example, I did a game a while ago with an abstract "Order" class with concrete subclasses named "Move", "Attack", "Capture", etc. The abstract parent is a noun, but the concrete children are verbs. (Possibly relevant: This is in Perl so, by convention, the full name of the class is "Order::Move", etc., providing context which would otherwise require naming the subclass "MoveOrder" (a noun).)

This is a fairly typical pattern for me when dealing with a group of verbs, though - a noun-named parent class with verb-named children. I would expect that the same would apply for other parts of speech as well, although I can't really come up with a non-pathological case where creating separate Speed::Fast and Speed::Slow classes would be warranted.

Dave Sherohman
In this usage, Move pretty much is a noun. It's the "Move Action" you are working on.Although I'm pretty sketchy at my sentence diagramming, I'm pretty sure that if you were to speak about your Move action in a sentence, it would generally drop into the "Noun" spot.
Bill K
Yep, which is why I noted that it depends on whether you consider only the name of the class itself (Move) or also its ancestors (Order::Move). When talking about the class, it definitely takes the noun spot because the class itself is a thing (noun).
Dave Sherohman
A: 

Another yes. I can't say that I make a concerted effort to do so, but all of my classes end up being named after nouns because that makes sense. If it didn't make sense in some situation, I would not stick to it as a rule.

Ed Swangren
+2  A: 

Maybe not for functors.

Mark Ransom
A: 

An implementation of the Command pattern would do well to have verb class names.

Chris Marasti-Georg
I would disagree with you: http://en.wikipedia.org/wiki/Image:Command_Design_Pattern_Class_Diagram.png
Thomas Owens
Your link doesn't seem to prove anything. Are you saying that "class InsertTextAtLocationCommand: Command" is better than "class InsertTextAtLocation: Command"? Why?
Chris Marasti-Georg
Yes, because it's more clear that the Command Pattern is at play in the interactions between classes. If I see a pattern, I generally refactor the names so the pattern is more evident.
Thomas Owens
+4  A: 

I could be wrong but I think that you've missed the point of the blog post. I think the author pretty well sums it up with:

"Object Oriented Programming puts the Nouns first and foremost. Why would you go to such lengths to put one part of speech on a pedestal? Why should one kind of concept take precedence over another? It's not as if OOP has suddenly made verbs less important in the way we actually think. It's a strangely skewed perspective. As my friend Jacob Gabrielson once put it, advocating Object-Oriented Programming is like advocating Pants-Oriented Clothing."

His point being that a proper language should provide facilities for both functional and object-oriented programming methodologies.

So a more appropriate question to ask based on the post is whether the author is correct in his declaration that OOP should not eclipse functional programming but that they should exist as equal entities and partners in development?

I myself have had this discussion numerous times and as a PHP developer I frequently take advantage of its dynamic typing as well as its functional and object-oriented capabilities. I tend to lean toward object-oriented solutions but there are times when you need a simple stand-alone function that doesn't fit nicely into a specific class.

Noah Goodrich
That article is an example of extreme hyperbole. While it is possible to create a mess of nested Java commands like that, a class can also be a namespace for a collection of functions (or static methods as Java class them). The System class has a lot of these, such as System.currentTimeMillis()
R. Bemrose
I agree. I learned C# in school and I've never seen any purely object-oriented code that looks like the mess that the author was describing.
Noah Goodrich
A: 

yes - in english, nouns are for things, verbs are for actions/relationships

Steven A. Lowe
+1  A: 

Exceptions:

  • Command classes are normally verbs (Login, Logout, Create, etc..)
  • State classes are normally adjectives (Closed, Open, Locked, Unlocked, etc..)
  • Utility classes are normally plural nouns (Collections, Beans, Math, etc...)

EDIT:

@Thomas Owens makes a good point that commands can be suffixed with Command to become LoginCommand. Similarly states can be suffixed with State to become ClosedState.

toolkit
Like I said in response to Chris Marasti-Georg, I use ClosedState, OpenState, etc for State classes and LoginCommand, LogoutCommand, etc for Command classes and so on. I find this makes it more understandable, for myself and for others to see a pattern in place without even needing to see code.
Thomas Owens
Yep, valid point.
toolkit
+1  A: 

I name my objects after PROnouns. They're usually short and easy to type. And whose going to forget "it"? Of course, I tend to run out of them quickly, so when that happens I tack on an adverb.

MeQuickly reactorCore = new MeQuickly();
reactorCore.Temperature = ThemGently.GetTemperature();
Jeffrey L Whitledge
A: 

I think Yes. The only time I doubt myself is when there is a temptation to create a class that provides a certain service and sounds like a noun - for example FileParser or SpellChecker. Can't I just have a simple object (take SpellChecker as the example) and send it words to check? It can take in an object (a word) and spit out another (a boolean for whether or not the word is correctly spelled).

But the correct approach would be to have checkSpelling, the verb, as a method of whatever the word is typed as (likely String).

Ben Packard