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.

Slides:



Advertisements
Similar presentations
Coding Standards for Java An Introduction. Why Coding Standards are Important? Coding Standards lead to greater consistency within your code and the code.
Advertisements

1 CS1001 Lecture Overview Java Programming Java Programming.
Introduction to C Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
LESSON 3 - Identifiers JAVA PROGRAMMING. Identifiers All the Java components —classes, variables, and methods— need names. In Java these names are called.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
15-Jun-15 Beginning Style. 2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
26-Jun-15 Beginning Style. 2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Chapter 2: Introduction to C++.
Introduction to C Programming
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
CSC204 – Programming I Lecture 4 August 28, 2002.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
CPS120: Introduction to Computer Science
Style Guidelines. Why do we need style?  Good programming style helps promote the readability, clarity and comprehensibility of your code.
Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Java Coding Standards and Best Practices Coding Standards Introduction: After completing this chapter, you will able to keep your code up to standards.
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
1 written by BAGE 날짜 JAVA Coding Pattern 코딩 규칙 박혜웅.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List.
Code Conventions Tonga Institute of Higher Education.
C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List.
Even More Java. Java Packages What is a package? Definition: A package is a grouping of related types providing access protection and name space management.
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
DEFINITION Java IDE created by Xinox Software JCreator is a powerful interactive development environment (IDE) for Java technologies. A Java compiler.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Variables.
Data Types, Identifiers, and Expressions
Variables and Arithmetic Operators in JavaScript
Java Programming: From Problem Analysis to Program Design, 4e
Statements, Comments & Simple Arithmetic
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Coding Design, Style, Documentation and Optimization
Data Types, Identifiers, and Expressions
Introduction to C++ Programming
An overview of Java, Data types and variables
Chapter 2 Variables.
Anatomy of a Java Program
Beginning Style 27-Feb-19.
Chapter 2: Introduction to C++.
Chapter 2 Programming Basics.
Primitive Types and Expressions
Unit 3: Variables in Java
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Variables.
Tonga Institute of Higher Education
Chapter 2 Primitive Data Types and Operations
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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

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?

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;

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();

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

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; }

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!

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;

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“; … }

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”; }

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

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 }

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!

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;

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;

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 }

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; // }

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

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");

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);