Tuesday, 17 June 2008

SyntaxHighlighter

As you can see, I've used SyntaxHighlighter to smarted up the posted code.

One thing I found was that Blogger likes to put "<br>" elements in any "<pre>" code, so I wrote a little javascript to replace them with newlines:

(function(){var pres = document.getElementsByTagName('pre');
for ( var i = 0; i < pres.length; ++i ) {
for ( var br = pres[i].childNodes.length; br > 0; --br ) {
if ( pres[i].childNodes[br-1].nodeName == 'BR' ) {
pres[i].replaceChild(document.createTextNode('\n'), pres[i].childNodes[br-1] );
}
}
}})();

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;
}
}