 |
Reading Swing code and learning Java
Posted by kirillcool on July 20, 2006 at 06:14 PM | Comments (7)
Browsing the code for BasicScrollBarUI (yes, i don't have a life), i was puzzled by the following lines:
(ltr ? decrButton : incrButton).setBounds(leftButtonX,
itemY, leftButtonW, itemH);
(ltr ? incrButton : decrButton).setBounds(rightButtonX,
itemY, rightButtonW, itemH);
Say what? I had no idea that the ternary conditional operator can appear as a primary expression (JLS 15.8) followed by the method invocation. Well - it can (according to the 15.25 of JLS). Finally I have something to impress my wife. "So, honey, how was your day?", "You wouldn't believe!!! Did you know that the ternary conditional operator can appear as a primary expr... Hey, don't fall asleep!!!"
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
That's something I use quite often and I love it :)
Posted by: gfx on July 20, 2006 at 07:09 PM
-
In case you aren't a fighter jet pilot and you want a woman ... tell her about programming! Most of them get obsessed immediately!
Posted by: herkules on July 21, 2006 at 12:43 AM
-
it'd be better to make the test once, and assign *button to temporary variables with telling names, and use them in the setBounds* calls.
it's hard to guess what the author meant, or if the tests are correct, as the code is written.
Posted by: robilad on July 21, 2006 at 05:54 AM
-
I'd go with Dalibor on this one. One of the more compelling reasons would automatic code coverage during unit tests. Are these able to track that only one portion of the operator was executed? And the code isn't readable at all - an if-else is much more suited here.
This on top of the variable naming that this "clever" usage had to employ. Instead of prev/next buttons of the scroll bar that have clear meanings, you have left/right prefixes for quite some variables which are later used to set bounds of the scroll buttons. 2-3 lines fewer, but the logic is more obscure (hey, it rhymes)
Posted by: kirillcool on July 21, 2006 at 05:26 PM
-
Joerg, luckily for us a figther jet costs too much, so there aren't as many fighter jet pilots as there are women :)
Posted by: kirillcool on July 21, 2006 at 05:27 PM
-
I just have to agree that the programmer would have been done faster with:
if (ltr) {
decrButton.setBounds(leftButtonX, itemY, leftButtonW, itemH);
incrButton.setBounds(rightButtonX, itemY, rightButtonW, itemH);
}
else {
incrButton.setBounds(leftButtonX, itemY, leftButtonW, itemH);
decrButton.setBounds(rightButtonX, itemY, rightButtonW, itemH);
}
And unfortunately, this would actually run faster too, since there is now only one test! Not to mention the readability.
Steev Coco.
Posted by: steevcoco on July 25, 2006 at 09:23 AM
-
P.S.
The less time you actually spend coding, the more time you have to tell girls/boys about it.
Posted by: steevcoco on July 25, 2006 at 09:26 AM
|