Coding Guidelines in Java
Java is the most popular and widely used object-oriented programming language. It provides a platform to developers for developing variety of applications like web, desktop application, games, etc. The reason of using Java programming language is that it provides security, reliability, and also fast. Writing code in Java is a bit difficult because it follows number of rules and guidelines that we must follow for better readability.
What are coding guidelines?
In Java, the coding guidelines are the set of rules that are followed by the developer during application development. These guidelines provide readability to other developer and user who are dealing with the project. The guidelines must be followed because an application is not developed by a single programmer. Some major coding guidelines includes the following:
These are the rules for naming variables, methods, constants, classes, interfaces, etc. Generally, Java follows the camel case convention. It describes the following:
The use of curly braces is most important in programming. It defines the body of the class, methods, and loop. The two standard uses of curly braces are as follows:
The indentation should be 4 spaces. By pressing the Tab key, we get exactly 8-spaces. Indentation can be achieved by the space character and the tab characters.
The recognized standard for increasing readability of each line is:
It also plays a vital role in readability of the program. Any mathematical operations (+, -, *, %, etc.) should be surrounded by a space character. For example,
Keywords that reserved in Java must followed by white spaces. For example:
Always, put a space just after comma. It means comma mut followed by a white space.
Colons should be surrounded by white space. For example, the case statement must be written as:
Semicolons in for statements should be followed by a space character. For example: The for loop must be initialized as:
Comments in programming, is very important because it enhance the readability of the program. It contains the relevant information regarding program. Note that comments are ignored by the compiler.
Why do we use comments in a code?
Java provides two types of comments:
It is also started by two forward double slashes. Java also allows us to use /**/ for comments. It is used if we do not want to execute a chunk of code. There are four types of implementation comments:
block, single-line, trailing, and temporarily removing code.
Block comments provides the description of the method, data structure, and algorithm. Generally, it is used at the starting of file, before or within each method. There must have a blank line before a block comment have started, unless it comes immediately after starting of the compound statements.
The single-line comment is used to comment only one line of the code. It is the widely used and easiest way of commenting the statements.
Single line comments start with two forward slashes (//). It is used when a comment cannot be written in a single line. Any text in front of // is not executed by Java.
Trailing (short) comments are the comments that can be write on the same line but should maintain a far from the code. If more than one short comment appears, they should all be indented to the same tab setting. For example:
Temporarily Removing Code
It is used to comment a partial or a complete line. We can also use it in multiline comment. Note that it is used only when the code is under development and we want to remove a particular chunk of code. The unused code should eventually be physically removed as it can make the source more difficult to maintain. For example:
Documentation comments are usually used to write large programs for a project or software application as it helps to create documentation API. These APIs are needed for reference, i.e., which classes, methods, arguments, etc., are used in the code.
To create documentation API, we need to use the javadoc tool. The documentation comments are placed between /** and */.
Next TopicCouple Holding Hands Problem in Java