<?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"
	>
<channel>
	<title>Comments on: Never, never, never use String in Java (or at least less often :-)</title>
	<atom:link href="http://www.codemonkeyism.com/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codemonkeyism.com/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/</link>
	<description>Programming is hard</description>
	<pubDate>Sat, 22 Nov 2008 11:59:59 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: stephan</title>
		<link>http://www.codemonkeyism.com/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-197954</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Thu, 13 Nov 2008 14:49:12 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-197954</guid>
		<description>@Nathan: Thanks for your comment. Yes I had the problem also several times in the past.</description>
		<content:encoded><![CDATA[<p>@Nathan: Thanks for your comment. Yes I had the problem also several times in the past.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nathan</title>
		<link>http://www.codemonkeyism.com/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-197953</link>
		<dc:creator>Nathan</dc:creator>
		<pubDate>Thu, 13 Nov 2008 14:43:39 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-197953</guid>
		<description>Just wanted to say that less than an hour after I read this I ran into a problem on my project: "what is customerId, and how do I find it?"  The answer wasn't pretty.  The code wasn't reusable.  I will definitely be using these ideas on projects I have more control over.</description>
		<content:encoded><![CDATA[<p>Just wanted to say that less than an hour after I read this I ran into a problem on my project: &#8220;what is customerId, and how do I find it?&#8221;  The answer wasn&#8217;t pretty.  The code wasn&#8217;t reusable.  I will definitely be using these ideas on projects I have more control over.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://www.codemonkeyism.com/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-196374</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Tue, 11 Nov 2008 06:32:27 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-196374</guid>
		<description>@Dan: &lt;i&gt;"I think there is value in creating a ProjectID class and a TaskID class so that the compiler can guarantee, not just that the ID I have received is an integer, but that it is an integer representing the ID of a task, and not the ID of a project."&lt;/i&gt;

I've tried this too lately, and I really like it.  It makes code more readable than using int/long/String for IDs. As you've saif, ProjectId is something different than TaskId. And with a growing code base, problems will arise if every Id is String/Int/Long. (mostly because developers - if you take enough of them - won't name every ProjectId the same, some call it ProjectId, some PID, etc. all being Longs. They can't screw up with a ProjectId type)

Thanks for your long comment, thanks for the insights.</description>
		<content:encoded><![CDATA[<p>@Dan: <i>&#8220;I think there is value in creating a ProjectID class and a TaskID class so that the compiler can guarantee, not just that the ID I have received is an integer, but that it is an integer representing the ID of a task, and not the ID of a project.&#8221;</i></p>
<p>I&#8217;ve tried this too lately, and I really like it.  It makes code more readable than using int/long/String for IDs. As you&#8217;ve saif, ProjectId is something different than TaskId. And with a growing code base, problems will arise if every Id is String/Int/Long. (mostly because developers - if you take enough of them - won&#8217;t name every ProjectId the same, some call it ProjectId, some PID, etc. all being Longs. They can&#8217;t screw up with a ProjectId type)</p>
<p>Thanks for your long comment, thanks for the insights.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.codemonkeyism.com/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-196143</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Mon, 10 Nov 2008 22:53:54 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-196143</guid>
		<description>Wow, this post generated a lot of controversy, which I missed.  I think the most salient points are from Sean (in that case just use Ruby) and Gabriel (compiler benefit).

We are Java developers and we are using a statically typed language.  By defining types at compile time we are giving the compiler the information it needs to make certain guarantees.  We can not use booleans where Strings are expected.  We can not use Strings where doubles are expected.  When a method accepts an integer parameter, we know it is receiving one; the compiler makes and keeps that promise.

The question is, how much information do we want to give the compiler to gain this benefit?  A lot of people here seem to be arguing that we should only create classes for compound types, and for value types we should just use whatever classes are available, because (I guess) creating classes is too much work.

I haven't read Domain Driven Development (though I may, now) but this is something I've been thinking about lately.  To use an example from my project, a task ID is not a project ID, even though they're both stored in the database as number(10,0) and represented in Java by an int.  I think there is value in creating a ProjectID class and a TaskID class so that the compiler can guarantee, not just that the ID I have received is an integer, but that it is an integer representing the ID of a task, and not the ID of a project.

It is true that the IDs are populated somewhere using an integer, but it still greatly reduces the number of places in code that the knowledge that the ProjectID is implemented by an integer is embedded.  Lots of people have been saying, "YAGNI".  I don't see this as a question of building something you ain't gonna need.  That principle as I understand it is to avoid building something because you think it might add value later.  Creating these 'domain primitive' classes adds value today, the moment they are written, in the form of those compiler guarantees.  Also in the form of IDE assistance etc. which is definitely nice to have but secondary.  Less repetition, easier refactoring, and knowing that you have a ProjectID are also real benefits.

As far as saying that the code is =less= readable when using domain primitives ... I find that baffling.  How many times do I have to look up TaskID to know it is 'really' an integer?  In fact, I never have to look that up.  I know it is an integer.  Everyone working on this system knows it is an integer.  And if we have a new developer, he will have to look at the TaskID class once to see it is an integer.

Every time a developer looks at a TaskID, he will know it is an integer.  But the converse is not true.  I can be looking at an integer.  Is it a task ID?  I don't know.  Even if the variable is called taskID, I don't know.  You bring up method declarations with many variables of the same type.  It is trivially easy to mix up arguments of the same type.

The cost of this approach ... well, I will have to write a TaskID class.  I expect that to take one minute.  Developers will have to look at the TaskID class to comprehend that it is represented by an integer, if they are working on a system boundary where they care how it is implemented.  I expect that to take ten seconds.  If it saves two minutes of debugging, it's worth it.

I liken it to using generics.  When Java 5 was released, I added generics to parts of our code base and found many bugs that had been lurking there.

Thanks for this post, Stephan.  You've really given me something to think about.  I'm going to try this, so that I can more fully exploit the strict type system that is after all one of the reasons I am using Java.</description>
		<content:encoded><![CDATA[<p>Wow, this post generated a lot of controversy, which I missed.  I think the most salient points are from Sean (in that case just use Ruby) and Gabriel (compiler benefit).</p>
<p>We are Java developers and we are using a statically typed language.  By defining types at compile time we are giving the compiler the information it needs to make certain guarantees.  We can not use booleans where Strings are expected.  We can not use Strings where doubles are expected.  When a method accepts an integer parameter, we know it is receiving one; the compiler makes and keeps that promise.</p>
<p>The question is, how much information do we want to give the compiler to gain this benefit?  A lot of people here seem to be arguing that we should only create classes for compound types, and for value types we should just use whatever classes are available, because (I guess) creating classes is too much work.</p>
<p>I haven&#8217;t read Domain Driven Development (though I may, now) but this is something I&#8217;ve been thinking about lately.  To use an example from my project, a task ID is not a project ID, even though they&#8217;re both stored in the database as number(10,0) and represented in Java by an int.  I think there is value in creating a ProjectID class and a TaskID class so that the compiler can guarantee, not just that the ID I have received is an integer, but that it is an integer representing the ID of a task, and not the ID of a project.</p>
<p>It is true that the IDs are populated somewhere using an integer, but it still greatly reduces the number of places in code that the knowledge that the ProjectID is implemented by an integer is embedded.  Lots of people have been saying, &#8220;YAGNI&#8221;.  I don&#8217;t see this as a question of building something you ain&#8217;t gonna need.  That principle as I understand it is to avoid building something because you think it might add value later.  Creating these &#8216;domain primitive&#8217; classes adds value today, the moment they are written, in the form of those compiler guarantees.  Also in the form of IDE assistance etc. which is definitely nice to have but secondary.  Less repetition, easier refactoring, and knowing that you have a ProjectID are also real benefits.</p>
<p>As far as saying that the code is =less= readable when using domain primitives &#8230; I find that baffling.  How many times do I have to look up TaskID to know it is &#8216;really&#8217; an integer?  In fact, I never have to look that up.  I know it is an integer.  Everyone working on this system knows it is an integer.  And if we have a new developer, he will have to look at the TaskID class once to see it is an integer.</p>
<p>Every time a developer looks at a TaskID, he will know it is an integer.  But the converse is not true.  I can be looking at an integer.  Is it a task ID?  I don&#8217;t know.  Even if the variable is called taskID, I don&#8217;t know.  You bring up method declarations with many variables of the same type.  It is trivially easy to mix up arguments of the same type.</p>
<p>The cost of this approach &#8230; well, I will have to write a TaskID class.  I expect that to take one minute.  Developers will have to look at the TaskID class to comprehend that it is represented by an integer, if they are working on a system boundary where they care how it is implemented.  I expect that to take ten seconds.  If it saves two minutes of debugging, it&#8217;s worth it.</p>
<p>I liken it to using generics.  When Java 5 was released, I added generics to parts of our code base and found many bugs that had been lurking there.</p>
<p>Thanks for this post, Stephan.  You&#8217;ve really given me something to think about.  I&#8217;m going to try this, so that I can more fully exploit the strict type system that is after all one of the reasons I am using Java.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://www.codemonkeyism.com/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-186287</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Fri, 24 Oct 2008 04:38:20 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-186287</guid>
		<description>@Niclas: Verification has been discussed somewhere in this thread I think.

@Gabriel: Yes, I also think it makes code less error prone.</description>
		<content:encoded><![CDATA[<p>@Niclas: Verification has been discussed somewhere in this thread I think.</p>
<p>@Gabriel: Yes, I also think it makes code less error prone.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Niclas Hedhman</title>
		<link>http://www.codemonkeyism.com/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-186275</link>
		<dc:creator>Niclas Hedhman</dc:creator>
		<pubDate>Fri, 24 Oct 2008 04:05:26 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-186275</guid>
		<description>@Gabriel; You mean like;

bookTicket( new Name( "Apocalypse Now" ), new Film("Gabriel" ), new Count(1) );

;-)

The Strings are coming from "somewhere" and they can be messed up along the way, no matter what you come up with...</description>
		<content:encoded><![CDATA[<p>@Gabriel; You mean like;</p>
<p>bookTicket( new Name( &#8220;Apocalypse Now&#8221; ), new Film(&#8221;Gabriel&#8221; ), new Count(1) );</p>
<p>;-)</p>
<p>The Strings are coming from &#8220;somewhere&#8221; and they can be messed up along the way, no matter what you come up with&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gabriel C</title>
		<link>http://www.codemonkeyism.com/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-186183</link>
		<dc:creator>Gabriel C</dc:creator>
		<pubDate>Thu, 23 Oct 2008 22:37:38 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-186183</guid>
		<description>I came to the same conclusion. Although I believe that following the domain closely is a good reason, my main motivation is to avoid errors leveraging the Java type system.
Slightly modifying your example: 
bookTicket(String arg0, String arg1, int arg3) 
versus 
bookTicket(Name arg0,  Film arg2, Count arg3)

In the first case, nothing prevents me to book a ticket to see the "Gabriel" movie in the name of "Apocalypse Now"... is very easy to mix up the parameters. To catch that I have to actually write a unit test for something the compiler is giving me for free!
Even if wrapping the parameters takes the same amount of code than the test, I don't need to run it to have the result.

I believe that for non trivial projects, having the (type) contract enforced by the compiler is a good thing :)</description>
		<content:encoded><![CDATA[<p>I came to the same conclusion. Although I believe that following the domain closely is a good reason, my main motivation is to avoid errors leveraging the Java type system.<br />
Slightly modifying your example:<br />
bookTicket(String arg0, String arg1, int arg3)<br />
versus<br />
bookTicket(Name arg0,  Film arg2, Count arg3)</p>
<p>In the first case, nothing prevents me to book a ticket to see the &#8220;Gabriel&#8221; movie in the name of &#8220;Apocalypse Now&#8221;&#8230; is very easy to mix up the parameters. To catch that I have to actually write a unit test for something the compiler is giving me for free!<br />
Even if wrapping the parameters takes the same amount of code than the test, I don&#8217;t need to run it to have the result.</p>
<p>I believe that for non trivial projects, having the (type) contract enforced by the compiler is a good thing :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://www.codemonkeyism.com/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-186099</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Thu, 23 Oct 2008 21:01:35 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-186099</guid>
		<description>@Ralf: Nice try. but I've read to much Hofstadter to fall for this one.</description>
		<content:encoded><![CDATA[<p>@Ralf: Nice try. but I&#8217;ve read to much Hofstadter to fall for this one.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ralf Schneider</title>
		<link>http://www.codemonkeyism.com/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-186038</link>
		<dc:creator>Ralf Schneider</dc:creator>
		<pubDate>Thu, 23 Oct 2008 16:28:02 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-186038</guid>
		<description>That's not enough!
Be aware all your classes like Name still contain things like strings, integers, ... !!!!
Could you add another layer of indirection, please?

I'm happy people like you exists!</description>
		<content:encoded><![CDATA[<p>That&#8217;s not enough!<br />
Be aware all your classes like Name still contain things like strings, integers, &#8230; !!!!<br />
Could you add another layer of indirection, please?</p>
<p>I&#8217;m happy people like you exists!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://www.codemonkeyism.com/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-185221</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Wed, 22 Oct 2008 12:26:36 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/05/02/never-never-never-use-string-in-java-or-at-least-less-often/#comment-185221</guid>
		<description>"I doubt that you know all domains, [...]" 

No I don't you're right. 

"I just try to say, 'Balance'"

Yes, as the title is "Never, never, never use String in Java (or at least less often :-)" which was my amateur attempt to balance it :-) I totally agree with you, most often it's not an "Either/Or" choice.</description>
		<content:encoded><![CDATA[<p>&#8220;I doubt that you know all domains, [...]&#8221; </p>
<p>No I don&#8217;t you&#8217;re right. </p>
<p>&#8220;I just try to say, &#8216;Balance&#8217;&#8221;</p>
<p>Yes, as the title is &#8220;Never, never, never use String in Java (or at least less often :-)&#8221; which was my amateur attempt to balance it :-) I totally agree with you, most often it&#8217;s not an &#8220;Either/Or&#8221; choice.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
