• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class names  RSS feed

 
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

realise as simple as it is...its important

should class names be single or plural

eg:

class ComputerShop makes sense

but

Class ShopCustomer or ShopCustomers

thanks
 
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singular noun (almost in all cases).

Why? Because your mentioned ShopCustomer instance of a class would represent one shop customer. Collection of those would be shop customers, but that way you could name a list of them.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats great

thanks
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Singular noun (almost in all cases). . . .
There is an exception for utility classes. The class which provides methods to work with arrays is called Arrays and the class which provides methods to work with collections is called Collections.
 
Sheriff
Posts: 11496
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule of thumb is to make the name as grammatically correct as it can be in the context where it is most commonly used.

Although syntactically correct, this code is not grammatically correct and therefore more likely to confuse the reader:

You don't say "The customer reference variable represents one ShopCustomers," do you?

On the other hand, if you said "The customer reference variable represents one ShopCustomer," that would be grammatically and conceptually correct.

With the case that Campbell pointed out regarding the utility classes, those are also named in a way that suggests what they are conceptually intended to be, which is to provide a set of utilities to handle a certain class of objects.  That is, the  Arrays class is a set of utility methods that are meant to be used with all manner of Array objects and the Collections class is a set of utility methods that are meant to be used with all manner of Collection objects.  Think of it like a "Tools" label you'd put on a box of tools. The label wouldn't just say "Tool" because the box contains many tools, right?  Or like labels you put on folders on your desk for "Bills", "Coupons", "Receipts", "Paystubs", etc. You use the plural form for those labels because that's what the folders contain, more than just one bill, coupon, receipt, paystub, etc.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . .  Think of it like a "Tools" label you'd put on a box of tools. . . .
What a good way to put it
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!