Hello there, I hope you and your loved ones are doing great in these unprecedented Times of COVID-19.
In this blog, I have tried to cover the Naming Convention that all Dev follow while doing the coding. I'll be explaining all the conventions by taking examples in java language...but this convention is general and can be extended to all other languages and frameworks.
Let's get started!!!!
What is naming convention??
The naming convention is a rule to follow as you decide what to name your identifiers such as class, package, variable, constant, method, etc.
But, it is not forced to follow. So, it is known as convention not rule. These conventions are suggested by several Java communities such as Sun Microsystems and Netscape.
All the classes, interfaces, packages, methods and fields of Java programming language are given according to the Java naming convention. If you fail to follow these conventions, it may generate confusion or erroneous code.
The First question that came up to mind is Why is it necessary to use This Conventions???
Programming is a story. every character and scene needs to have a name that properly tells you what it is and what it does. You should be careful while naming things: You don’t want to make your code hard to read or debug. Remember: You are the creator here. You can name anything from hills, stars, trees, and mountains, almost anything (almost). Paying little attention to this feature will make your code more readable, easy-to-debug, and whatnot. And you should be able to look at it after months and be able to understand it without any trouble.
Naming Convention for Packages:
- Always Lower case
- package name should be unique.
- Use ur net name, reversed, as a prefix for the package. e.g:- com.Arshad_ali is the package name
Few More Examples are:
com.coderstea.whatsinaname com.coderstea.whatsinaname.innerpkg com.coderstea.whatsinaname.innerpkg.anotherpkg com.coderstea.whatsinaname.innerpkg2 com.coderstea.anotherproject com.coderstea.anotherproject.v2
Naming Convention for Classes:
- Camel case
- Class name should be Noun(Represent things)
- Should start with the capital name.
- Each word in the name should also start with a capital letter.(EX;- LinkedList)
- Use whole words and must avoid acronyms and abbreviations.
Few Examples are: class Student{} class ArrayList{} class HashMap{} class ComputerEngineer{}
Naming Convention for Methods:
- Mixed case with the first letter lowercase
- Often Verbs
- Reflect the function performed or the result returned.
Few Examples are:
void print(Object obj); void remove(Obejct obj); Object update(); int getCountOfCustomer();//getter void setCountOfCustomer(int countOfCustomers);//setter boolean isUserAdmin(User user);
Writing handNaming Convention for Variables:
- short yet meaningful
- camel-casing.
- Should be mnemonic i.e, designed to indicate to the casual observer the intent of its use.
- Variables can also start with either underscore('_') or dollar sign '$' characters.(NOT A GOOD PRACTICE)
EXAMPLE:- int countOfCustomer; float averageInterest; long timeInMillisecond, daysInYear; //with some unit boolean isEngineer, isCompleted, hasSubmitted; // questions
Naming Convention for Constant:Keyboard
- All Upper case.
- separated with an underscore.
- There are various constants used in predefined classes like Float, Long, String etc.
e.g:- int MAX_VALUE; public static final float PI = 3.14; static int CREATED_ON_YEAR = 2019;
Thanks, I hope you enjoy my little tiny effort to bring back to the community. Plz, share if you found the content inside it useful.
You can Connect me on Twitter:- twitter.com/arsh14_ali