Archive for the 'java' Category

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">
<td width="1%"><img height="100" border="0" width="150" src="/pat_to.jpg" /></td>
<td>some fancy text</td>

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 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:

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!

Published by Fabian on 30 Jun 2009

State of the Art Dependency Injection for PHP

The second symfony component has gone live. It is the Dependency Injection Monkey. For me as a Java guy, who is used to Spring or Guice it is a relief to see that finally there is a compact and great PHP DI container available. If you never have heard about dependency injection (also called Inversion of Control) you should try to understand it. If you have and are developing PHP you should try the symfony dependency injection. Or better If you are on Java, try Spring :-)

Published by Fabian on 21 Apr 2009

Blogging live from JAX

I am currently standing at out booth in the Rheingoldhalle in Mainz, where the JAX 2009 is taking place. Quite an amazing amount of high quality talks, and good talks and networking inbetween the session. codecentric is silver sponsor, we are having three nice sessions for you to attend and are also blogging live from here, and advertising meet the experts.

I already had a great talk from Angelika Langer which caused some worries about concurrency in my mind, and a talk about jBPM which looks really like a BPM solution one can include in an existing Java Architecture. I especially liked that you can JUnit test it.

Tonight we will have dinner at the famous Waldgeist Hofheim, where you can get a 2.5 kg Schnitzel! Hmmm tasty :-)

Mirko just put up a new blog posting on our company blog including a nice picture showing us at our booth. I shamelessly copy it :-)

Published by Fabian on 30 Mar 2009

Want to ask a guru?

Andrew Hunt writes in Pragmatic Progammer (an excellent book by the way) that you should consult a Guru on a topic where you find it difficult to find a good solution for a problem. But he admits that many companies are short of Gurus. He claims that the internet might be a good source to find Gurus.

I don’t think it is that easy. While there are really helpful Gurus participating in mailing lists and forums, written discussion is a problematic transport for your issues. And sometimes its not easy to select the appropriate Guru for your problem. Wouldn’t it be better to be able to talk to a Guru in person?

My employer codecentric now launches a new quarterly workshop series called “Meet the experts“. Because we are and want to continue to be among the thought leaders in Java Technology, Agile Software Development, Architecture and Performance, we decided to invite top notch Gurus to spend a day on our premises and have a full day with interesting talks and an Open Space session in the evening, which everybody can attend (well at least as long we still have seats left).

The first “meet the experts” will be held on 26th of June with “Performance” as it’s topic. Dr. Heinz Kabutz, Java Champion, and Kirk Pepperdine from, Alois Reitbauer from dynaTrace and my boss Mirko Novakovic will have a talk.

I am really excited about that day and have high expectations. And we have the next events already in the pipe, which I hope that I am allowed to talk about soon.

Published by Fabian on 23 Mar 2009

Google rates blogs very high

I always knew that Google somehow favours blogs, because they represent current informaton, rather than aged and decayed information found on “static” sites.

But somehow I am blown away by the Google ranking a recent blog post on our corporate blog produced. I wrote about a possible addition to StringUtils. Not very elaborated, but a good base to start from.

Today I needed to search for a method in StringUtils at a colleagues desk. We went to google and searched for StringUtils.

I expected the API link on top, but never ever expected the second most relevant hit to be my blog posting. That is pretty cool :-)

Next »