Last active
July 1, 2016 09:42
-
-
Save sagrawal31/713a86d52ff862babe42e6e1a5e4140c to your computer and use it in GitHub Desktop.
A simple code standard for our Groovy/Java code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Must have copyright statement either of the company or of our client. | |
* Follow other files in the repository. | |
*/ | |
package com.wp.test // package declaration just after copyright statement | |
// One line space after package declaration | |
// Import statement must be organized. Use organize tool in GGTS | |
import java.text.DateFormat | |
import com.cc.user.User | |
// One line space before class declaration | |
/** | |
* Put the class description and javadoc here. | |
* | |
* @author shashank // author | |
* @since 3.1.4 // When this class is introduced | |
*/ | |
class MainTest { // There must be a space between class name and curly bracket | |
// Insert one line space after starting of the class declaration | |
/** | |
* Java constants must be all UPPER case and multi words must be separated by underscores. | |
*/ | |
static final BASE_ID = "UID" | |
// Don't use "def" when you are sure about data type | |
static def somethingGood(int i) { | |
return i | |
} | |
/** | |
* Every method must have description and Javadoc. | |
* Also if this method was introduced after some time of introducing this class, | |
* then Javadoc for this method must have @author & @since properties. | |
*/ | |
static void testNow() { // Space after method declaration and curly bracket | |
User userInstance = User.get(2) | |
// If either user is missing or their full name is not set (example of inline comment which starts with space) | |
if (!userInstance || !userInstance.fullName) { | |
return null | |
} | |
// This code is not required (an example for inline comment code without space) | |
//userInstance.email = "newemail@gmail.com" | |
int i = 0 // There must be space at both sides of equal sign on any operation | |
while (i < 4) { // There must be space after any Java statement and also space before curly bracket | |
println("This is okay") | |
/* | |
* Use normal Javadoc with single starting quotes. Since these kind of Javadoc will not be | |
* imported when we generate HTML doc for these. | |
*/ | |
// No space for ternary operator | |
i++ | |
} | |
} | |
// There must be no spaces or tabs present in empty line and there must be only one line space between two combinations of method or variables | |
/** | |
* Try to use reference of custom classes in Javadoc, like given below. | |
* | |
* @param userInstance instance of {@link com.cc.user.User User}. Read more on Javadocs | |
* @param email new email of the user | |
* @return formulated email of the user | |
*/ | |
private String anotherFunction(User userInstance, String email) { // There must be spaces after each argument and argument name must follow variable conventions | |
for (int i=0;i<5; i++) // Must have spaces after operators and semicolons | |
{ // This bracket must start after previous line with a space | |
println("above 2 lines are totally wrong.") | |
} | |
// Brackets are optional in Groovy while calling a function but we need to use brackets to increase readability | |
somethingGood 5 // This is bad | |
somethingGood (5) // This is again bad | |
somethingGood(5) // This is good | |
// Space after "if", before "else" | |
if (true != false) { | |
} else if (true) { | |
} | |
// Use proper spacing | |
User userInstance = new User([ | |
email: "admin@example.com", | |
firstName: "John", | |
lastName: "Doe" | |
]) | |
// Return statement are optional in Groovy but you must use return statement to increase code readability | |
return "new.formulate.email@example.com" | |
} | |
/** | |
* Although, Groovy doesn't respect Java generics but try using it every places where possible. | |
* This helps in faster writing of code if you are using GGTS | |
*/ | |
List<String> getNames() { | |
List<String> nameList = [] | |
// This is problematic | |
(1..100).each { // Groovy closures uses "it" by default. Avoid using "it" variable and define some variable, this may cause some problems (see below) | |
nameList << "Name $it" | |
} | |
// This is absolutely wrong | |
(1..100).each { | |
["a", "b"].each { | |
nameList << "Name $it" // You will not be sure that "it" pointing to which loop | |
} | |
} | |
// This is correct | |
(1..100).each { number -> | |
nameList << "Name $number" | |
} | |
return nameList | |
} | |
// Try to keep variables, functions organized if you are adding a new class file (for example). Use GGTS code organizer | |
// Variables must be in lower camel case | |
float commission_value // Underscores are rarely used by us but besides constants they should't be used in Groovy/Java at all. | |
float commissionValue // | |
String email | |
// Don't use short names or names must be meaningful | |
String f_name | |
// Do use short names or names that are meaningful | |
String fullName | |
// All indentation must be of 4 spaces not a tab | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment