Jan 4

Feliz Navidad y Feliz Año Nuevo


Jan 4

I wish you a happy new year 2012.

S.


Feb 11

In his article, Mark Seemann [1] deals with the TDD approach, and he wondered if the TDD is a good design Method. For attempting to answer this question, his compare TDD to S.O.L.I.D. design.

References :

[1] : Mark Seemann – The TDD apostate


Feb 11

I just found a great series of articles about Asynchronous Programming in C# [1].

Summary :

Asynchrony in C# 5, Part Eight: More Exceptions
(In this post I’ll be talking about exogenous , vexing , boneheaded and fatal exceptions. See this post for a definition of those terms .) If your process experiences an unhandled exception then clearly something bad and unanticipated has happened. If its a fatal exception then you’re already in no position…
Asynchrony in C# 5, Part Seven: Exceptions
Resuming where we left off (ha ha ha!) after that brief interruption: exception handling in “resumable” methods like our coroutine-like asynchronous methods is more than a little bit weird. To get a sense of how weird it is, you might want to first refresh your memory of my recent series on the design…
Asynchrony in C# 5 Part Six: Whither async?
A number of people have asked me what motivates the design decision to require any method that contains an “await” expression to be prefixed with the contextual keyword “async”. Like any design decision there are pros and cons here that have to be evaluated in the context of many different competing…
Asynchrony in C# 5 Part Five: Too many tasks
Suppose a city has a whole bunch of bank branches, each of which has a whole bunch of tellers and one gofer. There are a whole bunch of customers in the city, each of whom wants to withdraw a whole bunch of money from the bank at some varying time throughout the day. The algorithm goes like this: A customer…
Asynchrony in C# 5.0 part Four: It’s not magic
Today I want to talk about asynchrony that does not involve any multithreading whatsoever. People keep on asking me “but how is it possible to have asynchrony without multithreading?” A strange question to ask because you probably already know the answer. Let me turn the question around: how is it possible…
Asynchrony in C# 5, Part Three: Composition
I was walking to my bus the other morning at about 6:45 AM. Just as I was about to turn onto 45th street, a young man, shirtless, covered in blood ran down 45th at considerable speed right in front of me. Behind him was another fellow, wielding a baseball bat. My initial thought was “holy goodness, I…
Asynchronous Programming in C# 5.0 part two: Whence await?
I want to start by being absolutely positively clear about two things, because our usability research has shown this to be confusing. Remember our little program from last time? async void ArchiveDocuments(List<Url> urls) { Task archive = null; for(int i = 0; i < urls.Count; ++i) { var document…
Asynchrony in C# 5, Part One
The designers of C# 2.0 realized that writing iterator logic was painful. So they added iterator blocks. That way the compiler could figure out how to build a state machine that could store the continuation – the “what comes next” – in state somewhere, hidden behind the scenes, so that you don’t have…
Continuation Passing Style Revisited Part Five: CPS and Asynchrony
Today is when things are going to get really long and confusing. But we’ll make it through somehow. Consider the following task: you’ve got a list of URLs. You want to fetch the document associated with each URL. (Let’s suppose for the sake of argument that this always succeeds.) You then want to make…
Continuation Passing Style Revisited Part Four: Turning yourself inside out
The obvious question at this point is: if CPS is so awesome then why don’t we use it all the time? Why have most professional developers never heard of it, or, those who have, think of it as something only those crazy Scheme programmers do? First of all, it is simply hard for most people who are used…
Continuation Passing Style Revisited Part Three: Musings about coroutines
Last time I sketched briefly how one might implement interesting control flows like try-catch using continuations; as we saw, the actual implementations of Try and Throw are trivial once you have CPS. I’m sure that you could extend that work to implement try-catch-finally. Or, another basic exercise…
Continuation Passing Style Revisited Part Two: Handwaving about control flow
Last time on Fabulous Adventures: “ But we can construct arbitrarily complex control flows by keeping track of multiple continuations and deciding which one gets to go next. ” Let’s look at an example of something more complex than a conditional. Consider a simplified version of “try-catch”, where there…
Continuation Passing Style Revisited, Part One

References :

[1] Eric Lippert : Asynchronous Programming in C#, The Series


Nov 3

Today, I read an excellent article about NotifyingObject on WPF, Tomer Shamam [1] provides a very nice solution to use the NotifyingObject with lambda expressions.

He adds some nice features like callback, coercing, validation, and other goodies from WPF DependencyProperty.

References :
[1] : Tomer Shamam, NotifyingObject for WPF & Silverlight


Oct 30

In his weblog, Jeffrey Richter [1] explains that a customer wanted to increase the performance of his code and the best way to accomplish this was to have multiple threads processing their own data independently of each other. But, again, the class library code base would produce errant results if we did this.

In this excellent article, Jeffrey Richter demonstrated the problem of data isolation in multi-threaded environment. Two distinct sources (threads) want to change the same data. To simulate this, both threads use a static generic list.

To correct the problem, Jeffrey Richter uses proxies over AppDomain (see the Chris Brumme article [2]) for isolation. I think that it’s a great job to isolate data and it’s a good pattern to do this.

However in this case, we have another way to correct  the data isolation problem by using the [TreadStatic] attribut. A static field marked with ThreadStaticAttribute is not shared between threads. Each executing thread has a separate instance of the field, and independently sets and gets values for that field. If the field is accessed on a different thread, it will contain a different value.

Do not specify initial values for fields marked with ThreadStaticAttribute, because such initialization occurs only once, when the class constructor executes, and therefore affects only one thread. If you do not specify an initial value, you can rely on the field being initialized to its default value if it is a value type, or to a null reference if it is a reference type.

I would like to illustrate this with the exemple below :

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. ThreadPool.QueueUserWorkItem( (object target) => { NonConcurrentAlgorithm.Add("Item1"); } );
  6. NonConcurrentAlgorithm.Add("Item2");
  7. Thread.Sleep(1000); // To demonstrate the problem has been fixed
  8. NonConcurrentAlgorithm.GetItems().ForEach( (String name) => { Console.WriteLine(name); } );
  9. Console.WriteLine("Appuyer sur une touche pour quitter.");
  10. Console.ReadLine();
  11.  
  12. /* This code will produce output similar to the following:
  13. * Item2
  14. * Appuyer sur une touche pour quitter. */
  15. }
  16. }
  17.  
  18. internal static class NonConcurrentAlgorithm
  19. {
  20. [ThreadStatic]
  21. private static List _items=null;
  22. private static List Items
  23. {
  24. get
  25. {
  26. if (_items == null) _items = new List();
  27. return _items;
  28. }
  29. }
  30. public static void Add(String item)
  31. {
  32. Items.Add(item);
  33. }
  34. public static List GetItems()
  35. {
  36. return Items;
  37. }
  38. }



