Coding Style Guide

General Style

  • Config your IDE or editors to use white space instead of hard tabs.
    • Eclipse
    • Window->Preferences->Editors->Text Editors->Insert spaces for tabs
    • Window->Preferences->Java->Code Style->Formatter->Edit->Indentation-> Tab policy = "Spaces Only"
    • Existing tabs in the file will prevent spaces in new lines created with Enter.
    • Sublime Text: Preferences -> Settings User ->
    "tab_size": 4,
    "translate_tabs_to_spaces": true,
  • Notepad++: Settings -> Preferences -> Tab Settings -> "Replace by space"
  • Line length in Eclipse
    Window->Preferences->Java->Code Style->Formatter->Edit->Line Wrapping->Max line width: 100
  • Indent for wrapped lines
    Window->Preferences->Java->Code Style->Formatter->Edit->Line Wrapping-> Default indentation for wrapped lines: 1
  • White spaces
  • Separating any reserved word from an open parenthesis ( that follows it on that line, if, for, catch, while.
  • Separating any reserved word from a closing curly brace } that precedes it on that line, such as else or catch
  • Before any open curly brace {
if (a = b) {
       // code
} else {
       // code
}
  • On both sides of any binary or ternary operator.
int name = "Mary"
foreach (int num : array)
  • After ,:; or the closing parenthesis ) of a cast
void funcName(int foo, int bar)
  • Braces
  • Braces are used with if, else, for, do and while statements, even when the body is empty or contains only a single statement.
  • No line break before the opening brace.
  • Line break after the opening brace.
  • Line break before the closing brace.
  • Line break after the closing brace, only if that brace terminates a statement or terminates the body of a method, constructor, or named class. For example, there is no line break after the brace if it is followed by else or a comma.
return new MyClass() {
        @Override public void method() {
            if (condition()) {
                try {
                    something();
                } catch (ProblemException e) {
                    recover();
                }
            } else if (otherCondition()) {
                somethingElse();
            } else {
              lastThing();
            }
        }
};    
  • Line wrapping
  • The prime directive of line-wrapping is: prefer to break at a higher syntactic level
  • When a line is broken at a non-assignment operator the break comes before the symbol.
  • When a line is broken at an assignment operator the break typically comes after the symbol
  • A method or constructor name stays attached to the open parenthesis ( that follows it.
  • A comma , stays attached to the token that precedes it.
        myResult = this.thisIsAVeryLongMethodNameBlaBlaBlaBlaBla(
            parameter1, parameter2, parameter3, parameter4, parameter5, parameter6,
            parameter7, parameter8, parameter9, parameter10, parameter11, parameter12);
  • Exceptions
    • Lines where obeying the column limit is not possible (for example, a long URL in Javadoc, or a long JSNI method reference).
    • package and import statements
    • Command lines in a comment that may be cut-and-pasted into a shell.

[Google Java coding style guide]
(https://google.github.io/styleguide/javaguide.html)

SQL

  • Bind parameters in SQL queries, don't use literal values.
  • Prefer PreparedStatement over Statement in JDBC
sql = "SELECT t.name FROM hr.employees t WHERE employee_id = ?";
statement = connection.prepareStatement(sql);
System.out.println("Start: " + new Date());
for(int i=0; i<10000; i++) {
        statement.setInt(1, i);
        resultset = statement.executeQuery();
        if(resultset.next()) {
            name = resultset.getString("name");
            doSomething(name);
        }
        resultset.close();
    }            
System.out.println("End: " + new Date());
statement.close();
  • Bind variable in sql
  • Hibernate code
    Don't do this:
String endShowDateSql = "TO_DATE('"+endShowDate+"', 'Month dd, YYYY HH:MI:SS AM')";
sql = StringUtil.replace(sql, "[$END_SHOW_DATE$]", endShowDateSql);
sql = StringUtil.replace(sql, "[$DMA_CODE$]", dmaCode);

Do this:

SQLQuery q = session.createSQLQuery(sql);
q.addScalar("Id_", Type.LONG);
q.addScalar("rank", Type.FLOAT);
QueryPos qPos = QueryPos.getInstance(q);
qPos.add(classNameId);
qPos.add(ArrayUtils.toPrimitive(siteIds.toArray(new Long[siteIds.size()])));
qPos.add(companyId);

你可能感兴趣的:(Coding Style Guide)