Excellent book written by Excellent author.Please go and get a copy of this book soon.
There is a book review available by Amr Elssamadisy in InfoQ site about this book.
I wanted to share few important points i found from this book.
1) Symmetry in code is where the same idea is expressed the same way everywhere it appears in the code.Often finding and expressing symmetry is a preliminary step to removing duplication.
Here is an example of code that lacks Symmetry.
void process() {
input();
count++;
output();
}
We can rewrite this on the basis of symmetry,resulting in:
void process() {
input();
incrementCount();
output();
}
Still this method violates symmetry.The input() and output() operations are named after intentions,incrementCount() after an implementation.Looking for symmetries,i think about WHY i am incrementing the count,perhaps resulting in:
void process() {
input();
tally();
output();
}
2) Another Principle behind the implementation patterns is to express as much of our intention as possible declaratively.Imperative Programming is powerful and flexible,but to read it requires that you follow the thread of execution.
For example in older versions of JUnit,classes could have a static suite() method that returned a set of tests to run.
public static junit.framework.Test suite() {
Test result=new TestSuite();
.......
return result;
}
We have to go read and understand the method to know what tests are going to run?
JUnit4 on the other hand uses the principle of declarative expression to solve the same problem.Now we only need to look into TestClasses annotation to see what results will be run.Because the expression of the suite is declarative we donot need to suspect any tricky exceptions.
@RunWith(Suite.class)
@TestClasses({
SimpleTest.class
ComplicatedTest.class
})
class AllTests {
}
3) Rate of Change principle is to put logic or data that changes at the same rate together and seperate logic or data that changes at different rates.Sometimes a Rate of Change principle applies to changes programmer makes.
Two fields that change together but out of sync with their neighbouring fields probably belong to a helper object.
If a financial intrument can have its value and currency change together,then those two fields would probably better be expressed as a helper Money object.
setAmount(int value,String currency) {
this.value=value;
this.currency=currency;
}
becomes
setAmount(int value,String currency) {
this.value=new Money(value,currency);
}
and then later
setAmount(Money value) {
this.value=value;
}
4) Code to interfaces but not to implementation.
5) To Get an inner class that is completly detached from its enclosed instances,declare it static.
6) Delegation: Another way to execute different logic in different instances is to delegate work to one of several possible kinds of objects.The common logic is held in the referring class and variations in the delegates.
An example of using a delegate to capture variation is handling user input in a graphical editor.Sometimes a button press means "create a rectangle",sometimes it means "move a figure" and so on.
One way to express is to use Conditional logic:
public void mouseDown() {
switch(getTool()) {
case SELECTING:
....
break;
case EDITING_TEXt:
....
break;
default:
break;
}
}
This has problems as adding a new tool requires modifying the code and the duplication of the conditional(in mouseUp(),moveDown(),etc..) making adding new tools complicated.
Delegation allows for flexibility.
public void mouseDown() {
getTool().mouseDown();
}
7) John Von Neumann contributed one of the primary metaphors of computing - a sequence of instructions that are executed one by one.
8) Symmetry can improve the readability of code.Consider the following code.
void compute() {
input();
helper.process(this);
output();
}
While this method is composed of three others,it lacks Symmetry.The readability of the method is improved by introducing a helper method that reveals the latent symmetry.
compute() {
new Helper(this).compute();
}
Helper.compute() {
input();
process();
output();
}
9) Guard classes are particularly useful when there are multiple conditions.
void compute() {
Server server=getServer();
if(server!=null) {
Client client=server.getClient();
if(client!=null) {
Request current=client.getRequest();
if(current!=null)
processRequest(current);
}
}
}
Nested conditionals breed defects.The guard clause version of the same code notes the prerequisites to processing a request without complex control structures.
void compute() {
Server server=getServer();
if(server==null) return;
Client client=server.getClient();
if(client==null) return;
Request current=client.getRequest();
if(current==null) return;
processRequest(current);
}
A variant of the guard clause is the continue statement in a loop.
10) General Strategy for visibility for methods it to restrict as much as possible.
11) We cannot simply ask a Map for its iterator because it is not clear whether we want an iterator over keys,over the values,or over the pairs of keys-and-values.
12) The potential problem with ArrayList is that contains(Object) and other operations that rely on it like remove(Object) take time proportional to the size of the collection.
13) It is better to delegate to a collection rather than inherit from one.
class Library {
Collection<Book> books=new ArrayList<Book>();
}
Hope you have enjoy reading this wonderful book.
About The Author
Kent Beck is the creator of Extreme Programming[1], developed while he was serving as project leader on Chrysler Comprehensive Compensation (C3), a long-term project for employee payroll. Beck was one of the 17 original signatories of the Agile Manifesto in 2001.[1]
Beck has pioneered software design patterns, the rediscovery of Test-driven development, as well as the commercial application of Smalltalk. Beck popularized CRC cards with Ward Cunningham and along with Erich Gamma created the JUnit unit testing framework.
Kent Beck has an M.S. degree in computer science from the University of Oregon.
2 comments:
Nice article, thank you for posting your conclusions.
Cheers!!
Alin,
Thanks a lot for your encouragments.
Thanks
Prashant
Post a Comment