Windows Support Number

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Saturday, 22 September 2012

Applying LINQ principles to business logic

Posted on 10:27 by Unknown
This was inspired by a conversation I was having with @leeoades & @hamishdotnet at work earlier this week, we were discussing the use of Yield keyword to short-cut method execution. Lee has a great post about sequences here. I wanted to get down how my thinking about the Yield keyword has now changed.

Historically I've always thought about using the yield when I'm defining a custom enumerator method, something like GetFooEnumerator - typically I'd have placed this on some kind of model object to allow the easy iteration of some child model instances. Lee was saying why not use yield where ever you have the following usage pattern:

   1:  public IEnumerable<int> Range(int start, int end)
   2:  {
   3:      var numbers = new List<int>();
   4:      for (var i = start; i < end; i++)
   5:      {
   6:          // Do some business logic here...
   7:          numbers.Add(i);
   8:      }
   9:   
  10:      return numbers;
  11:  }

When ever you have a method which internally creates & populates a collection and then returns the collection why not use yield to make it more efficient?

This got me thinking, what kind of improvement efficiency are we talking about?

Time for a quick test, first we need a couple of methods to compare, first one very similar to above and another making use of the yield keyword but both achieving the same logic - returning a collection of numbers to the caller:

   1:  public class Numbers
   2:  {
   3:      public IEnumerable<int> Range(int start, int end)
   4:      {
   5:          var numbers = new List<int>();
   6:          for (var i = start; i < end; i++)
   7:          {
   8:              numbers.Add(i);
   9:          }
  10:   
  11:          return numbers;
  12:      }
  13:   
  14:      public IEnumerable<int> YieldRange(int start, int end)
  15:      {
  16:          for (var i = start; i < end; i++)
  17:          {
  18:              yield return i;
  19:          }
  20:      }
  21:  }

Then a test program - use each method as part of LINQ call to validate the returned sequences contain the number 420:

   1:  static void Main(string[] args)
   2:  {
   3:      Console.WriteLine();
   4:   
   5:      var numbers = new Numbers();
   6:   
   7:      var sw = new Stopwatch();
   8:      sw.Start();
   9:      var found1 = numbers.Range(0, 999).Any(n => n == 420);
  10:      sw.Stop();
  11:   
  12:      Console.WriteLine("Normal Loop: ticks = " + sw.ElapsedTicks);
  13:   
  14:      var sw2 = new Stopwatch();
  15:      sw2.Start();
  16:      var found2 = numbers.YieldRange(0, 999).Any(n => n == 420);
  17:      sw2.Stop();
  18:             
  19:      Console.WriteLine("   Yielding: ticks = " + sw2.ElapsedTicks);
  20:      Console.ReadLine();
  21:  }

Running this up I got the following output!
What!

I wasn't expecting it to be slower, after all the YeildRange method only has to iterate 420 times before finding a match where as the Range method has to iterate over all 1000 numbers before returning...

Then @hamishdotnet pointed out the cost of yielding and I remembered the chapter in Jon Skeet's C# In Depth about iterator blocks and how a set of classes are created by the compiler when ever you use the yield keyword.

Then I had the realisation the methods didn't really have any 'business logic' - DOH!

Basically the 2 implementations don't do anything, so to simulate this I put a Thread.Sleep in to represent an I/O or processor bound call:

   1:  public class Numbers
   2:  {
   3:      public IEnumerable<int> Range(int start, int end)
   4:      {
   5:          var numbers = new List<int>();
   6:          for (var i = start; i < end; i++)
   7:          {
   8:              System.Threading.Thread.Sleep(10);
   9:              numbers.Add(i);
  10:          }
  11:   
  12:          return numbers;
  13:      }
  14:   
  15:      public IEnumerable<int> YieldRange(int start, int end)
  16:      {
  17:          for (var i = start; i < end; i++)
  18:          {
  19:              System.Threading.Thread.Sleep(10);
  20:              yield return i;
  21:          }
  22:      }
  23:  }

More promising results now...

So what do I mean by 'Applying LINQ principles to business logic'?

By thinking about concepts such as 'lazy evaluation' & 'materialization' when designing your methods you allow for the opportunities for your code to be more efficiently executed when used as part of any linq queries.
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in C#, Development, LINQ | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Unit testing Rx methods Timeout & Retry with moq
    Earlier this week I was trying to unit test an asynchronous service (Foo) which used another asynchronous service (Bar) internally and ran i...
  • StructureMap: ILifecycle
    The other day I wanted to control the scope of a service inside a web based app with semantics which didn't fit either 'HttpContextS...
  • How many pins can Bing Maps handle in a WP7 app - part 1
    part2 -  http://awkwardcoder.blogspot.com/2011/10/how-many-pins-can-bing-maps-handle-in.html part3 -  http://awkwardcoder.blogspot.com/2011/...
  • MVVM anti-pattern: Injecting the IoC container into a View Model
    This is another anti-pattern I've seen a lot recently, the dynamic use of the IoC container inside a view model to resolve child view mo...
  • faking data in WP7 and other .Net platforms
    I needed to fake some data for a WP7 app yesterday and I was about to write a couple of classes when I thought why not check out what's ...
  • Implementing a busy indicator using a visual overlay in MVVM
    This is a technique we use at work to lock the UI whilst some long running process is happening - preventing the user clicking on stuff whil...
  • Daily Dilbert Service - the most important service I've ever written...
    NuGet package available here ... First off a big shout to  @hamish  &  @leeoades  on this one - I'm just blogging about it. At work ...
  • Implementing a message box using a visual overlay in MVVM
    I've blogged about implementing a busy indicator before, this post is an extension of this pattern to implement a message box - this is...
  • Observations on web service design for mobile devices
    This post was prompted by my use of back end third party services for the FINDaPAD app and my recent set of posts about push pins and Bing ...
  • WP7Contrib: Isolated Storage Cache Provider
    15/04/2001 - The code for this example has been updated - new WP7 build of SilverlightSerializer, this has removed the explicit implementati...

