Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agile Lab 2008a Armin B. Cremers, Günter Kniesel, Pascal Bihler, Tobias Rho, Marc Schmatz, Daniel Speicher Sommersemester 2008 R O O T S Coding standards.

Similar presentations


Presentation on theme: "Agile Lab 2008a Armin B. Cremers, Günter Kniesel, Pascal Bihler, Tobias Rho, Marc Schmatz, Daniel Speicher Sommersemester 2008 R O O T S Coding standards."— Presentation transcript:

1 Agile Lab 2008a Armin B. Cremers, Günter Kniesel, Pascal Bihler, Tobias Rho, Marc Schmatz, Daniel Speicher Sommersemester 2008 R O O T S Coding standards Matthias Berg bergm@cs.uni-bonn.de

2 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards2 Source of ideas l Code Conventions for the Java Programming Language uhttp://java.sun.com/docs/codeconv/html/CodeC onvTOC.doc.htmlhttp://java.sun.com/docs/codeconv/html/CodeC onvTOC.doc.html l Own experience Opinion?

3 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards3 Class Names l Nouns l Simple and descriptive l Starting with a capital l CamelCase l Whole words - avoid acronyms and abbreviations uUnless the abbreviation is widely used èURL èHTML class Raster; class ImageSprite;

4 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards4 Methods l verbs l mixed case uthe first letter lowercase uthe first letter of each internal word capitalized run(); runFast(); getBackground();

5 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards5 Variables l not start with _ or $ Variable l short yet meaningful l avoid one-character variable names uexcept for temporary "throwaway" variables u Common names for temporary variables èi, j, k, m, and n for integers èc, d, and e for characters. l Java-Variables: camelCase l Prolog-Variables: CamelCase

6 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards6 Variables l NOT: private String text; public void setText(String text) { this.text = text; } private String text; public void setText(String text) { this.text = text; } private String text; public void setText(String newText) { text = newText; } private String text; public void setText(String newText) { text = newText; } private String text; public void setText(String myText) { text = myText; } private String text; public void setText(String myText) { text = myText; }

7 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards7 Variable Assignments l Avoid double assignments l Assignment operator might be confused with equal operator ushould be written as l Do not use embedded assignments ushould be written as fooBar.fChar = barFoo.lchar = 'c'; // AVOID! if (c = d) { // AVOID!... } if ((c = d) != 0) {... } a = b + c; d = a + r; d = (a = b + c) + r; // AVOID!

8 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards8 Constants l all uppercase l words separated by underscores ("_"). static final int MIN_WIDTH = 4; static final int MAX_WIDTH = 999; static final int GET_THE_CPU = 1; static final int MIN_WIDTH = 4; static final int MAX_WIDTH = 999; static final int GET_THE_CPU = 1;

9 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards9 Fields l Initialize fields within the constructur l NOT: public class TestClass { private String text; public TestClass() { this.text = „test“; } public class TestClass { private String text; public TestClass() { this.text = „test“; } public class TestClass { private String text = „test“; … } public class TestClass { private String text = „test“; … }

10 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards10 Blocks l Start Braces (Curly Brackets) on the same line, not on the next l Use them even if they are not needed l NOT public void setText(String text) { this.text = text; if (language.equals(“german”)) { this.text = “Hallo Welt”; } else { this.text = “Hello World”; } } public void setText(String text) { this.text = text; if (language.equals(“german”)) { this.text = “Hallo Welt”; } else { this.text = “Hello World”; } } public void setText(String text) { this.text = text; if (language.equals(“german”)) this.text = “Hallo Welt”; else this.text = “Hello World”; } public void setText(String text) { this.text = text; if (language.equals(“german”)) this.text = “Hallo Welt”; else this.text = “Hello World”; }

11 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards11 Parentheses l Use parentheses to show precedence if (a == b && c == d) // AVOID! if ((a == b) && (c == d)) // RIGHT

12 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards12 Special Comments l XXX: something that is bogus but works l FIXME: something that is bogus and broken l TODO: when something is missing public class TestClass { private String world = “world”; // XXX private String[] text; public TestClass(String[] text) { this.text = text; for (int i = 0; i < text.length; i++) { // TODO: fill in the text } public class TestClass { private String world = “world”; // XXX private String[] text; public TestClass(String[] text) { this.text = text; for (int i = 0; i < text.length; i++) { // TODO: fill in the text }

13 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards13 The ? operator l Do not use it l If you need to use it… u never ever nest it uexpressions should be parenthesized boolean a = true; boolean b = false; boolean c = true; boolean d = false; boolean e = true; boolean f = a ? (b ? c : d) : e; // Avoid! boolean a = true; boolean b = false; boolean c = true; boolean d = false; boolean e = true; boolean f = a ? (b ? c : d) : e; // Avoid! ((x >= 0) ? x : -x); // Good!

14 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards14 Returning Values l Try to make the structure of your program match the intent if (booleanExpression) { return true; } else { return false; } if (booleanExpression) { return true; } else { return false; } return booleanExpression; if (condition) { return x; } return y; if (condition) { return x; } return y; According to SUN return (condition ? x : y); According to SUN return (condition ? x : y); According to Matthias who hates ‘?’ String text = y; if (condition) { text = x; } return text; According to Matthias who hates ‘?’ String text = y; if (condition) { text = x; } return text;

15 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards15 ++ / -- / += / *= … operators l Avoid using +=, -=, *=,… l Do not mix ++ or -- together with asignment u error prone != sum += counter; sum = sum + counter; a = i++; a = ++i;

16 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards16 Comments l Trailing Comments ushifted far enough to separate them from the statements uindented to the same tab setting uShortcut in Eclipse: ctrl+7 or ctrl+shift+c for // if (a == 2) { return true; // special case } else { return isPrime(a); // works only for odd a } if (a == 2) { return true; // special case } else { return isPrime(a); // works only for odd a }

17 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards17 Comments l End-Of-Line Comments u// to comment out a complete line uOn consecutive muliple lines: èNOT for text comments èBUT for commenting out sections of code if (foo > 1) { // Do a double-flip.... } else { return false; // Explain why here. } // if (bar > 1) { // // Do a triple-flip. //... // } else { // return false; // } if (foo > 1) { // Do a double-flip.... } else { return false; // Explain why here. } // if (bar > 1) { // // Do a triple-flip. //... // } else { // return false; // }

18 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards18 Blank lines l Two blank lines between: uSections of a source file uClass and interface definitions l One blank line: uBetween methods uBetween the local variables in a method and its first statement uBefore a block or single-line comment uBetween logical sections inside a method to improve readability

19 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards19 Blank spaces l Seperate a keyword followed by a parenthesis by a space l DO NOT to seperate a method name and its opening parenthesis l a blank space after commas in argument lists l Seperate binary operators from their operands by a space uEXCEPT ‘.‘ l DO NOT sperate unary operators (++, !) from their operands a += c + d; a = (a + b) / (c * d); while (d = s) { n++; } printSize("size is " + foo + "\n"); a += c + d; a = (a + b) / (c * d); while (d = s) { n++; } printSize("size is " + foo + "\n");

20 XP Praktikum Agile 2008 R O O T S Agile-Lab 2008a – Coding Standards20 Blank spaces l The expressions in a for statement should be separated by blank spaces. l Casts should be followed by a blank space. for (expr1; expr2; expr3) myMethod((byte) aNum, (Object) x); myMethod((int) (cp + 5), ((int) (i + 3)) + 1); myMethod((byte) aNum, (Object) x); myMethod((int) (cp + 5), ((int) (i + 3)) + 1);


Download ppt "Agile Lab 2008a Armin B. Cremers, Günter Kniesel, Pascal Bihler, Tobias Rho, Marc Schmatz, Daniel Speicher Sommersemester 2008 R O O T S Coding standards."

Similar presentations


Ads by Google