Object Calisth

Transcription

Object Calisth
Object Calisthenics - Objektgymnastik
Franziska Sauerwein & David Burkhart, andrena objects ag
Die 10 Regeln
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Don‘t abbreviate
Don’t use the else keyword
No static methods (except for factories)
Use only one level of indentation per method
Use only one dot per line
Don’t use any getters/setters/properties
Wrap all primitives and strings
Use first-class collections
Keep all entities small
Don’t use any classes with more than two instance variables
14.11.2013
2
Fragen?
14.11.2013
3
Praxis
• Tennis Kata
– https://github.com/emilybache/Tennis-Refactoring-Kata
– Refactoring (empfohlen: Tennis3) oder neu schreiben
– Kein Internet? USB-Stick abholen
• Unterstützte Sprachen:
– C++, C#, Java, JavaScript, Objective C, Perl, Python, Ruby
– … oder beim Neuschreiben jede objektorientierte Sprache
• Aufgabe für die Pause:
– Pairing Partner finden
– Entwicklungsumgebung vorbereiten
– Code auschecken / abholen
14.11.2013
4
Regel 1: Don't abbreviate
int sum(int[][] m) {
int i, j, s = 0;
for(i = 0; i < m.length; i++) {
for(j = 0; j < m[i].lenght; j++) {
s = s + m[i][j];
}
}
return s;
}
int sum (Matrix matrix) {
int sumOfValues = 0;
for(Row row : matrix) {
sumOfValues += sum(row);
}
return sumOfValues;
}
int sum(Row row) {
int sumOfValues = 0;
for(int value : row) {
sumOfValues += value;
return sumOfValues;
}
Seite 5
Regel 2: Don’t use the ELSE keyword
if (status == OK) {
doSomething();
} else {
processError();
}
Seite 6
Regel 3: No static methods
Seite 7
Regel 4: On level of indentation per method
class Board {
class Board {
...
...
String board() {
String board() {
StringBuffer buf = new StringBuffer();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < 10; i++) {
collectRows(buf);
for (int j = 0; j < 10; j++)
return buf.toString();
buf.append(data[i][j]);
}
buf.append(“\n”);
void collectRows(StringBuffer buf) {
}
for (int i = 0; i < 10; i++)
return buf.toString();
collectRow(buf, i);
}
}
}
void collectRow(StringBuffer buf, int row) {
for (int i = 0; i < 10; i++)
Buf.append(data[row][i]);
buf.append(“\n”);
}
}
Seite 8
Regel 5: One dot per line
class Board {
...
class Piece {
...
String representation;
}
class Location {
...
Piece current;
}
String boardRepresentation() {
StringBuffer buf = new StringBuffer();
for (Location l : squares())
buf.append(
l.current.representation.substring(0, 1));
return buf.toString();
}
}
Seite 9
Regel 5: One dot per line
class Board {
...
class Piece {
...
private String representation;
String character() {return representation.substring(0, 1); }
void addTo(StringBuffer buf) { buf.append(character()); }
}
class Location {
...
private Piece current;
void addTo(StringBuffer buf) { current.addTo(buf); }
}
String boardRepresentation() {
StringBuffer buf = new StringBuffer();
for (Location l : squares()) l.addTo(buf);
return buf.toString();
}
}
Seite 10
Regel 6: No getters/setters/properties
class Number {
int value;
int getValue() {
return value;
}
}
class Calculator {
…
Number add(Number first,
Number second) {
return new Number(
first.getValue() +
second.getValue());
}
}
class Number {
int value;
Number add(Number other) {
return value + other.value;
}
}
class Calculator {
…
Number add(Number first,
Number second) {
return first.add(second);
}
}
Seite 11
Regel 7: Wrap all primitives and Strings
class Account {
...
void transferMoney(double amount) {
...
}
}
class Account {
...
void transfer(Money amount) {
...
}
}
Seite 12
Regel 8: First class collections
class Library {
List<Book> books;
List<Person> subscribers;
...
}
class Library {
Books books;
Subscribers subscribers;
…
}
class Books {
List<Book> books;
...
}
class Subscribers {
List<Subscriber> subscriber;
...
}
Seite 13
Regel 9: Keep all entities small
Keine Klassen länger als 50 Code-Zeilen.
Kein Package mit mehr als 10 Klassen.
Seite 14
Regel 10: No Classes with more than two instance variables
class Name {
String first;
String middle;
String last;
}
class Name {
Surname family;
GivenNames given;
}
class Surname {
String family;
}
class GivenNames {
List<String> names;
}
Seite 15

Documents pareils