Скачать презентацию Top 10 Java Style Conventions Ed Gibbs Скачать презентацию Top 10 Java Style Conventions Ed Gibbs

c1207b3965e61eb6b658cb185a08daf4.ppt

  • Количество слайдов: 36

Top 10 Java Style Conventions Ed Gibbs Top 10 Java Style Conventions Ed Gibbs

Overview l All java development is supposed to follow The Elements of Java Style. Overview l All java development is supposed to follow The Elements of Java Style. l While 100% compliance isn’t necessary you should have a good reason. l Standardized code is easier to read and easier to maintain. l This is mostly a reminder presentation.

Indent nested code l Elements of Java Style #5 l Curly braces { start Indent nested code l Elements of Java Style #5 l Curly braces { start at the end of the line that introduces the project l Closing brace } goes at the end on it’s own line

Indent nested code l Bad examples: public void get. Name() { return this. name Indent nested code l Bad examples: public void get. Name() { return this. name } try { Employee. Manager. Dao dao = Employee. Factory. get. Instance(); } catch (Data. Exception e) { e. print. Stack. Trace(); }

Indent nested code l Good examples: public void get. Name() { return this. name Indent nested code l Good examples: public void get. Name() { return this. name } try { Employee. Manager. Dao dao = Employee. Factory. get. Instance(); } catch (Data. Exception e){ e. print. Stack. Trace(); }

Use meaningful names l Elements of Java Style #9 l Use a name that Use meaningful names l Elements of Java Style #9 l Use a name that will be meaningful for a developer who reads your code 2 years later. l Avoid single character variables. l Doesn’t apply to temporary counter variables like ‘i’ in a for loop.

Use meaningful names l Bad examples: Person p = new Person(); //… a lot Use meaningful names l Bad examples: Person p = new Person(); //… a lot of other code p. set. Id(1); Date d = p. get. Birth. Date(); Date cur = new Date(); boolean v = false; if (d. after(cur)) { v = false; } else { v = true; }

Use meaningful names l Good examples: Person person = new Person(); //… a lot Use meaningful names l Good examples: Person person = new Person(); //… a lot of other code person. set. Id(1); Date person. Birth. Date = p. get. Birth. Date(); Date current. Date = new Date(); boolean valid. Birth. Date = false; if (person. Birth. Date. after(current. Date)) { valid. Birth. Date = false; } else { valid. Birth. Date = true; }

Join the vowel generation l Elements of Java Style #12 l Abbreviations reduce readiblity. Join the vowel generation l Elements of Java Style #12 l Abbreviations reduce readiblity.

Join the vowel generation l Bad examples: private FBean exec(String ref) { // do Join the vowel generation l Bad examples: private FBean exec(String ref) { // do something } public void proc. Ln (String ln) { // do something }

Join the vowel generation l Good examples: private Loan. Package. Form. Bean execute. Update(String Join the vowel generation l Good examples: private Loan. Package. Form. Bean execute. Update(String loan. Reference. Number) { // do something } public String process. Line. Formatting (String line) { // do something }

Capitalize only the first letter in acronyms l Elements of Java Style #13 l Capitalize only the first letter in acronyms l Elements of Java Style #13 l Eliminates confusion when uppercase letters act as separators. l Especially important if two acronyms follow each other. l It’s not unusual to see code with some acronyms capitalized and others not.

Capitalize only the first letter in acronyms l Bad examples: set. WALTSSeparator(); create. Person. Capitalize only the first letter in acronyms l Bad examples: set. WALTSSeparator(); create. Person. DAO(); load. XMLReprentation();

Capitalize only the first letter in acronyms l Good examples: set. Walts. Separator(); create. Capitalize only the first letter in acronyms l Good examples: set. Walts. Separator(); create. Person. Dao(); load. Xml. Reprentation();

Use nouns when naming classes l Elements of Java Style #19 l Classes define Use nouns when naming classes l Elements of Java Style #19 l Classes define things. l Classes should be easily identifiable from their names.

Use nouns when naming classes l Bad examples: Wfn 00; Bird. Class; Proc; Use nouns when naming classes l Bad examples: Wfn 00; Bird. Class; Proc;

Use nouns when naming classes l Good examples: Wfn 00; Loan; Bird. Class; Bird; Use nouns when naming classes l Good examples: Wfn 00; Loan; Bird. Class; Bird; Proc; Process. Controller;

Use lowercase for the first word and capitalize only the first letter after l Use lowercase for the first word and capitalize only the first letter after l Elements of Java Style #22 l The capitalization allows the name to be read more easily. l Lowercase first word differentiates between methods and constructors.

Use lowercase for the first word and capitalize only the first letter after l Use lowercase for the first word and capitalize only the first letter after l Bad examples: Get. Amount(); EXEC(); Start();

Use lowercase for the first word and capitalize only the first letter after l Use lowercase for the first word and capitalize only the first letter after l Good examples: Get. Amount(); get. Amount(); EXEC(); execute(); Start(); start();

Pluralize the names of collection references l Elements of Java Style #27 l Allows Pluralize the names of collection references l Elements of Java Style #27 l Allows the reader to differentiate between single value and multiple value variables.

Pluralize the names of collection references l Bad examples: Array. List borrower = new Pluralize the names of collection references l Bad examples: Array. List borrower = new Array. List(); String[] setting = new String[] {“false”, “verbose”};

Pluralize the names of collection references l Good examples: Array. List borrowers = new Pluralize the names of collection references l Good examples: Array. List borrowers = new Array. List(); String[] settings = new String[] {“false”, “verbose”};

Pluralize the names of collection references l Java 1. 5 feature for collections is Pluralize the names of collection references l Java 1. 5 feature for collections is generics. l These define collections at instantiation. Map numbers = new Hash. Map(); numbers. put(“ 0”, “zero); numbers. put(“ 1”, “one”); String value. At. Zero = numbers. get(“ 0”); // no cast to String

All uppercase with underscores for constants l Elements of Java Style #31 l Capitalization All uppercase with underscores for constants l Elements of Java Style #31 l Capitalization distinguishes constants from other non-final variables.

All uppercase with underscores for constants l Bad examples: public final static String smtp All uppercase with underscores for constants l Bad examples: public final static String smtp = “smtp. edfund. org”; public final static int c_Success = “ 0”; public final static int c_Failure = “ 1”;

All uppercase with underscores for constants l Good examples: public final static String SMTP_SERVER All uppercase with underscores for constants l Good examples: public final static String SMTP_SERVER = “smtp. edfund. org”; public final static int CVS_SUCCESS = “ 0”; Public final static int CVS_FAILURE = “ 1”;

Define small classes and small methods. l Elements l Easier of Java Style #69 Define small classes and small methods. l Elements l Easier of Java Style #69 to: Design l Code l Test l Document l Read l Understand l Use l

Define small classes and small methods. l General rule of thumbs: 5 -10 lines Define small classes and small methods. l General rule of thumbs: 5 -10 lines of code in a method l 10 or less methods l l Involves a lot of refactoring to simpler concepts l OK, to violate this rule occasionally say for setting a lot of properties in a Java. Bean.

public void substitute(String req. Id, Print. Writer out) throws IOException { // Read in public void substitute(String req. Id, Print. Writer out) throws IOException { // Read in the template file String template. Dir = System. get. Property(TEMPLATE_DIR, ""); String. Buffer sb = new String. Buffer(""); try { File. Reader fr = new File. Reader(template. Dir + "template. html"); Buffered. Reader br = new Buffered. Reader(fr); String line; while(((line=br. read. Line())!="")&&line!=null) sb = new String. Buffer(sb + line + "n"); br. close(); fr. close(); } catch (Exception e) { } source. Template = new String(sb); try { String template = new String(source. Template); // Substitute for %CODE% int template. Split. Begin = template. index. Of("%CODE%"); int template. Split. End = template. Split. Begin + 6; String template. Part. One = new String(template. substring(0, template. Split. Begin)); String template. Part. Two = new String(template. substring(template. Split. End, template. length())); code = new String(req. Id); template = new String(template. Part. One + code + template. Part. Two); // Substitute for %ALTCODE% template. Split. Begin = template. index. Of("%ALTCODE%"); template. Split. End = template. Split. Begin + 9; template. Part. One = new String(template. substring(0, template. Split. Begin)); template. Part. Two = new String(template. substring(template. Split. End, template. length())); altcode = code. substring(0, 5) + "-" + code. substring(5, 8); out. print(template. Part. One + altcode + template. Part. Two); } catch (Exception e) { System. out. println("Error in substitute()"); } out. flush(); out. close(); }

String read. Template(Reader reader) throws IOException { Buffered. Reader br = new Buffered. Reader(reader); String read. Template(Reader reader) throws IOException { Buffered. Reader br = new Buffered. Reader(reader); String. Buffer sb = new String. Buffer(); try { String line = br. read. Line(); while (line!=null) { sb. append(line); sb. append("n"); line = br. read. Line(); } } finally { try {if (br != null) br. close(); } catch (IOException ioe_ignored) {} } return sb. to. String(); } void substitute. Code ( String template, String pattern, String replacement, Writer out) throws IOException { int template. Split. Begin = template. index. Of(pattern); int template. Split. End = template. Split. Begin + pattern. length(); out. write(template. substring(0, template. Split. Begin)); out. write(replacement); out. write(template. substring(template. Split. End, template. length())); out. flush(); } public void substitute(String req. Id, Print. Writer out) throws IOException { String. Writer template. Out = new String. Writer(); substitute. Code(source. Template, "%CODE%", req. Id, template. Out); String alt. Id = req. Id. substring(0, 5) + "-" + req. Id. substring(5, 8); substitute. Code(template. Out. to. String(), "%ALTCODE%", alt. Id, out); out. close(); }

Place types that are used together in the same package. l Elements of Java Place types that are used together in the same package. l Elements of Java Style #104 l Packaging is a way of organizing related classes l Classes are typically dependent on classes in the same package

Place types that are used together in the same package. l Bad examples: /org. Place types that are used together in the same package. l Bad examples: /org. edfund. facs Dialer. java Person. Bean. java Person. Form. java Person. Action. java Date. Utils. java /org. edfund. facs. business Dialer. java Start. Dialer. Action. java Date. Utils. java /org. edfund. facs. domain Person. java Person. Form. java /org. efund. facs. struts Dialer. Dispatch. Action. java Address. java

Place types that are used together in the same package. l Good examples: org. Place types that are used together in the same package. l Good examples: org. edfund. facs. business Dialer. Manager. java Call. Service. java org. edfund. facs. domain Person. java Address. java org. edfund. facs. struts. actions Start. Dialer. Action. java org. edfund. facs. struts. forms Person. Form. java Login. Form. java org. edfund. facs. persistence Person. Dao. java Person. Dao. Factory. java Person. Dao. Sql. Map. Impl. java org. efund. facs. util Date. Utils. java

Next Actions l Review The Elements of Java Style l Modify IDE to support Next Actions l Review The Elements of Java Style l Modify IDE to support conventions l Review your own code l Reviewing source code checking tools for addition to automated builds. (Clover, Checkstyle)

Goal: Readable source code Public void authenticate. User() { find. Authentication. Method(); if (is. Goal: Readable source code Public void authenticate. User() { find. Authentication. Method(); if (is. Network. Connection()) { authenticate. Host(); } String password = get. Password. From. User(); if (invalid. Password(password) { sound. Alarm(); } }