Monday, September 22, 2008

Naming conventions for class, variables and functions

This post is related to the naming conventions of the identifiers that we are declaring in the program. During programming the problems that I have faced in programming and have learnt from others how to do declaration of class, function and variables. So for that I am published this post to reduce the problems that I have faced that you don't have to face it.

Class Declaration:
Always declare class name with first character in the Capital letter. ie class Increment. If the class name contains two words such as "vishal" and other as "joshi" then declare class as class VishalJoshi. Give some useful name to the class so that one can understand that this class is doing some particular work; such as if a class is related to person's information class then declare it as class PersonInformation.


Variable Declaration:
In variable decalration, don't try to give useless name such as int i. Give some useful name such as if a variable is for temparary use decalre it as int temporary;. Now in such a way if a variable is used for declaring storing name of a person then decalre it as String personName, here put first letter of first word in small letter and first letter of second word in the capital letter, so that it becomes easy for others to understand the decalration of variable. Don't put underscore (_) in the declaration till possible and useless name; and even though the name of variable becomes too long let it be long.

Function Declaration:
In function declaration, give function name as void createListing(); same as variable declaration. But give some useful name to the function so that one can get idea of what the function is doing, such as for generating new random password declare function as
public String generateRandomPassword(). Here one can understand that this function is used to generat a random password. Here also don't put underscore (_) in between the words this is bad habit.


From this post you might have got some idea of declaring variables, classes and functions in your code. Due to this declarations your code becomes easy to understand for you and others also and you also don't have to do a lot of documentation for your code. If you'll declare variables using useless words then you also will have to face a lot of problem. So please give some meaningful name to the variables, functions and classes.




No comments: