Published by Fabian on 13 Jan 2010

Ugliest Code of the Year

Usually I rant about code from others. But this is something different. this is my code:

PdfPCell pdfPCell = table.getRow(0).getCells()[0];
List compositeElements = pdfPCell.getCompositeElements();
Paragraph p = (Paragraph) compositeElements.get(0);
Chunk c = (Chunk) p.get(0);
Jpeg jpeg = (Jpeg) (((Object[])c.getAttributes().get("IMAGE"))[0]);

Now comes my excuse for this crap. I need to parse this HTML and render it in a PDF (currently using iText):

<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="1%"><img height="100" border="0" width="150" src="/pat_to.jpg" /></td>
<td>some fancy text</td>
</tr>
</table>

iText cannot automatically resize columns. So the column needs an explicit width. I don’t know the width of the table, because its not on the page, so I need the page width. I need to delve into the cell of the table which I know to contain the image. I need to know that its an Jpeg and that it had no alignment because if it had it would not be wrapped as chunk in a paragraph.
This is among the ugliest I ever wrote. But now my excuse. It is not my faul :)

The iText API (in 2.7.1) is not consistent at all, you have Arrays, Lists, Maps mixed and matched, keys as String, Constant (public or private constant) or no keys at all. Properties of images stored for each type in a different way. And you need to know a lot on yout parsed html structure, because iText cannot discover it. The way the HTML parser is implemented is way to basic and not extendable.

Currently thinking about switching to POI or rewriting the iTextParser….

Published by Fabian on 01 Dec 2009

Today is symfony Project Day

Last night I hung out with Fabien, Jon and Kris on IRC, preparing today!
Here the results of our hackathon

We all hope you enjoy our stuff hand have productive days till Christmas.

Published by Fabian on 26 Nov 2009

Management logic?

on one of my projects we introduced scrum… ok lets be honest, we are doing “scrum … but”. But we are now able to deliver much more than before, and the effort = money spend on each feature has decreased. However we track that we talk more than before. We do standups every day, and once a week a planning session (either backlog with the PO or sprint with the SM) and we are doing retrospectives.
But somehow the higher level management is unhappy. They tell us we spend to much time on “communication”. They are very much happy with our productivity though.
Might be that they have not realized that the increased communication time is related to the increased productivity?

Published by Fabian on 25 Nov 2009

devoxx is over

If you were following me on twitter, you might have noticed that i was in Antwerp Belgium, for the devoxx conference. Which is one of the best Java conferences in Europe and very affordable. As usual, it is pretty interesting that you can find out that famous gurus are normal people that you can talk to. Well they are geeks, so am I, that is why we can come along.

I learned a lot (as always), but interestingly it was mainly on three topics
* New Features of JDK 7, EE 6 and Spring 3
* New programming models like the famous map&reduce, big tables etc
* New JVM based languages: Clojure and Scala

I also attended the live recording for the javaposse.com episode, which was tremendously fun, especially because Atlassian sponsored a round of beer.
I met Kirk, who still reminds our good hours playing Guitar Hero at the meet the experts – performance, and had the pleasure to also meet Holly, who was unfortunately the only female speaker, and could introduce her also to our meet the experts concept.
I would like to have her there, because she really knows the internals of all the IBM JVMs.

I also had a length talk with Howard about Tapestry and various other stuff. because we all like open source and sharing ideas to make the world better, I talked with him about porting the web debug toolbar from symfony to Tapestry.
I think that will be an interesting experiment, also refreshing my Tapestry skills.

Great conference, lot to learn, very inspiring. Now back to daily work :-/

Published by Fabian on 10 Nov 2009

IT industry has changed

Bitkom, the major IT industry organization, said that the industry has changed.

Eating pizza together for lunch, having flex time and the big boss as friend. Those are only clichés. Those shops where people almost live in the company have disappeared.

When asking our my boss Mirko about this, while he was for lunch with a few colleagues and me at our local pizza supplier:

Pfft… they don’t have a clue

I think Mirko was right. So I could without regret spend the evening playing Chess and Guitar Hero in the office :-)

Published by Fabian on 01 Nov 2009

Why do people code like that? And how do you review it?

if (!""==paramString.trim() && paramString.trim().length() > 0 && paramString != null)

I do like to coach colleagues and totally stangers in writing good code. I believe there is also a lot I can learn.
My colleague Thomas tweeted:

unless(!disable_message_hiding) {...}

quad negation. !@#$@# (via @mhevery)

I think I could have done this myself some time ago. You have to learn to avoid negations so this doesn’t pile up like that. I can understand that somebody might have coded that.
But coming back to the line at the beginning of this post:

if (!""==paramString.trim() && paramString.trim().length() > 0 && paramString != null)

How can somebody write this code? Obviously not test driven, because test would have showed that this code is actually mostly nonfunctional.
Ok, when beginning with Java it can happen to you that you check String equality with == and not with String.equals(). Interestingly this does work quite often (but people don’t know why).
trim() shows already some more advanced understanding, because why else would you understand that you want to remove spaces from the string first.
But after having understood that we need to trim() before comparing to empty string, why the additional length check?
And why the hell the not null check? I suspect there was once a stacktrace in the log and this was supposed to fix this. Obviously in reality this method almost never gets null.

So: How could this code have been created? I think the author had no understanding of what he was doing there.

if (paramString.length() > 0)

is code Newbies would write. Its not bad. it is actually covering most of the normal cases. It is almost not worse than the previous example but much easier to read.

Such “empty String” checking is almost in every code. This is why commons-lang provided us with something neat:

if (! StringUtils.isEmpty(paramString))

How do you handle talking about bad code in code reviews? Pair programming?
I found it hard. So i decided to go the way Douglas Crockford goes when he talks about JSlint:

Warning!
JSLint will hurt your feelings.

I say: “that code makes no sense”, “that code is stupid”, “that can be done way shorter”, “code repetition – thats evil”.
It is like the standard measure for code reviews:

WTFs per Minute

WTFs per Minute

I usually try to avoid to call the author stupid. But I try also to be very clear on the code. Usually it helps showing how this can be done much better.
Can somebody recommend a paper or book on the topic?

Oh, in case I did review your code and say something nasty: I apologize. I want to make the code better. I want us all to produce better code. Better code saves us weekends!

Next »