Don't be lazy. Avoid the type "Tuple"

*Updated* 26 September 2010: Updated with comments from Adam Cogan
*Updated* 27 September 2010: Updated the comparison between anonymous types and Tuple's from blog comments

Tuple is a new type introduced in .NET4. My first impression of it was great, because it's a nice and easy way to group elements of a different type together. Sweet!

My first experience using the Tuple type was using it as a return value from a few methods, where I previously had an "out" parameter before, like this…

            string errormsg;
            User user;
            bool success = AuthenticationService.GetUser(usercode, out user, out errormsg);
Figure: Bad example - We should avoid "out" parameters, because it means we return 2 objects from a method.

A method should return only 1 thing, otherwise it is considered as a code smell. More on Stackoverflow here and here

Refactoring: Take #1.

I refactored the above code to this…

  
            Tuple<bool, User, string> result = AuthenticationService.GetUser(usercode);
            
            Console.WriteLine("Result {0}  with user {1}", result.Item1, result.Item2);
Figure: Better example - Here the Tuple type replaces the "out" parameters

 

image  But I have a problem with the readability of the code, specifically the stuff in yellow

  
            Tuple<bool, User, string> result = AuthenticationService.GetUser(usercode);
            
            Console.WriteLine("Result {0}  with user {1}", result.Item1, result.Item2);
Figure: What is Item1 and Item2?

 

Refactoring: Take #2.

Take 2 minutes (if you are using Resharper) and create a nice class that represents the above code better

  
    GetUserResult result = AuthenticationService.GetUser(usercode);
    if (result.Result == false)
    {
        Console.WriteLine("Result {0} Error {1} ", result.Result, result.ErrorMessage);
    }
            
    public class GetUserResult
    {
        public string ErrorMessage { get; set; }
        public bool Result { get; set; }
        public User User { get; set; }
    }
Figure: Good example - Item1 and Item2 are replaced with your own class


PS: Still not convinced? Search for "Tuple c# Item1 Item2 Item3" and you will see a lot of unreadable code samples using "Tuple"

 

PPS: Some have asked me: What is the difference between a "Tuple" and an "Anonymous Type"?

I use both, while keeping in my mind:

  1. Anonymous types cannot cross method boundaries  (Yes they can, if you declare the parameter as object, or by using Generics, THANKS Anonymous)
  2. Anonymous types cannot be used in the declaration of method parameters, obviously ;-)
  3. With Anonymous types you can name the properties, which is nice!
            var mySearchResult = new
                                     {
                                         Result = true,
                                         ErrorMessage = "None",
                                         User = new User("Peter", "Gfader")
                                     };
Figure: Good example - You can have nice names for properties when using anonymous type

 

Suggestion to Microsoft

I would like the C#/CLR team to give me the ability to give a Tuple nice property names, like this

 Tuple<bool Result, User UserFromWS, string ErrorMessage> result = AuthenticationService.GetUser(usercode);

Figure: Id like Microsoft to make this change in the C# language

Latest Posts

Popular Posts

 
Google Analytics Alternative