From the category archives:

Programming

When creating a .NET object that you need to be able to make copies of, it seems fairly intuitive, at least on the surface, that you would want to implement the ICloneable interface. Surprisingly enough, Microsoft actually recommends against implementing this interface:

Do not implement ICloneable

There are two general ways to implement ICloneable, either as a deep, or non-deep copy. Deep-copy copies the cloned object and all objects referenced by the object, recursively until all objects in the graph are copied. A non-deep copy (referred to as ‘shallow’ if only the top level references are copied) may do none, or part of a deep copy.

Because the interface contract does not specify the type of clone performed, different classes have different implementations. A consumer cannot rely on ICloneable to let them know whether an object is deep-cloned or not.

If you need a cloning mechanism, define your own Clone, or Copy methodology, and ensure that you document clearly whether it is a deep or shallow copy.

My personal preference for handling my object copying needs is to define explicit ShallowCopy() and DeepCopy() methods, so that there is no question which type of object copy operation is going to be performed.

The other problem I have with the ICloneable interface is that the Clone() method returns a plain object, resulting in the need to perform a type cast on every call. Since there is no support for generics in the ICloneable interface, a developer is unable to explicitly define the return type of their Clone() method. However, if you choose to explicitly define your own ShallowCopy() and DeepCopy() methods, they can be strongly typed to your class, so that no type cast is required.

Why Microsoft hasn’t deprecated the ICloneable interface at this point is beyond me. They really should go beyond simply advising against its use, and officially deprecate the interface. This would allow them to define a much more explicit, and more importantly, an “opinionated” interface that would promote better programming practices.

{ 0 comments }

I recently wrote about searching Active Directory users with C#, and mentioned that the next part of the equation was searching Active Directory groups, and returning all the users in the group. Since groups can contain other groups, we must perform this search recursively. In order to avoid the infinite recursion possibility, I added a search depth parameter, and set the maximum search depth to 5. The code I used is below, and you’ll probably notice that it’s pretty similar to my code for searching Active Directory users.

Read the full article →

{ 0 comments }

Adding custom JavaScript to a SharePoint Form

March 15, 2009 Programming

Recently I was implementing a simple bug tracker in the form of a SharePoint list, and I wanted to add some simple JavaScript to the bug entry form that captured the referring page. This seemed like a simple enough feature, but as it turned out, using JavaScript within a SharePoint form isn’t exactly straight-forward.

Read the full article →

Searching for an Active Directory user with C#

March 2, 2009 Programming

Recently I had the need to add a piece of functionality to one of my web applications that allowed for users to search through Active Directory. This turned out to not be quite as simple as I would have thought, mostly due to lack of documentation. I figured I would share what I [...]

Read the full article →

You are using source control, aren’t you?

February 16, 2009 Programming

For something that is so important to the software development process, I am continually amazed at how many developers are still not using source control. There’s a good reason why using source control is #1 on The Joel Test: any development team that is writing coding without it is asking for trouble.
One of the [...]

Read the full article →

Unit tests are only as useful as you make them

February 3, 2009 Programming

There are many developers out there who stress the importance of unit tests, both in terms of Test-Driven Development (TDD), and in writing stand-alone tests for an existing codebase. Some developers go so far as to promote 100% code coverage. Now, I’m all for writing well-designed, testable code, but insisting on 100% code [...]

Read the full article →