Disable copy construction when the type is not copy safe

A couple of days ago I finished coding up a feature in our C++ code base, hit F5 and was met with a nasty memory corruption debugger dialog.  After about an hour of investigation it appeared one of my types was living past the lifetime of it’s owning heap. I decided the next step was…

5

Do not throw a NullReferenceException when validing “this” in an extension method

One pattern I’ve started running into is developers explicitly throwing a NullReferenceException when validating the “this” parameter of an extension method. public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action) { if (null == enumerable) { throw new NullReferenceException(); } // rest omitted } The desired behavior here is to make extension methods look even more…

3

Using lambdas to create generic factories

One item I find to be limiting in C# is the new generic constraint.  The syntax construct specifies that the type backing a given generic parameter contains a parameter less constructor.   It allows methods to create instances of generic parameters in a type safe manner. public static void Example<T>() where T : new() { var…

4

Making Equality Testing Simple

Getting equality correct on a .Net type is a fairly involved process involving adherence to a large set of rules in order to be considered correct.  Including Object.Equals overrides on reference types must return false for null values Object.Equals overrides must return false for incompatible types Excluding null cases x.Equals(y) must be the same as…

4

Having fun with events in F#

Recently I ran into a situation where I needed to handle some events in F# in a special way.  In this particular case I wanted to be able to disable and re-enable my handler based on changes in the program.  Essentially the C# equivalent of continually adding and removing the handlers.  I started by using…

1

If you implement IEquatable<T> you still must override Object’s Equals and GetHashCode

CLR 2.0 introduced IEquatable<T> which is an interface that allows for type safe equality comparisons.  Previously, the best available method for comparing equality was the virtual Object Equals method.  The method is loosely typed since it takes an object as a parameter.  This is easy enough to deal with on the client with a simple…

0

Comparing Continuations in F# and C#

Lately I’ve been playing quite a bit with F#.  I have several hobby projects I’m working on that take up a bit of my time.  But when I’m not playing around with F# I’m exploring ways to apply certain functional patterns to actual coding on the job and/or porting to my functional library: RantPack. Recently…

4

Custom Exceptions: When should you create them?

I think the best answer is: rarely.   It’s really hard to go straight to a justification here though.  I find that answering a different question will eventually shed led on when to create a new exception. "What are the benefits of creating a new/custom exception?" The answers I come up with or have heard before…

5

If it’s not broken, maybe you should fix it anyway

I know this is goes against conventional wisdom but it’s something I believe in.  Every sufficiently large project has that section of code nobody wants to go near.  The mere mention of it causes people to leave the room.  It usually has a couple of properties Old and Stagnant Critical or important routine Fragile Written…

2

Unfold

F# has a handy method called Unfold.  Think of it as the logical opposite of an Aggregate function.  Aggregates take a sequence of elements and convert them to a single element.  An unfold method will take a single element and turn it into a (potentially infinite) sequence of elements.  The API is straight forward.  It…

3