10

Apart of the code conventions to use camelCase, PascalCase etc., are there any conventions for naming packages and classes in Java?

For example, I have an mvc project and the main package is com.myproject. In this package I have:

  1. com.myproject.model
  2. com.myproject.view
  3. com.myproject.controller.

Is there any convention or best practise to give a name to the class in these 3 packages? Like for the package 1 avoid model in the classname, or something like this? And if I want to use the same class name like User, what is better: to use UserModel, UserView, UserController, or give the same name to the 3 classes? Is there some kind of best practise or naming convention to do this?

4
  • possible duplicated stackoverflow.com/questions/3226282/…
    – anion
    Commented Jun 5, 2018 at 13:00
  • 2
    Naming three classes User would be confusing. They're not users, they're controllers or views or models, related to user.
    – Kayaman
    Commented Jun 5, 2018 at 13:06
  • 1
    So the package name is usually the URL for your company, the project, then any sub-packages. That's the convention set up by Sun in the way back machine. Not everyone follows it, but a majority do. Commented Jun 5, 2018 at 13:13
  • 1
    @Kayaman, The model would be the User, but the view and controller should definitely be named differently. Commented Jun 5, 2018 at 14:48

4 Answers 4

19

Short answer: Naming two classes the same in the same project/library/context is not advisable.

Aside from the obvious inconvenience, there is a deeper point why it isn't a good idea and neither is your proposed package naming. We as developers are responsible for modeling business problems, and our naming vocabulary should reflect this fact. This is kind-of the point of Domain Driven Design.

Putting the technology above the actual function might be logical for us, because we are more interested in technology, patterns, architecture and things like that, but makes our model less expressive, less maintainable.

My point is: Package names and class names should reflect business concepts. There should be no model, view, controller packages, there should be no UserModel, UserController classes, etc.

Model the problem, not the technology! Find suitable abstractions straight from the business requirements and hide implementation details, like the fact that you are using MVC.

4
  • Your point of view is very interesting, I asked this question to improve myself with the others experience and knowledge. So, suppose that my mvc project model registration on a website, in which way you suggest to organize package and class? Commented Jun 6, 2018 at 7:33
  • 2
    Without knowing your exact requirements, I can think of following words: registration (package), Credentials, User/Customer, authenticate(). Things like that. The model/view would not be Java code maybe, but HTML and CSS respectively. Commented Jun 6, 2018 at 10:38
  • Indeed, interesting
    – Asraful
    Commented Nov 14, 2021 at 14:15
  • @RobertBräutigam Are you aware of any openly available projects that do a decent job of hiding the fact that they are using the MVC pattern? Commented Jan 28 at 10:34
6

Regardless of language, but specifically in Java, you want to, more or less follow these guidelines:

  1. Classes named after nouns (Printer, Controller, Mailbox, Manager, Handler), meaning, something, an entity that makes sense in your problem domain.
  2. Interfaces as adjectives (Iterable, Serializable, Secured, Asynchronous), meaning, a capability or characteristic of an entity implementing said interface.
  3. Methods as verbs or in some cases, adverbs or adverbial clauses, whichever makes sense to describe an action (execute, deliver, validate)
  4. Packages as a grouping of things that make sense to be together (again, this is very strongly tied to your problem domain.)

Beyond these general guidelines (not rules, but guidelines or suggestions), it is hard to give concrete examples without having a concrete problem domain.

We want to define a problem domain independently of the programming language, one that describes the functionality you need, and the entities that deliver the functionality, and the interactions between them.

Then and only then you have a starting point to start naming classes, packages and methods.

1
  • thank you for your answer. Image that the project is used to: save modify and show user informations. What you suggest? Commented Dec 6, 2019 at 11:00
2

Naming conventions are pretty much left to the developer or team most of the time. For me personally, I have almost always relied on the information published via Oracle when working in Java.

http://www.oracle.com/technetwork/java/codeconventions-135099.html

More directly related to your question, if you wanted to have a class called "Person" in all three packages, that would be permissible. When you want to include those packages somewhere and actually use the classes therein, that is where you will see that it may or may not be a good idea.

Looking at your package names for example, "com.myproject.model" why would you have a class called "Model" versus something else, such as "Person" or "Order"?

Maybe I am misunderstanding the question but it makes more logical sense to me for it to be used in this way such as in the following pseudocode:

com.myproject.model.Person x = new com.myproject.model.Person();
6
  • Thank you for your answer, I have no class called Model. com.myproject.model is the name of the package, the question is that is better to use UserModel in the package com.myproject.model instead of User, but as you and the other suggested, is better to use UserModel class instead of User, because If i use the name User for 3 different package is difficult to understand and use Commented Jun 5, 2018 at 15:04
  • I see, I misunderstood the question a bit it would seem. I would use "User" as the name of my class in the Model (Just like in my Person example above). In a typical MVC implementation I would not need a User class defined in the View or the Controller. Commented Jun 5, 2018 at 18:32
  • so what would be the name of the class that "control" the user or the name of the class that show the user details? Commented Jun 6, 2018 at 7:35
  • That answer depends on the specific framework of MVC you are using and what you are trying to do. For example, in Microsoft MVC, your controller class would be named something relevant to the web application. For example: "HomeController" or "ReportsController". Within that class you might have action method called "Index" or "UserDetails", etc.. Some more examples: docs.microsoft.com/en-us/previous-versions/aspnet/… Commented Jun 6, 2018 at 18:36
  • I guess I should refute the accepted answer as well. UserController very well could exist as a class name. If you have a path in your web application Http://www.somesite.com/user* you could have a "UserController". There is absolutely nothing against it. There is no "engineering standard", de facto or otherwise, for controller class names that I know of. As with many things Software "Engineering" related you have to use your best judgement based on the business case, technology available and your experience & training. Commented Jun 6, 2018 at 18:46
1

In general, giving the same name to multiple classes in the same project (not package, project) is a bad practice. Among other bad things that can happen, if these things need to refer to one another, it becomes ambiguous which, for example, "User" you are trying to instantiate when you create a new User.

In MVC, the way I've usually seen it done is your package is named com.MYNAME.PROJECTNAME, and then add .MODULENAME for each module (in your case, "model", "view", "controller", etc)

As for classes, your class name should say what the class is. If it's a Controller for your User object, then it should be called UserController. This is true even if the package is the "controller" module; when looking at code in an IDE, it is easy to mistake or forget which package you're looking at for any given file.

2
  • Thank you for your answer, I have the last question, if one of my package name is "configuration" , is better for example, to have ServerConfiguration.class or Server.class? Commented Jun 5, 2018 at 14:21
  • ServerConfiguration.class
    – Ertai87
    Commented Jun 5, 2018 at 14:44

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