Categories

  • .Net
  • .Net 4.5
  • Abstractions
  • Advertising
  • Agile
  • Agile Courage
  • AOP
  • Async
  • automated testing
  • Azure
  • Azure IIS RESTful development
  • BDD
  • Bing Maps
  • Bounded Context
  • C#
  • C# 5.0
  • Caching
  • Chocolatey
  • CLoud
  • CodePlex
  • Coding
  • Coding Building CI Testing
  • Coding C#
  • coding C# IoC StructureMap
  • Coding Functional-Programming
  • Coding REST Knowledge
  • Coding Services
  • Coding TDD Refactoring Agile
  • Command
  • continuous testing
  • coupling
  • CultureInfo
  • DAL
  • databases
  • DDD
  • DDD Coaching
  • DDD Domain Events Auditing nHibernate
  • DDD Entities Value Objects
  • Debugging
  • Design Patterns
  • Design Patterns Databases Auditing
  • Developement
  • Development
  • Development Coding
  • Development Process
  • Development unit testing
  • Development VS 2011
  • Diagnostics
  • Disposable
  • Exceptions
  • FINDaPAD
  • FindaPad Property Rental Windows Phone 7 Mobile Devices
  • Fun Coding Duct-Tape
  • Hotfixes
  • integration testing
  • IoC
  • jasmine
  • javascript
  • Jobs Development
  • LINQ
  • marketplace
  • Mobile Devices
  • Mocking
  • MSDN Coding
  • MSpec
  • Multilingual
  • MVC
  • MVVM
  • nCrunch
  • nHbiernate Repository Pattern Criteria
  • nHibernate Auditing Design Fluent
  • nHibnerate Entities Events Listeners
  • node.js
  • nodes.js
  • Nokia
  • NoSQL RavenDB Azure Development
  • Observations
  • OO
  • ORM
  • Performance
  • Portable Class Library
  • Portable Library
  • PostSharp
  • Process
  • Rants
  • RavenDB IIS 7.5 Development
  • Reactive
  • Reactive Extension
  • Reactive Extensions
  • ReadOnlyCollections
  • Resharper
  • REST Distributed-Systems
  • REST HTTP
  • rest web
  • RESTful
  • Rx
  • Serialization
  • Silverlight
  • Silverlight Installation
  • Task
  • TDD
  • TDD IoC DI
  • TDD Mocking
  • TDD Team Observation
  • Telerik
  • testing
  • threading
  • TPL
  • UI
  • Undo-Redo
  • unit testing
  • ViewModels
  • VS 2012
  • wcf
  • web api
  • Web Services
  • web services mobile devices data
  • WebAPI
  • Windows
  • Windows 8
  • windows phone
  • Windows Phone 7
  • WP7
  • WP7 Bing Maps Development Network HTTP
  • WP7 Bing Maps Development UK Crime
  • WP7 Bing Maps Development UK Crime Clustering
  • WP7 Bing Maps Development UK Polygons Clustering Performance
  • WP7 cryptography bouncy castle
  • WP7 Cultures C#
  • WP7 feedback development app store
  • WP7 Javascript web browser
  • WP7 MSBuild
  • WP7 ORM Databases performance
  • WP7 Serialisation
  • WP7 SilverlightSerializer C#
  • WP7 sqlite performance development
  • WP7 WP7Contrib Bing Maps Development
  • WP7 WP7Contrib Bing Maps Polygon Development
  • WP7 WP7Contrib CodePlex
  • WP7 WP7Contrib CodePlex Bing Maps Development
  • WP7 WP7Contrib CodePlex ObservableCollection
  • WP7 WP7Contrib ILMerge .Net
  • WP7 WP7Contrib Phone Maps
  • WP7 WP7Contrib SilverlightSerializer C#
  • WP7Contrib
  • WP7Contrib Bing Maps WP7
  • WP7Contrib WP7 Geo-Location development C#
  • WP7Contrib WP7 HTTP Compression
  • WP7Contrib WP7 Url Development Rx
  • WP7Dev
  • WPF
  • WPF Cultures
  • WuApi
  • XAML

Blog Archive

  • ►  2013 (16)
    • ►  November (5)
    • ►  September (3)
    • ►  August (1)
    • ►  July (1)
    • ►  June (3)
    • ►  May (2)
    • ►  January (1)
  • ▼  2012 (44)
    • ►  November (2)
    • ►  October (8)
    • ▼  September (5)
      • Listening to a single value from an Rx observable ...
      • Applying LINQ principles to business logic
      • How do I flatten an enumerable IObservable to IObs...
      • Does the Rx subscriber get disposed when OnComplet...
      • Using PostSharp for AOP with Reactive Extensions -...
    • ►  August (2)
    • ►  July (4)
    • ►  June (3)
    • ►  May (1)
    • ►  April (2)
    • ►  March (13)
    • ►  February (4)
  • ►  2011 (52)
    • ►  December (3)
    • ►  November (5)
    • ►  October (7)
    • ►  September (7)
    • ►  August (11)
    • ►  July (4)
    • ►  May (2)
    • ►  April (1)
    • ►  March (5)
    • ►  February (3)
    • ►  January (4)
  • ►  2010 (1)
    • ►  August (1)
  • ►  2009 (32)
    • ►  December (3)
    • ►  November (7)
    • ►  October (6)
    • ►  September (11)
    • ►  April (1)
    • ►  March (4)
Powered by Blogger.

About Me

Unknown
View my complete profile