2

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?

  • possible duplicated stackoverflow.com/questions/3226282/… – anion Jun 5 '18 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 Jun 5 '18 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. – Berin Loritsch Jun 5 '18 at 13:13
  • 1
    @Kayaman, The model would be the User, but the view and controller should definitely be named differently. – Berin Loritsch Jun 5 '18 at 14:48
5

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.

  • 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? – Liz Lamperouge Jun 6 '18 at 7:33
  • 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. – Robert Bräutigam Jun 6 '18 at 10:38
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.

  • 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? – Liz Lamperouge Jun 5 '18 at 14:21
  • ServerConfiguration.class – Ertai87 Jun 5 '18 at 14:44
1

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();
  • 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 – Liz Lamperouge Jun 5 '18 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. – Robert McAtee Jun 5 '18 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? – Liz Lamperouge Jun 6 '18 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/… – Robert McAtee Jun 6 '18 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. – Robert McAtee Jun 6 '18 at 18:46

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.