The coding standards are important because most of the software cost goes towards maintenance. And also, the software is not always developed by a single developer. Therefore, maintaining a convention for writing software increases the readability of the program.
Naming Conventions :
Names representing packages should be in all lower case.
Class/enum names must be nouns and written in PascalCase.
Variable names must be in camelCase.
Constant names must be all uppercase using underscore to separate words (aka SCREAMING_SNAKE_CASE).
Names representing methods must be verbs and written in camelCase.
Abbreviations and acronyms should not be uppercase when used as a part of a name.
All names should be written in English.
Boolean variables/methods should be named to sound like booleans (e.g. isSet, isVisible).
Plural form should be used on names representing a collection of objects.
Associated constants should have a common prefix.
Layout and Indentation :
Basic indentation should be 4 spaces.
Tab-stops should be set exactly every 8 spaces.
Apply indentation to alike items in a vertical list (such as end-of-line comments, and identifiers in declarations).
Surround the binary operators (including assignment) by spaces.
Add a space between a keyword(“if”, “while”, “return”, “catch”, “switch”, “for”) and a succeeding parenthesis.
Surplus parentheses can also help to highlight the structure of expressions (but avoid using too many nested parentheses).
Place line break to improve readability.
Operators, reserved words, commas, colons, semicolons should be followed by a space character.
No blank lines should be present after the opening brace or before the closing brace.
A curly brace is applied at the end of the line that starts the class, method, loop, etc., and the closing brace is on a line by itself, lined up vertically with the start of the first line.
Get Jaweria Jawed’s stories in your inbox
Join Medium for free to get updates from this writer.
Package and Import statements :
Put every class in a package.
The ordering of import statements must be consistent.
Imported classes should always be listed explicitly.
Variables, Loops & Conditionals :
Variables should be initialised where they are declared and they should be declared in the smallest scope possible.
Class variables should never be declared public unless the class is a data class with no behaviour.
The loop body should be wrapped by curly brackets irrespective of how many lines there are in the body.
The conditional should be put on a separate line.
Single statement conditionals should still be wrapped by curly brackets.
Comments :
Comments should contain only the information that is relevant for reading and understanding the program.
Implementation comments are delimited by // or /*…*/ .
Block comments are used to provide descriptions of files, methods, data structures, and algorithms. Block comments may be used at the beginning of each file and before each method or within methods.
Single-line comments can appear on a single line indented to the level of the code that follows. If a comment can not be written in a single line, it should follow the block comment format.
Trailing(very short) comments can appear on the same line of the code they describe but should be separated from the code at a far off distance.