Software Engineering : This category relates to interesting computer related stuff that I am researching or reading about. Most of it is in the area of .NET technologies which is the focus of most of my computer related time at the moment.
Updated: 9/21/2004; 3:35:46 PM.

 








Subscribe to "Software Engineering" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

Subscribe To
Mark's Weblog

 
 

Sunday, September 07, 2003

Don't use generics simply to bury a cast operation
Google Search It

I glanced briefly at Eric Gunnerson's post entitled Hangin' with generics.  I think it is worth pointing out that using a cast within a generic method should be done with caution.  At first it is appealing may seem appealing but in cases such as Eric's, where there is not way to specify a constraint on the cast, developers should be careful.

Take the following example:

public static T Deserialize<T>(Stream stream, IFormatter formatter)

{

      return (T)formatter.Deserialize(stream);

}

A user of this method would not know the internal implementation but be very excited to use it because it represents strongly typed deserialization.  Unfortunately, however, it is not strongly typed deserialization at all.  Rather it simply hides and internal cast making the users less likely to check for an InvalideCastException.

Using the generic version of Deserialize would look something like this:

string greeting = Deserialization.Deserialize<string>(stream, formatter);

However, I suspect that this is not preferable to the non-generic call:

string greeting = (string)Deserialization.Deserialize(stream, formatter);

I believe Eric casts appropriately but if another reasonable way could be found in which the cast was explicit I think it would be preferable.

The moral of the story:  Don't use generics simply to bury a cast operation.


6:33:42 AM   []    comment []

© Copyright 2004 Mark Michaelis.



 


September 2003
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
Aug   Oct


Recent Posts