Friday 16 November 2007

1 UI Control, 73 Unit Tests

When you start introducing dependencies into things, they get complicated pretty quick.
This is doubly true with validation: if one value relies on another, then you need to validate both inputs when either of them change.
If A and B depend on each other, and B and C depend on each other, then you get into really deep water.
The way I've dealt with this is by using a "propagate" flag in the validation functions, which determines whether something should call validation on its dependents.
It seems to work pretty well, but I can see this getting grizzly when theres more than one thing going on. It does help isolate each validation function to be able to validate one thing at a time.
Also, doing it test driven has helped - towards the end, the cycle was only hindered by the speed of compilation.
I'm now confident that it's a robust control that I can build other parts of the UI around.
I think using some kind microcontroller may have helped break it up a bit mind, but that's a refactoring for another day.

Friday 12 October 2007

Shuffling a List in C#

Fixed this post:

public static readonly Random random = new Random();
public static IList<T> Shuffle<T>(IList<T> toShuffle)
{
List<T> deck = new List<T> ( toShuffle );
int N = deck.Count;

for (int i = 0; i < N; ++i )
{
int r = i + (int)(random.Next(N - i));
T t = deck[r];
deck[r] = deck[i];
deck[i] = t;
}

return deck;
}

Generics Array Helper

Rather than

ObjectWithReallyLongName [] array = new ObjectWithReallyLongName [] { new ObjectWithReallyLongName(...), ... }

We can use generics to create a function:

public T[] Array<T> ( params T[] array )
{
return array;
}

used

ObjectWithReallyLongName [] array = Array(new ObjectWithReallyLongName(...), ...);

Seems to read better too.


Wednesday 26 September 2007

Installing Infragistics NetAdvantage on Vista

Note to self: To do this successfully, you need to turn off UAC. If you don't it won't install properly.

Monday 17 September 2007

Take a bath...

From Pi:


SOL Remember Archimedes of Syracuse? The King asks Archimedes to determine if a present he's received was actually solid gold. Unsolved problem at the time. It tortures the great Greek mathematician for weeks. Insomnia haunts him and he twists and turns on his bed for nights on end. Finally, his equally exhausted wife, she's forced to share a bed with this genius, convinces him to take a bath, to relax. While stepping into the tub he observes the bathwater rise as he enters. Displacement. A way to determine volume. And thus, a way to determine density, weight over volume. And thus, Archimedes solves the problem. He screams "Eureka!"—Greek for "I found it!"—and is so overwhelmed he runs dripping naked through the streets to the King's castle to report his discovery. Now, what's the moral of the story.
MAXThat a breakthrough will come...
SOL Wrong. The point of the story is the wife. Listen to your wife, she will give you perspective. Meaning, you need a break, Max, you have to take a bath, otherwise you'll get nowhere. There will be no order, only chaos. Go home and take a bath.

I had an Archimedes moment over the weekend. I'd been struggling to work out the problem all last week. Came in this morning and got it nailed in a few hours.

Take a bath ;)

Enum

inspired by Enum, here's my version... it still needs to work with resources, but works otherwise.


public static class Enum <T>
{

public static T Parse ( string name )
{
return ( T ) Enum.Parse ( typeof ( T ), name );
}

public static IEnumerable < T > GetValues ( )
{
foreach ( T value in Enum.GetValues ( typeof ( T ) ) )
{
yield return value;
}
}


public static IEnumerable<AttributeType> GetAttributes <AttributeType> ( T value ) where AttributeType : Attribute
{
FieldInfo fieldInfo = typeof ( T ).GetField ( value.ToString ( ) );


if (fieldInfo != null)
{
foreach ( object customAttribute in fieldInfo.GetCustomAttributes ( typeof ( AttributeType ), false ) )
{
yield return customAttribute as AttributeType;
}
}
}

public static IList<AttributeType> GetAttributeList <AttributeType> ( T value ) where AttributeType : Attribute
{
return new List<AttributeType> ( GetAttributes<AttributeType> ( value ) );
}

public static string GetDescription( T value )
{
IList< DescriptionAttribute > attributeList = GetAttributeList<DescriptionAttribute> ( value );

if (attributeList.Count < 1)
return value.ToString();

return attributeList[ 0 ].Description;
}
}

Sunday 9 September 2007

Tools

Programmers love tools - just to write a program, you need at least three: text editor; compiler; and, a linker - and that's excluding the computer and the operating system!
However, as with any other craft, there are certain rules you need to follow when using tools:
  1. Use the right tool for the job
    Obviously, you wouldn't try to edit your text with a linker, but even-so...
  2. Know how to use your tools
    The same way you can take your fingers using a circular saw incorrectly, you can run your project into a lot of trouble by abusing your tools.
  3. Use the best tools you can get your hands on
    It's not just about cost - you can get good and cheap tools, but you can also get expensive shit.
  4. Look after your tools
    You main tool as a programmer is your computer. Keep it clean and sharp and it'll be a lot easier to use.

Wednesday 4 July 2007

.Net Testing And Assemblies Signed with Strong Names

.Net Testing And Assemblies Signed with Strong Names

An old post about clearing the GAC download cache when unit testing signed assemblies: gacutil /cdl

Friday 29 June 2007

Welcome To Beardy Box

This blog is my "programmers notes" blog; somewhere I can put all programming and development stuff I think about and not bore my non-technical friends with.
At the moment, as you can see, it's blank, but hopefully it will become a mine of useful information*.

*some chance!