<?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: Versioning and serialization of enums</title>
	<atom:link href="http://brian.pontarelli.com/2006/01/17/versioning-and-serialization-of-enums/feed/" rel="self" type="application/rss+xml" />
	<link>http://brian.pontarelli.com/2006/01/17/versioning-and-serialization-of-enums/</link>
	<description>Brian Pontarelli</description>
	<pubDate>Wed, 08 Sep 2010 00:32:24 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
		<item>
		<title>By: Brian Pontarelli</title>
		<link>http://brian.pontarelli.com/2006/01/17/versioning-and-serialization-of-enums/#comment-5</link>
		<dc:creator>Brian Pontarelli</dc:creator>
		<pubDate>Thu, 19 Jan 2006 22:02:50 +0000</pubDate>
		<guid isPermaLink="false">http://brian.pontarelli.com/?p=29#comment-5</guid>
		<description>&lt;blockquote&gt;
What (if any) languages are actually good at versioning? Is it always something you have to graft into the language?
&lt;/blockquote&gt;

.Net versions every Class in addition to the serialized version of the Class instance. This allows them a lot of flexibility because they have the ability to see what version a Class is and distinquish between two different versions of a Class in the VM. Python does some cool things with Class loading and scoping of Classes. The thing to keep in mind with Java is that a Class instance and the Class object only know the ClassLoader that they came from. They don't know what version they are, who called them, who they might call, what dependencies they have, how they were deployed, etc. This means that you have to do all versioning using ClassLoader magic and custom code on top of Java.

&lt;blockquote&gt;
Canâ€™t you use the serialVersionUID to determine if a enum has changed (forgive my ignorance, as I havenâ€™t done much with enums)?
&lt;/blockquote&gt;

My example actually assumes you have added a serialVersionUID to the type safe enum (non JDK 1.5). If you haven't, you won't even have the opportunity to return null or throw an exception from the readResolve method because the serialization will throw a UnmarshalException instead. Additionally, JDK 1.5 enums don't get new serialVersionUIDs when you add or subtract a enum value. This is different than type safe enum pattern that is a Java class which does get a new serialVersionUID during compilation (if one wasn't set). Regardless, using serialVersionUID throws a different runtime exception placing you back in the same tight spot and in using type safe enum pattern actually makes life a bit worse.

&lt;blockquote&gt;
Should it be a requirement that all enums have a UNKNOWN value, or that every caller should check for null (which would mean the same thing)?
&lt;/blockquote&gt;

This is an option, but one that I try to shy away from. The reason is that if your enum on the server contains values that the client can actually use than what you've actually done is place data that is operational into a static structure. Using an UNKNOWN value would have to retain the value from the enum on the server side so that the client could get at that information. I think my main point of this post is really to address the fact that most enums really shouldn't be enums unless you can 100% guarentee that you've thought of all the possible values and they WILL NOT change in the forseeable future, or ever. Thread state is a good example of a true enum. Countries is an example of a bad enum.

&lt;blockquote&gt;
Why would you have Foo be a string? Doesnâ€™t that defeat the whole point of enums (that there are only X valid values allowed)? 
&lt;/blockquote&gt;

This is the tough part. You are using an enum to ensure a constrained set, but that set is in flux. I'd much rather make the objects flyweights that are backed by a database or properties file. This can ensure that bad values aren't used, but allows client and server to be updated to work with new values without restarting the VM and changing code. There are other solutions like a reloadable resource bundle or the like as well. But, you better make sure to update the clients first and the server second ;)</description>
		<content:encoded><![CDATA[<blockquote><p>
What (if any) languages are actually good at versioning? Is it always something you have to graft into the language?
</p></blockquote>
<p>.Net versions every Class in addition to the serialized version of the Class instance. This allows them a lot of flexibility because they have the ability to see what version a Class is and distinquish between two different versions of a Class in the VM. Python does some cool things with Class loading and scoping of Classes. The thing to keep in mind with Java is that a Class instance and the Class object only know the ClassLoader that they came from. They don&#8217;t know what version they are, who called them, who they might call, what dependencies they have, how they were deployed, etc. This means that you have to do all versioning using ClassLoader magic and custom code on top of Java.</p>
<blockquote><p>
Canâ€™t you use the serialVersionUID to determine if a enum has changed (forgive my ignorance, as I havenâ€™t done much with enums)?
</p></blockquote>
<p>My example actually assumes you have added a serialVersionUID to the type safe enum (non JDK 1.5). If you haven&#8217;t, you won&#8217;t even have the opportunity to return null or throw an exception from the readResolve method because the serialization will throw a UnmarshalException instead. Additionally, JDK 1.5 enums don&#8217;t get new serialVersionUIDs when you add or subtract a enum value. This is different than type safe enum pattern that is a Java class which does get a new serialVersionUID during compilation (if one wasn&#8217;t set). Regardless, using serialVersionUID throws a different runtime exception placing you back in the same tight spot and in using type safe enum pattern actually makes life a bit worse.</p>
<blockquote><p>
Should it be a requirement that all enums have a UNKNOWN value, or that every caller should check for null (which would mean the same thing)?
</p></blockquote>
<p>This is an option, but one that I try to shy away from. The reason is that if your enum on the server contains values that the client can actually use than what you&#8217;ve actually done is place data that is operational into a static structure. Using an UNKNOWN value would have to retain the value from the enum on the server side so that the client could get at that information. I think my main point of this post is really to address the fact that most enums really shouldn&#8217;t be enums unless you can 100% guarentee that you&#8217;ve thought of all the possible values and they WILL NOT change in the forseeable future, or ever. Thread state is a good example of a true enum. Countries is an example of a bad enum.</p>
<blockquote><p>
Why would you have Foo be a string? Doesnâ€™t that defeat the whole point of enums (that there are only X valid values allowed)?
</p></blockquote>
<p>This is the tough part. You are using an enum to ensure a constrained set, but that set is in flux. I&#8217;d much rather make the objects flyweights that are backed by a database or properties file. This can ensure that bad values aren&#8217;t used, but allows client and server to be updated to work with new values without restarting the VM and changing code. There are other solutions like a reloadable resource bundle or the like as well. But, you better make sure to update the clients first and the server second ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan Moore</title>
		<link>http://brian.pontarelli.com/2006/01/17/versioning-and-serialization-of-enums/#comment-4</link>
		<dc:creator>Dan Moore</dc:creator>
		<pubDate>Wed, 18 Jan 2006 16:45:11 +0000</pubDate>
		<guid isPermaLink="false">http://brian.pontarelli.com/?p=29#comment-4</guid>
		<description>Hey Brian,

A few questions:

What (if any) languages are actually good at versioning?  Is it always something you have to graft into the language?

Can't you use the serialVersionUID to determine if a enum has changed (forgive my ignorance, as I haven't done much with enums)?

Should it be a requirement that all enums have a UNKNOWN value, or that every caller should check for null (which would mean the same thing)?

Why would you have Foo be a string?  Doesn't that defeat the whole point of enums (that there are only X valid values allowed)?</description>
		<content:encoded><![CDATA[<p>Hey Brian,</p>
<p>A few questions:</p>
<p>What (if any) languages are actually good at versioning?  Is it always something you have to graft into the language?</p>
<p>Can&#8217;t you use the serialVersionUID to determine if a enum has changed (forgive my ignorance, as I haven&#8217;t done much with enums)?</p>
<p>Should it be a requirement that all enums have a UNKNOWN value, or that every caller should check for null (which would mean the same thing)?</p>
<p>Why would you have Foo be a string?  Doesn&#8217;t that defeat the whole point of enums (that there are only X valid values allowed)?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
