### Round and Round we go; Netbeans WTF

I found the following code in a recent snapshot of netbeans. If anyone has a clue as to what the developer was thinking, let me know. The code is in org.openide.awt.HtmlLabelUI:

private static int textWidth(String text, Graphics g, Font f, boolean html) {

if (text != null) {

if (html) {

return Math.round(

Math.round(

Math.ceil(

HtmlRenderer.renderHTML(

text, g, 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE, f, Color.BLACK,

HtmlRenderer.STYLE_CLIP, false

)

)

)

);

} else {

return Math.round(

Math.round(

Math.ceil(

HtmlRenderer.renderPlainString(

text, g, 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE, f, Color.BLACK,

HtmlRenderer.STYLE_CLIP, false

)

)

)

);

}

} else {

return 0;

}

}

Labels: WTF

## 2 Comments:

Franky speaking I do not understand it from the very beginning: I have found the method in google:

public static double

renderPlainStringRender a string to a graphics instance, using the same API as renderHTML...

Returns the width in pixels successfully painted.Why the successful pixels amount is a double?

Maybe the programmer is just a huge fan of Math.round() ^^

I can imagine how this one developed, he just used

Math.ceil()first, but then:possible loss of precisionfound : double

required: int

ok, so he was looking for a method to convert it to int, he had the javadoc for the class Math still open (he just looked up

Math.ceil()) and found the methodstatic long round(double a)"That's gonna work - long is also an integer right?"

Math.round(Math.ceil())of course...

possible loss of precisionfound : long

required: int

it didn't. But there is another

round()method:static int round(float a)And being the clever programmer he obviously is ^^, he knew Java would convert his

longtofloat. So, using round a second time, the result would now beint!"Hooray!"

Post a Comment

<< Home