1. Always have static variables before declaring the instance variables.
2. Every you have a constant value declare it as static final variable
example :-
3. If the constant is being used across application make it public
2. Every you have a constant value declare it as static final variable
example :-
private static final int ONE_HOUR_IN_MILLIS = 60*60*1000;
3. If the constant is being used across application make it public
public static final int ONE_HOUR_IN_MILLIS = 60*60*1000;Final variables can be initialized only once. We can declare a final variable without assigning any value and it is called 'blank final'. But it needs to get assigned a value. Other wise it will result in compilation error.
public class Test { public static void main (String args[]) { } } class A { private final String STR; A (String s) { STR = s; } }
4. When ever you want to initialize some value better to use null values and object data type as against primitive data type. For example:-
Long startTime; // is better than long startTime = -1;as -1 is a value just like any other long value where as null value is unique.
5. Try to initialize all instance variables within a constructor.
6. Break down the code into logically meaningful methods.
7. If you are using ResultSet. This following would be a better code to close it.
private long getEndTimeFromDB (int dataPkey) { long endTime = 0l; ResultSet rs = null; try { rs = getFullRecordFromDataTable(dataPkey); while (rs.next()) { endTime = Long.parseLong(rs.getString(END_TIME)); } } catch (Exception e) { Logger.error(e, "Error:"); } finally { if (rs != null) { try { rs.close(); } catch (Exception e) { // Ignore. } } } return endTime; }
8. From within a function, always have a single point of return. Avoid the following
private long getTimeStamp () { long returnValue = 0l; if (a>b) { return 19909888l; } else { return 788875565; } return returnValue; }
9. Code should be always clean a. Remove the unused variables. b. Remove the unused functions. c. Remove the unused imports.
10. Use standard naming for variables, first letter of the first word should be in lower case, the first letters of subsequent words should be upper.
11. Comments inside a function should be /* */
12. Use enums for constants, it has useful methods like equals(), valueOf()
13. Do not make a method a method unless, we really need to.
14. Whenever you use collections, either delaration or in method signatures always use the generic Interface.
For example :- Always use List to declare a collection, in that way a developer can decide which one they need to go with either ArrayList or LinkedList at the time of implementation.
15. Put String literal first for comparisons to avoid Null pointers
if("VALUE".equals(value)) {....}
16. For Collections always check it is not null and empty
Listlist; if(null != list && !list.isEmpty()) {....}
17. Use StringBuilder instead of String, if we know for sure that we are going to apply '+' on String.
18. Use entrySet() when you iterate a map and if you need both keys and values.
for (K key : map.keySet()) { V value : map.get(key); }
… rather than the following:
for (Entryentry : map.entrySet()) { K key = entry.getKey(); V value = entry.getValue(); }
19. Avoiding memory leaks
1. Always release database connections when querying is complete.
2. Try to use Finally block as often possible.
20. If you want to return more than one value from a method, it is best practice to create another class with the return values as data members, rather than using HashMap. Map is not type safe. Any body can put anything into a map. We usually use String as keys to map which is also not type safe, what if some body misspells the key when trying to retrieve the value from the map. This can cause logical errors and has the possibility of creating class cast exceptions.
21. Naming Conventions
http://www.oracle.com/technetwork/java/codeconventions-135099.html
22. Breaking lines
In general, break lines before operators, and indent the subsequent lines:
ListsurveyConfigEmailMappingList = SurveyConfigEmailMapping.find.newFinderFor("mob").asList();
To me, the leading operator clearly conveys that "this line was continued from something else, it doesn't stand on its own
No comments:
Post a Comment