1

I am learning Java and have found the naming conventions used in tutorials as well as on Oracle's website , com.etc.project.program

Do I really have to follow this convention?

and what are the pros and cons of following or not following it?

dhirwan
  • 49
  • 6
  • 1
    you had better follow these conventions if you want to provide a public API for other users – Andrew Tobilko Jul 28 '16 at 11:46
  • 2
    Possible duplicate of [Java package naming conventions](http://stackoverflow.com/questions/7411717/java-package-naming-conventions) – Tom Jul 28 '16 at 11:51

5 Answers5

4

Conventions are not mandatory but help

  • YOU to create bettter code and
  • OTHERS to understand it easier:

According wikipedia:

Reasons for using a naming convention (as opposed to allowing programmers to choose any character sequence) include the following:

  • to reduce the effort needed to read and understand source code
  • to enable code reviews to focus on more important issues than arguing over syntax and naming standards.
  • to enable code quality review tools to focus their reporting mainly on significant issues other than syntax and style preferences.
  • to enhance source code appearance (for example, by disallowing overlong names or unclear abbreviations).
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Thanks @Jordi , I don't know how I missed "others". For sure to interpret the names there must be a standard criteria. – dhirwan Jul 29 '16 at 13:05
0

No you don't need to follow any conventions, you can write everything that compiler allows.

But following some conventions will make it easier to understand your code by others (and by yourself in the future).

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
0

As you're saying in your question itself, it's just a "convention"

Convention is a way in which something is usually done.

It's only a convention. It's upto you if you want to follow it or not. But it's always recommended that you do follow these conventions.


Naming conventions in Java

NAME            CONVENTION
class name      should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc.
interface name  should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc.
method name     should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc.
variable name   should start with lowercase letter e.g. firstName, orderNumber etc.
package name    should be in lowercase letter e.g. java, lang, sql, util etc.
constants name  should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
0

The package naming conventions are just conventions. You can ignore them, but you shouldn't.

The conventions help preventing naming conflicts, and make your code more readable by others.

As soon as you start to collaborate with other developers, following the conventions will make your and their job easier.

And, they are really not difficult to follow.

Thomas Stets
  • 3,015
  • 4
  • 17
  • 29
0

There are other StackOverflow pages that have talk about this here and here.

But in case you haven't seen them, let me give you some reason and reference:

If you're just doing personal projects where nobody else will use the code, then you can make up a package name that you like. Don't make up something that starts with com. or net. or other top-level domain though, because that would imply that you own the domain name (ie. using com.john as your package name just because your name happens to be John is not a good idea).

If you're going to give the code to anybody else, you should use a globally unique package name, which according to Java conventions means you should register and use a domain name.

Community
  • 1
  • 1
Young Emil
  • 2,220
  • 2
  • 26
  • 37
  • Prefer to flag this question as a duplicate instead of copying answer from there. – Tom Jul 28 '16 at 11:52
  • Thanks @YoungMillie , got it, I really searched the same question but didn't reach to the right answer. – dhirwan Jul 29 '16 at 13:08