Adventures of a wannabe geek!

Ranting within

Functional Programming in .Net

Oliver Sturm, formerly of DevExpress, came to Edge UG on Tuesday 13th April at the Vue Cinema in Fulham Broadway. This was of course the Venue for 1 of the Microsoft UK TechDays. Oliver came to talk about what functional programming is and why it is interesting. He also brielfy spoke about F#, LINQ and TPL [although were contrained by time]

 

So What Did I Learn About Functional Programming?

Functional Programming:

 

  • Avoids state management and mutable data
  • Helps to focus on the reduction of side-effects [or tries to]

What Do We Mean By The Reduction of Side-Effects?
When a function/ method replies of global variables then it cannot be determined that that variable has not been changed before the function uses it. Of course you could write a check in the function for the correct type but this seems to be a tight coupling towards that type. Therefore the calling function may fail/ throw an exception 
	Person[] people;
    const int maxPeople = 10;
    Person[] displayPeople;
    int displayCount;

    private void InitData( ) 
	{
      people = new Person[maxPeople];
      people[0] = new Person { Name = "Anna" };
      people[1] = new Person { Name = "Chris" };
      people[2] = new Person { Name = "Willy" };
      people[3] = new Person { Name = "Hugh" };
      people[4] = new Person { Name = "Steve" };
      people[5] = new Person { Name = "Betty" };
      people[6] = new Person { Name = "Carla" };
      people[7] = new Person { Name = "John" };
      people[8] = new Person { Name = "Pete" };
      people[9] = new Person { Name = "Susan" };
      displayPeople = new Person[maxPeople];
      UpdateUI( );
    }
	
	private void UpdateUI( ) 
	{
      DisplayListBox.Items.Clear( );
      for (int i = 0; i < displayCount; i++)
	  {
        DisplayListBox.Items.Add(displayPeople[i]);
	  }
    }



So in order to keep our functions side effect free then we should pass in the variable that the function uses rather than rely on it globally.

 

const int maxPeople = 10;
    int displayCount;

    private void InitData( ) 
	{
      Person[] people = new Person[maxPeople];
      people[0] = new Person { Name = "Anna" };
      people[1] = new Person { Name = "Chris" };
      people[2] = new Person { Name = "Willy" };
      people[3] = new Person { Name = "Hugh" };
      people[4] = new Person { Name = "Steve" };
      people[5] = new Person { Name = "Betty" };
      people[6] = new Person { Name = "Carla" };
      people[7] = new Person { Name = "John" };
      people[8] = new Person { Name = "Pete" };
      people[9] = new Person { Name = "Susan" };
      Person[] displayPeople = new Person[maxPeople];
      UpdateUI(displayPeople);
    }
	
	private void UpdateUI(Person[] displayPeople) 
	{
      DisplayListBox.Items.Clear( );
      for (int i = 0; i < displayCount; i++)
	  {
        DisplayListBox.Items.Add(displayPeople[i]);
	  }
    }
This is because a function is a piece of code that should take parameters, process them and return a result. The parameters or the result can even be functions if necessary. This all accounts for a very pure function

When at Oliver’s talk i asked the question:
“How does functional programming help with unit testing and TDD?”

Oliver pointed out that this is perfect for unit testing as it has no outside influences, therefore if written correctly, it can be tested easily. This function is what we try to product when we write code via TDD - that pure function - as unit tests shouldn’t have any dependencies. 

How Does a Reduction in Side-Effects Help Us?
  • Scalability
  • Optimized
  • Tested
  • Easily Debugged
our code is modularized and potenailly clean and well refactored as the function should be pure.

Oliver showed coding examples of how you go from a function that has side effects and how this can be refactored or cleaned in order to make that function pure. As time constraints got the better of us there was not a huge amount of time to go into details of F# - we had an overview of what F# was and we seen the code samples in F#. From what I seen of it, it looks like id have to delve into that further to grasp anything firm. A small amount of time was given to Parallel in DotNet 4 - since the demo i have looked into this myself and see its very nice - blog post to follow


This was yet another talk that really helped me to think about my current practices. I would definately recommend seeing this talk if you get the opportunity as Oliver is an excellent presenter. He has very kindly put his slides on his blog. Thanks to Oliver for Code samples i have used here.

Thanks to Oliver and Gavin from Edge UG for a very worthwhile talk