Coding Convention- Tutorial 1

In today’s world there is always a need of good programmer who can write the good quality code. The good quality code should be a Reusable, Readable and understandable. But now a days, the biggest problem which is being faced by the developers is the proper coding convention for writing their application code as no one tells them the proper way to write the code. They have knowledge of code convention but not in proper way, this tutorial will helps you in understanding more about the code convention. In this tutorial we will guide you about the proper use of code convention like what is the benefit of using code convention, when they have to use, where and how to use proper code convention.

Code conventions are important to programmers for a number of reasons:

  • Maintenance
  • Improve the readability of the software
  • Understand new code more quickly and thoroughly.
  • Well packaged and clean

Code convention is nothing it is the combination of reusability, naming convention, readability etc. For writing a good program we have to remember few point. Code convention is very important for our programming, because we need to follow the conventions in each line of program like class name, method name, comments, spaces etc.

Let’s explain this with an example. We will explain the concept using Java. It may differ for other language.

Program 1:- Write a java program to find a number is odd or even.

Solution 1.1:-

class Test {
	public static void main(String ar[]){
		int x = 8;
		if(x % 2 == 0)
			System.out.println("Number is EVEN");
		else
			System.out.println("Number is ODD");
	}
}

Solution 1.2:-

public Class TestOddEven{
	public static boolean isEven(int number){
		return (number%2==0);
	}
}

class Main{
	public static void main(String ar[]){
		Scanner scanner= new Scanner(System.in);
		int number = scanner.nextInt();
		if(isEven(number))
			System.out.println("Number is Even");
		else
			System.out.println("Number is Odd");
	}
}

We have given the two solution for the above problem, first solution is simple and short . But another solution is reusable and more understandable code using the code conventions. The above two solution have differences. Let’s discuss those differences one by one.

  • You will say that Sol1.2 is lengthy but as a good program you have to give more attention on program reusability, readability, proper naming convention instead of lines of code.
  • Now main points that need to remember are as follows :-
    • Take a proper class name. there should be one main class in every application. In main class, there should be a Main() method which is the starting point of the application.
    • You should define a separate function for each functionality with a proper name. Name should be understandable and method should be reusable so that we can use it anywhere.
    • Use proper Indenting, Comments and Spaces.
  • Sol1.2 is easy to understand, Other programmer can easily understand the functionality of program.
  • Sol1.2 is dynamic program. you can send any value and it will return a correct result.
  • Few users may say that the Sol1.2 will increase the number of lines of code. I also agree with their point that it will increase the number of lines but if you see in terms of an application it will not increase the lines of code as this program is using proper coding convention. We will explain my point with an example, Suppose in your application, If you want to check number again and again. Then according to Sol1.1, you have to write whole program again and again in your application where you have to use that functionality. But according to Sol1.2, you just have to make a call to the method and it is easy to find class and method with the help of proper naming convention
  • Now the point comes how to give proper and meaningful name to a method. Like we have taken method name isEven(int number) and data type is Boolean. Below are the point which should be considered while giving the method name.
    • Suppose you want to check the result in term of TRUE/FALSE then you should start your method name from ‘is’ keyword.For example :- isPass(), isEven(), isChar(), isNumber() and so on.
    • Suppose you want to do some operation and expecting a value from the function then you should start your method name as ‘get’. For Example: – If you want to calculate the sum of two number then your method name should be:-getSum(int num1, int num2);

Remember you should avoid using name like isPASSFail() because we want only true or false in result but the function name will create a confusion that condition is PASS or Fail. So be careful to while giving method names.

We will explain you about comment, line spaces, and give you more examples in which we will explain you more about re-usability and readability in our next post. Keep visiting to see update.

Swati Kashyap
Swati Kashyap

Latest posts by Swati Kashyap (see all)

18 Comments
  1. Ankit Agarwal
    • admin
  2. Julius
    • admin
  3. north face blouson
    • admin
  4. parajumpers jakke
  5. pjs jacketparajumpers stockholm
    • admin
  6. pjs doudoune prix
    • Swati Kashyap Swati Kashyap
  7. Whitney
  8. parajumpers vinterjakke
  9. parajumpers vest
  10. Parajumpers Long Bear Dam
    • Swati Kashyap Swati Kashyap
  11. cheap car insurance quotes online

Leave a Reply