Creating enum which sucks

April 22nd, 2009

We create many enumerations in our programs. But sometimes we can make a mistake that drives somebody else mad.

Let's suppose that the first programmer created an enum like this:

C#:
  1. enum Mood
  2. {
  3.     Unknown = -1,
  4.     Awesome,
  5.     Cool,
  6.     Bad,
  7.     Awful
  8. }

It does not look bad at the first look, yet another enum.

Another programmer created a list:

C#:
  1. List<mood> availableMoods = new List<mood>();
  2. availableMoods.Add(Mood.Awful);
  3. availableMoods.Add(Mood.Bad);

Then he would like to find there a first mood type which is a part of "good mood" group and pass the result to another layer of an application. He wrote the code:

C#:
  1. Mood found = availableMoods.Find(
  2.     delegate(Mood f)
  3.     {
  4.         return f == Mood.Awesome || f == Mood.Cool;
  5.     }
  6. );

Now he realized that it cannot work because Find method of a List(T) class returns default(T) when a list does not contain desired item. In this case he would get Mood.Awesome because it is the default value of the enum. I do not need to add that the Mood.Awesome is not even the part of the list.

We can solve this problem by several different approaches.

  1. Create different enum where the default element will be "Unknown = 0"
  2. Use FindIndex method of List(T) class so we will be able to handle the situation of not found element
  3. I believe that other approaches could work as well

The question to discuss - which programmer made a mistake? The first one who created "unfortunate" enum or the second one who used wrong structure / wrong searching method?

Unit testing and objects equality

February 16th, 2009

Let's assume that we have a class like below:

C#:
  1. public class Foo : IEquatable<Foo>
  2.     {
  3.         private int variable;
  4.  
  5.         public int Variable
  6.         {
  7.             get { return variable; }
  8.             set { variable = value; }
  9.         }
  10.  
  11.         #region IEquatable<Foo> Members
  12.  
  13.         public bool Equals(Foo other)
  14.         {
  15.             return this.variable == other.Variable;
  16.         }
  17.  
  18.         #endregion
  19.     }

and a unit test for that:

C#:
  1. [TestClass()]
  2.     public class FooTest
  3.     {
  4.         [TestMethod()]
  5.         public void EqualsTest()
  6.         {
  7.             Foo one = new Foo();
  8.             one.Variable = 5;
  9.             Foo two = new Foo();
  10.             two.Variable = 5;
  11.  
  12.             Assert.AreEqual<Foo>(one, two, "AreEqual<Foo> failed");
  13.             Assert.AreEqual(one, two, "AreEqual failed");
  14.             Assert.AreSame(one, two, "AreSame failed");
  15.         }
  16.     }

and a question is - which Assert fails? All of them o_O
I could not find an explanation why implemented Equals method is not called. Framework always calls default Equals(object)

Ballet of PES

February 8th, 2009

My PES replays - Enjoy ;)

Programmers dialog

January 8th, 2009

Perfect dialog which I heard today at work (translated from Polish but the meaning is the same):

D - database programmer, P - C# programmer

D: OK, it should probably work
P: It seems that it probably works

I'm afraid that result of their work will be moved to a production environment in a next few days :D

Shadow Mapping

December 14th, 2008

I added new effect to my effects library - Shadow Mapping. Simple version without any additional techniques for soft edges.

Go to the Shadow Mapping page

Designed by SirMike © All rights reserved

Valid XHTML 1.0! Valid CSS!

Powered by Rootnode