Notes :
Use with caution, this attribute is useful only if you control the lifecyle of your threads  (see the Scott Hanselman’ article [3]).




References :

[1] : Using AppDomains to make Non-Threadsafe code Threadsafe

[2] : AppDomains (“application domains”)

[3] : A tale of two techniques: The [ThreadStatic] Attribute and System.Web.HttpContext.Current.Items


Oct 28

This terms are often use in TDD, but many people mix up this terms.  In his article (based upon Gerard Meszaros’s  book [3]) Martin Fowler [1] explains the main difference between them.

  • Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
  • Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).
  • Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it ’sent’, or maybe only how many messages it ’sent’.
  • Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

Jeremy D. Miller [4]  capsulated this idea  by “state_based testing vs interaction testing” :

“The real difference between a mock and a stub is in the style of unit testing, i.e. state-based testing versus interaction testing.  A stub is a class that is hard-coded to return data from its methods and properties.  You use stubs inside unit tests when you’re testing that a class or method derives the expected output for a known input.

A mock object is a tool for interaction based testing.  You use a mock object to record and verify the interaction between two classes.  Roy Osherove made my all time favorite explanation of mock objects by comparing a mock object to a spy that listens to the interaction between two classes.”

Roy Osherove [2] provided a good scheme of the difference in flow information (see below)

Difference in flow information

Framework .NET for Mocking, Stubing & Faking :

References :

[1] : Martin Fowler, Mocks Aren’t Stubs

[2] : Roy Osherove, Mocks and Stubs – The difference is in the flow of information

[3]  : Gerard Meszaros, XUnit Test Patterns

[4] : Jeremy D. Miller, Mock Objects and Stubs: The Bottle Brush of TDD


Oct 27
Sur son blog Jason Zanders [1] présente une fonctionnalité très intéressante dans la validation des couches d’architecture. Cette validation permet de mettre en évidence des problèmes de dépendances invalides entre les couches d’une applications.



Références :
[1] : Article de Jason Zanders