Java Reference
In-Depth Information
Behavioral Write Everything Twice
Write Everything Twice (WET) is the opposite of the well-known Don't Repeat Yourself
(DRY) pattern. This code smell crops up in situations where your code ends up in repetitive
boilerplate that produces more code that needs to be tested, is harder to refactor, and is brittle
to change.
Not all WET situations are suitable candidates for point lambdification. In some situations,
couple duplication can be the only alternative to having an overly closely coupled system.
There's a good heuristic for situations where WET suggests it's time to add some point
lambdification into your application. Try adding lambdas where you want to perform a simil-
ar overall pattern but have a different behavior from one variant to another.
Let's look at a more concrete example. On top of our music domain, I've decided to add a
simple Order class that calculates useful properties about some albums that a user wants to
buy. We're going to count the number of musicians, number of tracks, and running time of
our Order . If we were using imperative Java, we would write some code like Example 7-5 .
Example 7-5. An imperative implementation of our Order class
public
public long
long countRunningTime () {
long
long count = 0 ;
for
for ( Album album : albums ) {
for
for ( Track track : album . getTrackList ()) {
count += track . getLength ();
}
}
return
return count ;
}
public
public long
long countMusicians () {
long
long count = 0 ;
for
for ( Album album : albums ) {
count += album . getMusicianList (). size ();
}
return
return count ;
}
public
public long
long countTracks () {
long
long count = 0 ;
for
for ( Album album : albums ) {
count += album . getTrackList (). size ();
}
return
return count ;
}
Search WWH ::




Custom Search