Coding Standards in java

Ayush Kumar
5 min readJun 7, 2021

It's an art to writing code neatly which is readable and easy to maintain.

Need of it?

Usually in an organization, a team of software developers works on a specific project and is usually developed and maintained over years and that's necessary that standard must be followed and maintained. As many people will be involved in the process and if everyone will write according to themselves then its a mixture of n-number of different styles and will be very difficult to understand by other people and the entire project will become clumsy and hard to read. So standardizing is the way so everyone will be on the same style and pattern.

So let's see whats are these standards-

1. Identifiers

Identifiers

These are commonly known as variables. So as the name suggests “Identify” the variables are named in such a way that it should give the meaning of what they are storing.

Now, again if see the above image you can see the there are two variables “myName” and “myMood” what do you understand from these name what they store? a name and mood? So that is 1 st rule they must represent what they are storing. so they must be meaningful and must represent what they store.

Now again if we see “myName” as you are reading most likely your name may or may not be Ayush. so if you are reading this then most probably “my” is not the correct word for naming this variable.

identifiers (b)

How about this? does this solve the purpose? either you read it or I will read it or any person read it means same the author’s Name. so this is also important the variables should remain the same from everyone's perspective. Avoid using a variable like myVariable.

Now once we know how to name a variable then the next stuff is syntax. So we should write the variable as in the lower camel case. i.e. first word as the lower and afterward first letter of each word must be capitalized. example -

identifiers (c)

So here is the correct way to write it. so we will move towards Methods i.e function in java.

2. Methods

Now for method means we are doing some task so the Verb in the name of methods should represent that action.

As you can see for void return type its verb is show and another function where we want something from method so it's advisable to use get.

So these verbs should represent exactly what function’s action is.

So let's talk about syntax -

a) Lower Camel case should be followed.

b) after method definition there must be a single space in between {

showAuthorBlog() {

getAuthorContactDetails(int userId) {     //correct
getAuthorContactDetails(int userId){ // wrong - no sapce

c) No space between ( and argument list

getAuthorContactDetails(int   // CorrectgetAuthorContactDetails ( int // Wrong  - Space givengetAuthorContactDetails( int  // Wrong  - Space givengetAuthorContactDetails (int  // Wrong  - Space given

d) No trailing space in between argument and closing )bracket.

getAuthorContactDetails(int userId) {    // CorrectgetAuthorContactDetails(int userId ) {   // wrong

e) There should be proper spacing between multiple arguments.

getBookPrice(String bookName, String language) {   //CorrectgetBookPrice(String bookName,String language) {    // WronggetBookPrice(String bookName,  String language) {  //WronggetBookPrice(String bookName ,String language) {   //Wrong

3. CLASS

So all methods and variables will be defined in some Class so we have a convention for representing that too.

Classes

It should be in the Upper Capital camel Case. eg -

public class Match {    // Correctpublic class Matches { // Correct IF its representing 1 match only public class match {    // Wrong first letter must be capitalizepublic class PlayerDAO{    // Correct for Dao classespublic class PlayerDao{    // Correct for Dao classespublic class Playerdao{    // wrong for Dao classespublic class Player{    // Correct for Dao classes 

4. packages

packages

It must be in small letters only.

And it's written in reverse order like

com.ayush.web.controller 
com.ayush.web.util // all are correct
com.ayush.web.model
com.ayush.web.dao
com.ayush.web.Model // This is wrong

5. if-else

if-else block

rules-

if (condition   // must have space between if & (if (condition   // Must not have space in between (conditionif (condition ==  // space req between conditiom and  ==if (condition == true)  // same for another condition== true) {   // space after ) and { is required
if (condition) { //body} // exactly below if
if (condition) {
} else { // it should starts from same line

6. Loops

for loops

Rules for loops also remain the same as all above the same spacing and bracket rules.

Hope You like this blog !! Do Clap and comment.

--

--