Thursday, September 20, 2007

The Scientific Method of Programming

The scientific method is a body of techniques for investigating phenomena, acquiring new knowledge, or correcting and integrating previous knowledge. The emphasis of the method has always been on seeking truth! The method consists of a series of steps that will show that what we believe to be true is actually true. The steps are shown below:

  1. Define the question.
  2. Gather information and resources (observe).
  3. Form hypothesis.
  4. Perform experiment and collect data.
  5. Analyze data.
  6. Interpret data and draw conclusions that serve as a starting point for new hypotheses.
  7. Publish results.
  8. Retest (frequently done by other scientists).
So how does this relate to programming?
  1. Identify the need.
  2. Identify the task, talk to information holders, disect the problem.
  3. Set up assertions and tests.
  4. Write the code and run the tests.
  5. Validate the tests.
  6. Did the tests pass? Where the assertions correct? If not, correct it.
  7. Publish results (frequently done by Cruise Control).
  8. Retest (frequently done by Cruise Control).
The scientific method of programming is known by another name. It is Test Driven Development or TDD for short.

Tuesday, September 11, 2007

Combining DataTriggers and Property Triggers in WPF

How to combine property-triggers and data-triggers in WPF is not quite clear from the documentation.

Property-trigger that changes the color of a buttons background

A property-trigger is a trigger that works with the visisble element properties of WPF. Button has among others an IsMouseOver-property.

<Style TargetType="{x:Type Button}">
 <Setter Property="Button.Background" Value="AliceBlue" />
  <Trigger Property="IsMouseOver" Value="True">
   <Setter Property="Button.Background" Value="Yellow" />

Data-trigger that changes the color of a buttons background

A data-trigger is a trigger that works with the properties of my own objects. My user object below has a Name and a Role property.

public class User {
    private string name;
    private string role;

    public string Name {
        get { return name; }
        set { name = value; }

    public string Role {
        get { return role; }
        set { role = value; }

<Style TargetType="{x:Type Button}">
  <DataTrigger Binding="{Binding Path=Role}" Value="Admin">
   <Setter Property="Button.Background" Value="Red" />

A combined property-trigger and a data-trigger that changes the color of a buttons background

If I want to combine the two triggers above to check if the role is admin and the mouse is over there is no straightforward way to do this, so I have to resort to this.

<Style TargetType="{x:Type Button}">
    <!-- This binding refers to the actual WPF element. -->
    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True"/>
    <Condition Binding="{Binding Path=Role}" Value="Admin"/>
   <Setter Property="Button.Background" Value="Orange" />

Sunday, June 17, 2007

NullToBooleanConverter for WPF

I have found it useful to enble or disable WPF elements based on if they are null or not. This is not supported out of the box so I need a simple value converter. The simple value converter has been updated to support all things that has a Count property.

public class NullToBooleanConverter : IValueConverter
   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
       if (value == null) return false;
       PropertyInfo propertyInfo = value.GetType().GetProperty("Count");
       if (propertyInfo != null)
           int count = (int) propertyInfo.GetValue(value, null);
           return count > 0;
       if (!(value is bool || value is bool?)) return true;
       return value;

   public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
       return value;

The simple value converter above allows me to disable any element with the following syntax in XAML.

<local:NullToBooleanConverter x:Key="nullToBooleanConverter"/>

<Button IsEnabled="{Binding Path=Owned, Converter={StaticResource nullToBooleanConverter}}" Content="Tapir"/>

The converter can of course be used for any other property taking a boolean argument.

Wednesday, May 30, 2007

Avoid unwanted synchronization: IsSynchronizedWithCurrentItem= False

All Selectors in WPF have a property named IsSynchronizedWithCurrentItem. The documentation reads:
Gets or sets a value that indicates whether a Selector should keep the SelectedItem synchronized with the current item in the Items property.
This makes me think that I need this set to have the selected item is sync with the current in the items property. This is not the case. If you set the property your selection will be in sync with all selectors sharing the same Items or ItemsSource. If you do not want this synchronization across all selectors sharing the same collection simply set the property to false.

Thursday, May 10, 2007

Adaptive or Rigid?

Adaptive People, Rigid Management

Adaptive People and Management