Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Often, in libraries especially, packages contains classes that are organized around a single concept. Examples: xml, sql, user, config, db. I think we all feel pretty naturally that these packages are correct in the singular.

com.myproject.xml.Element
com.myproject.sql.Connection
com.myproject.user.User
com.myproject.user.UserFactory

However, if I have a package that actually contains a collection of implementations of a single type - such as tasks, rules, handlers, models, etc., which is preferable?

com.myproject.tasks.TakeOutGarbageTask
com.myproject.tasks.DoTheDishesTask
com.myproject.tasks.PaintTheHouseTask

or

com.myproject.task.TakeOutGarbageTask
com.myproject.task.DoTheDishesTask
com.myproject.task.PaintTheHouseTask

share|improve this question
1  
singular just like database table names should always be singular but for different reasons. Look at any popular standard library like Java or Python for example. –  Jarrod Roberson May 12 '11 at 18:39
    
@Jarrod Roberson: Please post your answer as an answer so we can upvote it properly. –  S.Lott May 12 '11 at 18:43
    
@Jarrod gonna need some examples, since in standard libraries most classes fall into the first category I listed. –  NickC May 12 '11 at 19:03
    
@Renesis Take a look at my updated answer. –  Matthew Rodatus May 12 '11 at 19:19
    
@Matthew - I like it. You've expressed what I suspected but wasn't sure how to codify. –  NickC May 12 '11 at 19:23
show 1 more comment

1 Answer

up vote 83 down vote accepted

Use the plural for packages with homogeneous contents and the singular for packages with heterogeneous contents.

A class is similar to a database relation. A database relation should be named in the singular as its records are considered to be instances of the relation. The function of a relation is to compose a complex record from simple data.

A package, on the other hand, is not a data abstraction. It assists with organization of code and resolution of naming conflicts. If a package is named in the singular, it doesn't mean that each member of the package is an instance of the package; it contains related but heterogeneous concepts. If it is named in the plural (as they often are), I would expect that the package contains homogeneous concepts.

For example, a type should be named "TaskCollection" instead of "TasksCollection," as it is a collection containing instances of a Task. A package named com.myproject.task does not mean that each contained class is an instance of a task. There might be a TaskHandler, a TaskFactory, etc. A package named com.myproject.tasks, however, would contain different types that are all tasks: TakeOutGarbageTask, DoTheDishesTask, etc.

share|improve this answer
4  
For a similar question, see english.stackexchange.com/q/25713. A category is analogous to the singular and a type is analogous to the plural. –  Matthew Rodatus May 17 '11 at 15:11
    
The very link you provided shows an exception to this rule. beans is plural, however java.beans contains all kind classes related to JavaBeans. –  SkyDan Sep 18 '12 at 12:11
    
Good answer, but I don't agree with the database relation logic. Relations in an ERD are singular because they are showing relationships between entities. Tables are a physical implementation of the relations, and they hold multiple rows, and should thus be plural IMO. The answer to "what is in this table?" is "users" not "user". Granted, it seems that singular naming is more common in the enterprise world (C#, Java) than it is in communities like Ruby, Python, Javascript, and PHP. –  ryeguy May 21 '13 at 20:49
    
So essentially name a package plurally if it has classes that really do serve different puprposes or whatever, but which are just similarly-themed, and name a package singularly if it just contains classes that heavily focus on coming together as a sort of "meta-class" for a single purpose? –  Panzercrisis Oct 22 '13 at 18:09
    
Also AS3 wouldn't be an exception to the rule, would it? –  Panzercrisis Oct 22 '13 at 18:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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