Coding Standard - Msadaf/Cse327.6T2 GitHub Wiki
Snake Casing In JAVA
Snake case is the practice of writing compound words or phrases in which the elements are separated with one underscore character (_) and no spaces, with each element's initial letter usually lowercase within the compound and the first letter either upper- or lowercase. For example, "foo_bar" and "Hello_world".
In Java, snake case uses for static final constants and enum values.
Given a sentence, task is to remove spaces from the sentence and rewrite in Snake case. It is a style of writing where we replace spaces with underscore and all words begin with small letters.
Example:
Input : Lower case with underscores
Output : lower_case_with_underscores
Naming Conventions for java
- PACKAGES: Names should be in lowercase. Ex : anyname
- CLASS : Usually class name should be noun starting with uppercase letter. If it contains multiple word than every inner word should start with lowercase and underscore between them. Ex : String, String_buffer, Dog.
- INTERFACE : Usually interface name should be adjective starting with uppercase letter. Ex: Runnable, Serializable, Comparable
- METHOD : Usually method name should either be verb or verb noun combination starting with lower letter. If it contains multiple word than every inner word should start with lowercase and underscores.Ex: print(), sleep(), set_salary().
- VARIABLE : Usually variable name should be noun starting with lowercase letter. If it contains multiple word than every inner word should start with lowercase and underscores. Should not start with underscore (โ_โ) or dollar sign โ$โ characters. Ex: name, age, mobile_number.
- CONSTANTS : Usually constant name should be noun. It should contain only uppercase If it contains multiple word than words are separated with ( _ ) underscore symbol. Usually we declare constants with public static and final modifiers. Ex : MAX_WIDTH.
Layout Conventions for java :
- Avoid line lengths longer than 80 character.
- Indent a fixed number of spaces. We recommend 4.
- Use parenthesis to contain list of parameters in method.
- Use parenthesis () after keywords if, else, do, while, for and foreach.
- Use braces {} to define a block of code, for classes, methods and local scopes.
- Use brackets [] to declare array types and use when dereferencing array values.
- Use semicolon ; to terminate statements.
- Use comma , separate consecutive identifiers in a variable declarations.
- Use comma , to chain statements together inside a for statement.
- Use colons :: to create a method or constructor references.
- Use period . to separate package names from subpackages and classes and also use to separate a variable or method from a reference variable.
- BLANK LINES :
- Between sections of a source file.
- Between class and interfaces definition.
- Between methods.
- Between the local variables in a method and its first statement.
- Before a block and single-line comment.
- Between logical sections inside a method to improve readability.
- Don't put more than one statement on a line.
- BLANK SPACES :
- Appear after commas in argument lists.
- All binary operators except . should be separated by operands byspace. but spaces never use in unary operators such as unary minus, increment (โ++โ), decrement (โ--โ).
- The expressions in a for statement should be separated by blank spaces.
- Casts should be followed by a blank.
- A keyword followed by a parenthesis should be separated by a space.
- Do not put spaces before a semicolon.
- Do not put spaces between an object name, the . separator, and amethod name.
- Do not put spaces between a method name and its left parenthesis.
- COMMENTS :
- Multi-line comments : This type of comments must begin with /* and end with*/ . Ex: /* This is a java program.Call this file โexample.Javaโ */
- Single-line comments : It begins with // and ends at the end of the line. Ex: int num ; // this declares a variable called num.
References: