Another crappy book
I came to read “JUnit Profi-Tipps” yesterday. This book from ‘Entwickler.Press’ is about unit-testing with JUnit, just like many others are.
But there is something outstanding to it: The more you read, the harder it gets to keep reading. Don’t get me wrong: i often read books i disagree with. It is a good educating and entertaining thing to do and a simple necessity when you want to actually learn something. But this one’s different:
Consider this: the author encourages the use of the following snippet:
| Java | | copy | | ? |
void assertInList(List list, Object key){ |
if (list==null){ fail("Liste ist null"); } |
int size = list.size(); |
for (int i = 0; i < size ; i++){ |
Object o = list.get(i); |
if (o.equals(key)){ |
return; |
} |
} |
fail ("Schlüssel "+key+" nicht in der Liste"); |
} |
Before going on, read this twice – this is really printed in a book called “Profi-Tipps”. It is a very good example, of how to screw up java code in general! Let me point to some obvious flaws here:
- what if key is null?
- For loop with int? What happened to Iterators?
- Try looping with list.get(int) over a large LinkedList!
- What if a null pointer is contained in the list?
- … and what is wrong with Collection.contains(Object), anyway ?
To be fair to the author, if i read this correct, it isn’t his own code but taken from some crappy Library. What the author is responsible for (apart from using this in his text), is his suggestion to introduce an abstract BaseTestCase in your project and putting stuff like this in (what about favoring composition over inheritance?).
I’m sure the author is a nice and smart guy, and the above was an accident caused by dealine-pressure, coffee and other drugs. More than bashing on him, i blame the publisher for his editorial process. Anyone with a basic understanding of Java pulls his hair out reading the above snippet and – in consquence – avoids any book from this publisher further on.
Sad.

