Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!news.netfront.net!not-for-mail From: Wanja Gayk Newsgroups: comp.lang.java.programmer Subject: Re: code generation for the ternary operator Date: Sat, 29 Oct 2011 00:23:11 +0200 Organization: Netfront http://www.netfront.net/ Lines: 50 Message-ID: References: <29cla79dh934epr5n27cghvupjk77mpuqr@4ax.com> NNTP-Posting-Host: 77.8.61.95 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: adenine.netfront.net 1319840596 30229 77.8.61.95 (28 Oct 2011 22:23:16 GMT) X-Complaints-To: news@netfront.net NNTP-Posting-Date: Fri, 28 Oct 2011 22:23:16 +0000 (UTC) User-Agent: MicroPlanet-Gravity/3.0.4 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9287 In article <29cla79dh934epr5n27cghvupjk77mpuqr@4ax.com>, see_website@mindprod.com.invalid says... > > I wonder if anyone has ever looked at the byte code and/or the hotspot > code generated by code like this: > > final int count = lines * 3 + ( b1 ? 2 : 0 ) + ( b2 ? 2 : 0 ) ; > > vs: > > int count = lines *3; > if ( b1 ) count += 2; > if ( b2 ) count += 2; > > I wondered how clever it is. This is just curiosity. This is not > critical code. > > Which style do you prefer? I personally don't care for the byte code unless it really matters, which is almost certainly never. So I concentrate on readability and code style. Among these two, I'd prefer the first variant for these reasons: It is obviously very clear that the value "count" won't change later in the program. It's short, concise and the brackets look like a "capsule" that wraps the decision being made. But if the term is getting too long, it might be a good idea to write this, which I find even more readable: final int v1 = b1 ? 2 : 0; final int v2 = b2 ? 2 : 0; final int count = lines * 3 + v1 + v2 ; Still I would try to find proper names for "v1", "v2", "b1" and "b2", like for example: final int optionalHeader = headerPrinted ? 2 : 0; final int optionalFooter = footerPrinted ? 2 : 0; final int count = lines * 3 + optionalFooter + optionalHeader; Kind regards, Wanja -- ..Alesi's problem was that the back of the car was jumping up and down dangerously - and I can assure you from having been teammate to Jean Alesi and knowing what kind of cars that he can pull up with, when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer] --- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---