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
6 Comments:
Franky speaking I do not understand it from the very beginning: I have found the method in google:
public static double renderPlainString
Render 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 precision
found : 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 method
static long round(double a)
"That's gonna work - long is also an integer right?"
Math.round(Math.ceil())
of course...
possible loss of precision
found : 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 long to float. So, using round a second time, the result would now be int!
"Hooray!"
Thank you for your post, I look for such article along time, today i find it finally. this post give me lots of advise it is very useful for me.
Best bca college in noida
Top bca colleges in noida
This is precisely what I need to familiarize myself with this area. Looking forward to many interesting and useful articles from you. the article is very interesting and effective.
DedicatedHosting4u.com
Nice post. It is really interesting. Thanks for sharing the post!
craigslist For PC
It is a wonder article. Astroswmig is India's famous astrology prediction wbsite.here you know your horoscope and kundali as per your date of birth and ask questions to the top astrologers of India regarding your problems.
Gemini Horoscope 2021
Taurus Horoscope 2021
Aries Horoscope 2021
Talk To Astrologer
Aaj ka Rashifal
Online Astrologer
Post a Comment
<< Home