<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Overloading int considered harmful</title>
	<atom:link href="http://cafe.elharo.com/blogroll/arguments/feed/" rel="self" type="application/rss+xml" />
	<link>http://cafe.elharo.com/blogroll/arguments/</link>
	<description>Longer than a blog; shorter than a book</description>
	<lastBuildDate>Fri, 19 Mar 2010 02:32:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: andyjbs</title>
		<link>http://cafe.elharo.com/blogroll/arguments/comment-page-1/#comment-84</link>
		<dc:creator>andyjbs</dc:creator>
		<pubDate>Fri, 06 Jan 2006 22:41:53 +0000</pubDate>
		<guid isPermaLink="false">http://minicafe.elharo.com/wordpress/java/arguments/#comment-84</guid>
		<description>&lt;h3&gt;Switches &amp; bitwise constants&lt;/h3&gt;

&gt;Paulm:

&gt;The only time it is less than convenient is when you want to use a 
&gt;switch statement.

In Java 5 the new language enum construct can be used directly
with a switch statement. In Java 1.4 with the enum pattern you&#039;d
just include an integer constant as an internal member of your enum
class and switch on ENUM_VALUE.intValue() instead.

&gt;Matt Plumtree:
&gt;Presumably your applyStyle / addStyle methods would use a Set 
&gt;internally? Overall this will add overhead relative to the bitwise 
&gt;operations, but it must be argued that this is quite 
&gt;acceptable overhead.

Similarly, if you really need bitwise operations, each enum
value (either the new Java 5 ones or the old 1.4 enum pattern) can
be given an integer value of your choice to do bitwise operations
with. You&#039;d need to worry about serialization and forward
compatibility, but they are not difficult problems to solve. I&#039;d
probably just use a &lt;code&gt;Set&lt;/code&gt; or something similar
though.</description>
		<content:encoded><![CDATA[<h3>Switches &amp; bitwise constants</h3>
<p>&gt;Paulm:</p>
<p>&gt;The only time it is less than convenient is when you want to use a<br />
&gt;switch statement.</p>
<p>In Java 5 the new language enum construct can be used directly<br />
with a switch statement. In Java 1.4 with the enum pattern you&#8217;d<br />
just include an integer constant as an internal member of your enum<br />
class and switch on ENUM_VALUE.intValue() instead.</p>
<p>&gt;Matt Plumtree:<br />
&gt;Presumably your applyStyle / addStyle methods would use a Set<br />
&gt;internally? Overall this will add overhead relative to the bitwise<br />
&gt;operations, but it must be argued that this is quite<br />
&gt;acceptable overhead.</p>
<p>Similarly, if you really need bitwise operations, each enum<br />
value (either the new Java 5 ones or the old 1.4 enum pattern) can<br />
be given an integer value of your choice to do bitwise operations<br />
with. You&#8217;d need to worry about serialization and forward<br />
compatibility, but they are not difficult problems to solve. I&#8217;d<br />
probably just use a <code>Set</code> or something similar<br />
though.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Plumtree</title>
		<link>http://cafe.elharo.com/blogroll/arguments/comment-page-1/#comment-83</link>
		<dc:creator>Matt Plumtree</dc:creator>
		<pubDate>Fri, 06 Jan 2006 22:41:08 +0000</pubDate>
		<guid isPermaLink="false">http://minicafe.elharo.com/wordpress/java/arguments/#comment-83</guid>
		<description> &lt;h3&gt;Bitwise constants&lt;/h3&gt;

 I&#039;m not really disagreeing with you. The whole API could do with a thorough going over to use more appropriate idioms. Presumably your &lt;code&gt;applyStyle&lt;/code&gt; / &lt;code&gt;addStyle&lt;/code&gt; methods would use a &lt;code&gt;Set&lt;/code&gt; internally? Overall this will add overhead relative to the bitwise operations, but it must be argued that this is quite acceptable overhead. The resultant API will be clearer and, as you point out in the original posting, less fragile.

The usual OOP way to avoid the switch would be to have an actual method of FontStyle to do the work. Then, the style set would be iterated over, getting each one to apply its feature. I&#039;m not sure this would be entirely appropriate for this example, but it would depend on just how font styles were implemented.

I suppose additional styles you could add would be things like underline, strike through, superscript, subscript and so on, much as you might see in a word processor&#039;s font characteristics dialog.

I think it just has to be accepted that there are many parts of the Java API that are immature. I know you have argued before for a proper cleanup, but Sun&#039;s preference for compatibility generally win out.</description>
		<content:encoded><![CDATA[<h3>Bitwise constants</h3>
<p> I&#8217;m not really disagreeing with you. The whole API could do with a thorough going over to use more appropriate idioms. Presumably your <code>applyStyle</code> / <code>addStyle</code> methods would use a <code>Set</code> internally? Overall this will add overhead relative to the bitwise operations, but it must be argued that this is quite acceptable overhead. The resultant API will be clearer and, as you point out in the original posting, less fragile.</p>
<p>The usual OOP way to avoid the switch would be to have an actual method of FontStyle to do the work. Then, the style set would be iterated over, getting each one to apply its feature. I&#8217;m not sure this would be entirely appropriate for this example, but it would depend on just how font styles were implemented.</p>
<p>I suppose additional styles you could add would be things like underline, strike through, superscript, subscript and so on, much as you might see in a word processor&#8217;s font characteristics dialog.</p>
<p>I think it just has to be accepted that there are many parts of the Java API that are immature. I know you have argued before for a proper cleanup, but Sun&#8217;s preference for compatibility generally win out.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: edavies</title>
		<link>http://cafe.elharo.com/blogroll/arguments/comment-page-1/#comment-82</link>
		<dc:creator>edavies</dc:creator>
		<pubDate>Fri, 06 Jan 2006 22:40:36 +0000</pubDate>
		<guid isPermaLink="false">http://minicafe.elharo.com/wordpress/java/arguments/#comment-82</guid>
		<description>&lt;h3&gt;Pascal enumerations &lt;/h3&gt;

Pascal had enumerated types whose values were disjoint from the integers over twenty years ago.</description>
		<content:encoded><![CDATA[<h3>Pascal enumerations </h3>
<p>Pascal had enumerated types whose values were disjoint from the integers over twenty years ago.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: paulm</title>
		<link>http://cafe.elharo.com/blogroll/arguments/comment-page-1/#comment-81</link>
		<dc:creator>paulm</dc:creator>
		<pubDate>Fri, 06 Jan 2006 22:40:06 +0000</pubDate>
		<guid isPermaLink="false">http://minicafe.elharo.com/wordpress/java/arguments/#comment-81</guid>
		<description>&lt;h3&gt;Typesafe Enum&lt;/h3&gt;

Joshua Bloch clearly defined the technique as item 21 of Effective Java. The only time it is less than convenient is when you want to use a switch statement. Even then you can have the object generate/return a unique integer. </description>
		<content:encoded><![CDATA[<h3>Typesafe Enum</h3>
<p>Joshua Bloch clearly defined the technique as item 21 of Effective Java. The only time it is less than convenient is when you want to use a switch statement. Even then you can have the object generate/return a unique integer.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Elliotte Rusty Harold</title>
		<link>http://cafe.elharo.com/blogroll/arguments/comment-page-1/#comment-80</link>
		<dc:creator>Elliotte Rusty Harold</dc:creator>
		<pubDate>Fri, 06 Jan 2006 22:39:27 +0000</pubDate>
		<guid isPermaLink="false">http://minicafe.elharo.com/wordpress/java/arguments/#comment-80</guid>
		<description>&lt;h3&gt;Combined definitions&lt;/h3&gt;

 Using bitwise constants and bitwise arithmetic to pick font styles is pretty old, C-style thinking. It&#039;s a neat hack; but it&#039;s not really necessary here. The right way to solve this problem is to replace &lt;code&gt;setStyle&lt;/code&gt; with &lt;code&gt;applyStyle&lt;/code&gt; or &lt;code&gt;addStyle&lt;/code&gt; or some such method. You may want a &lt;code&gt;removeStyle&lt;/code&gt; method too. An added advantage of this is you&#039;re no longer limited to a maximum of 8 or 32 constant values. You can define as many as you need.</description>
		<content:encoded><![CDATA[<h3>Combined definitions</h3>
<p> Using bitwise constants and bitwise arithmetic to pick font styles is pretty old, C-style thinking. It&#8217;s a neat hack; but it&#8217;s not really necessary here. The right way to solve this problem is to replace <code>setStyle</code> with <code>applyStyle</code> or <code>addStyle</code> or some such method. You may want a <code>removeStyle</code> method too. An added advantage of this is you&#8217;re no longer limited to a maximum of 8 or 32 constant values. You can define as many as you need.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Plumtree</title>
		<link>http://cafe.elharo.com/blogroll/arguments/comment-page-1/#comment-79</link>
		<dc:creator>Matt Plumtree</dc:creator>
		<pubDate>Fri, 06 Jan 2006 22:39:03 +0000</pubDate>
		<guid isPermaLink="false">http://minicafe.elharo.com/wordpress/java/arguments/#comment-79</guid>
		<description>&lt;h3 class=&quot;subject&quot;&gt;You missed at least one definition&lt;/h3&gt;


You would need to have a &lt;code&gt;FontStyle.BOLDITALIC&lt;/code&gt;. Or is that &lt;code&gt;FontStyle.ITALICBOLD&lt;/code&gt;? Or &lt;code&gt;FontStyle.BOLD_ITALIC&lt;/code&gt;? Or a &lt;code&gt;FontStyle.and(FontStyle other)&lt;/code&gt; method? Or something.

This is a slight disadvantage of using true types for combinations of values, that using ints doesn&#039;t have.

Pre-canned combinations are going to be more efficient than using a combining method, since they will be created once then assigned straight into classes as they are loaded, but their number increases exponentially as more mutually inclusive options are added. Consider all the constants to enter (and their names) if you had 8 options that could be combined in any way.

You may also need to have big switch statements in the implementation of &lt;code&gt;Font&lt;/code&gt;, to deal with each combination, possibly just to turn them back into a bitfield. Alternatively you could embiggen &lt;code&gt;FontStyle&lt;/code&gt; to include &lt;code&gt;isBold()&lt;/code&gt; and &lt;code&gt;isItalic()&lt;/code&gt; methods. This would require initializers for each style, probably bringing us back to bitfields again. However, at least they would be localized to the &lt;code&gt;FontStyle&lt;/code&gt; code.</description>
		<content:encoded><![CDATA[<h3 class="subject"&gt;You missed at least one definition</h3>
<p>You would need to have a <code>FontStyle.BOLDITALIC</code>. Or is that <code>FontStyle.ITALICBOLD</code>? Or <code>FontStyle.BOLD_ITALIC</code>? Or a <code>FontStyle.and(FontStyle other)</code> method? Or something.</p>
<p>This is a slight disadvantage of using true types for combinations of values, that using ints doesn&#8217;t have.</p>
<p>Pre-canned combinations are going to be more efficient than using a combining method, since they will be created once then assigned straight into classes as they are loaded, but their number increases exponentially as more mutually inclusive options are added. Consider all the constants to enter (and their names) if you had 8 options that could be combined in any way.</p>
<p>You may also need to have big switch statements in the implementation of <code>Font</code>, to deal with each combination, possibly just to turn them back into a bitfield. Alternatively you could embiggen <code>FontStyle</code> to include <code>isBold()</code> and <code>isItalic()</code> methods. This would require initializers for each style, probably bringing us back to bitfields again. However, at least they would be localized to the <code>FontStyle</code> code.</h3>
]]></content:encoded>
	</item>
	<item>
		<title>By: Elliotte Rusty Harold</title>
		<link>http://cafe.elharo.com/blogroll/arguments/comment-page-1/#comment-78</link>
		<dc:creator>Elliotte Rusty Harold</dc:creator>
		<pubDate>Fri, 06 Jan 2006 22:37:24 +0000</pubDate>
		<guid isPermaLink="false">http://minicafe.elharo.com/wordpress/java/arguments/#comment-78</guid>
		<description>&lt;h3&gt;Part 2&lt;/h3&gt;

I am working on a second half to this article that will consider:

ClassLoader issues
Serialization
Enums</description>
		<content:encoded><![CDATA[<h3>Part 2</h3>
<p>I am working on a second half to this article that will consider:</p>
<p>ClassLoader issues<br />
Serialization<br />
Enums</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bruce Eckel</title>
		<link>http://cafe.elharo.com/blogroll/arguments/comment-page-1/#comment-12</link>
		<dc:creator>Bruce Eckel</dc:creator>
		<pubDate>Fri, 06 Jan 2006 14:22:03 +0000</pubDate>
		<guid isPermaLink="false">http://minicafe.elharo.com/wordpress/java/arguments/#comment-12</guid>
		<description>&lt;h3 class=&quot;subject&quot;&gt;This is what Java 5 enums are for!&lt;/h3&gt;

Also, see Josh Bloch&#039;s &lt;i&gt;Effective Java&lt;/i&gt;, where he details a pre-5 enumeration idiom. I also cover this in Thinking in Java.</description>
		<content:encoded><![CDATA[<h3 class="subject">This is what Java 5 enums are for!</h3>
<p>Also, see Josh Bloch&#8217;s <i>Effective Java</i>, where he details a pre-5 enumeration idiom. I also cover this in Thinking in Java.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
