Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Josh Bloch

I was reading Effective Java again - its 99% applicable to .NET also. Now there is this whole section on making defensive copies and returning clones, all intended to prevent people from violating the invariants that your class is trying to enforce.

I am 100,000% sure JB is absolutely correct in describing these vulnerabilities, but, wow, it seems like a TON of overhead both in terms of code and performance, for the remote case that someone is smart enough to try to mangle your code that way.

Few of the books/examples/etc. that I come across suggest going to these lengths, so I am wondering, is JB's advice unduly coloured by his authoring ultra-widely used API's at Sun? Or do most OO programmers really do this?

Tuesday, September 28, 2004

Oh, also, I should add, this is a Fantastic book, my skepticism about this particular piece of advice notwithstanding.

Tuesday, September 28, 2004

I would agree that these types of defensive mechanisms are primarily a concern when exposing the API to the outsite world.  In the .Net environment an example of an API that is "public" but not for reuse is the Context class in the Remoting namespace; the documentation for this class fully qualifies it with "this class is used internally by the .Net framework and is not intended to by used in your code".  Of course people could still use this code, but at their own risk.  This is in contrast to java.util.Hashtable that is a public API both technically and explicitly.

BTW... Effective Java is easily the best book on API design I've come across.


<a href=">Harris Reynolds</a>
Wednesday, September 29, 2004

I agree completely. Its unfortunate that it appears at first glance to be Java specific.

Wednesday, September 29, 2004

*  Recent Topics

*  Fog Creek Home