Java Coding Conventions

Welcome to second tutorial of Java tutorial series, In this tutorial we are going to see the java coding conventions that make coding more fun and also make each line of code more readable. Following these coding conventions not only make a readable code but also help other fellow programmer to walk through code with ease of better understanding of code. but if you want to to read to read about Java then move to Introduction to Java i.e first tutorial of this series.



Wikipedia define Coding convention as

“Coding conventions are a set of guidelines for a specific programming language that recommend programming style, practices and methods for each aspect of a piece program written in this language.”

So we can see that it is set of guideline but does it is mandatory. Then my answer is no but remember coding conventions always make life more easier. So let’s see the reason why do we need to learn Java Coding Conventions-

  • Code Readability
  • Ease to grasp the code
  • Clean coding and better packaging of product.

But before moving ahead let’s remember few much talked component of Java these are

  • Class
  • Interface
  • Object
  • Variable
  • Method
  • Constructor
  • Comments

Wait for a minute, Do you have any idea, how you are going to differentiate one component from another.  if you follow the same kind of coding pattern all around the complete application code… Wait wait again think one more minute and think, if you get the opportunity to walk through the code and get opportunity to add one class that is interacting with old code, then might be it will be nightmare for you because you will take a lot of time to just walk through the code, because each line of code would look more like the other line of code . So it becomes mandatory to follow certain rule while coding so that even a naive java programmer could walk through the code and can understand it. So let’s  jump in to the list of Java Coding Convention that most of the people try to imbibe in their java code.

1- File extension of Java file :
Java file has extension .java and byte-code will have .class and to use all these classes we can have java archive file with name .jar.

2- Comment in Java 

  1. Single line comment : For single line comment, one can use “//” and what every we would write after this would be ignored by compiler Example:

or use following

Example

3- Methods: Method should start with lower case and if method contains multiple words then each inner words first letter should be in caps or in Upper Case. Along with this we should try to keep the name of method as verb.

Example:

Format of writing method

4- Classes: Each class should start with first letter in Upper and again like method, if class name is made up of multiple words then each inner word’s first letter should be in upper case.

Example:

Format of class declaration:

5- Constructors: It will always be same as Class name. Constructor does not have any return type that’s why we never write anything like void/any primitive data type as return type.




6- Variables:  It should start with lower case and like method if variable is made up of more than one words and first letter of each inner word would be in Upper Case. We should try to keep short name for variables and should avoid multiple word in variable name in code convention , both variable and methods seems similar only parentheses bring difference between a variable and method.

Example:

Example:

8- Constants: Constant should be in complete Upper Case ex: SUNDAY, MONDAY
9- Structure of a Java Class

a- Beginning comment

b- Package name and import statement

c- Class or interface declaration

d- Inside class order of variable

i- Static variable should come first

ii- public variable

iii- protected variables

iv- private

e- After variable declaration, Constructor should be declared

f- Inside class, method should be in functional order there is no ordering of method on the basis of scope or access modifier.

ex: Java generic class

10- Each line of code should terminate with “;”

11- For Readability purpose, try to keep each line at max 80 characters because many terminal does not support more than this.

12- For readability we should wrap line of code if they are going beyond 80 characters

Some thumb rules for wrapping lines

  • Break after a comma.

Example:

  • Break before an operator.

Example:

13- Single declaration in a line increase better readability and also support comment associated with declaration.

Example:

1- Two blank line

a- Between a class definition and interface definition

b- Between two functional sections in a single class file

2- Single blank line

a-  Before each single line comment

b- Between two methods

c- Between variables, methods and a single line comments

d- Between logical sections of code inside method for better readability.

3- Single space should be present between each binary operator except. , and also should be avoided with unary operators.

4- Single space should be present between parentheses with Java Keywords.
ex: while () and one space should also be present in method name and parentheses.

So now we have completed a list of coding convention of Java. So enjoy writing your Selenium WebDriver using Java.

So wish you all a very happy coding!!!

We have tried our best to keep all information in a single mind map. So please take your own cheat-seat

Java Code Convention

 

Read Similar Posts



One comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.