Friday, January 10, 2014

Java naming conventions

In this post, we look into the basic naming conventions that everybody should know. The basic thing is how to write class names, variables, interfaces and methods name as per oracle standard naming conventions.




In professional environments, the benefits of coding standards are readability, maintainability and compatibility. Any member of a development should be able to read the code of another member. The coder who maintains a piece of code tomorrow may not be the coder who programmed it today. In addition, today’s enterprise solutions are so complex that multiple development teams unite to build a singular enterprise software application. With coding standards, distinct teams can rely o­n the way that they can interface with the code built by a separate team.

Classes and Interfaces
The first letter should be capitalized and if several words are linked together to form the name, the first letter of the inner words should be uppercase.
This sometimes called "camelCase".

  • Try to keep your class names simple and descriptive.
  • Use whole words-avoid acronyms and abbreviations 
Example of class:
Dog
Account
Factorial
PrintWriter

Example of interface:
Runnable
Serializable
FlyingInterface



Methods
The first letter should be lowercase, and then normal camelCase rules should be used.

Example:
run()
runFast()
getBalance()
doCalculation()
setCustomerName()


Variables

  • Like methods, the camelCase format should be used, starting with a lowercase letter.
  • Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.
  • Variable names should be short yet meaningful.
  • The choice of a variable name should be designed to indicate to the casual observer the intent of its use.
  • One-character variable names should be avoided except for temporary "throwaway" variables.

Example:
int i;
float buttonWidth;
long accountBalance;


Constants
Java constants are created by marking variables static and final. They should be named using uppercase letters with underscore characters as separators.

Example:
static final int MIN_WIDTH = 4;
static final int MAX_WIDTH = 999;
static final int MIN_HEIGHT = 40;
static final int MAX_COUNT = 1000;



If you know anyone who has started learning java, why not help them out! Just share this post with them. 
Thanks for studying today!...

2 comments: