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?
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?
Conventions are not mandatory but help
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).
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).
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.
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.
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.