Is Using .NET 4.0 Tuples in my C# Code a Poor Design Decision?

前端 未结 13 1851
我在风中等你
我在风中等你 2020-12-02 04:55

With the addition of the Tuple class in .net 4, I have been trying to decide if using them in my design is a bad choice or not. The way I see it, a Tuple can be a shortcut

相关标签:
13条回答
  • 2020-12-02 05:24

    Tuples are pretty underwhelming addition to the CLR from the perspective of a C# programmer. If you have a collection of items that varies in length, you don't need them to have unique static names at compile time.

    But if you have a collection of constant length, this implies that the fixed of locations in the collection each have a specific pre-defined meaning. And it is always better to give them appropriate static names in that case, rather than having to remember the significance of Item1, Item2, etc.

    Anonymous classes in C# already provide a superb solution to the most common private use of tuples, and they give meaningful names to the items, so they are actually superior in that sense. The only problem is that they can't leak out of named methods. I'd prefer to see that restriction lifted (perhaps only for private methods) than have specific support for tuples in C#:

    private var GetDesserts()
    {
        return _icecreams.Select(
            i => new { icecream = i, topping = new Topping(i) }
        );
    }
    
    public void Eat()
    {
        foreach (var dessert in GetDesserts())
        {
            dessert.icecream.AddTopping(dessert.topping);
            dessert.Eat();
        }
    }
    
    0 讨论(0)
  • 2020-12-02 05:24

    It depends, of course! As you said, a tuple can save you code and time when you want to group some items together for local consumption. You can also use them to create more generic processing algorithms than you can if you pass a concrete class around. I can't remember how many times I've wished I had something beyond KeyValuePair or a DataRow to quickly pass some date from one method to another.

    On the other hand, it is quite possible to overdo it and pass around tuples where you can only guess what they contain. If you are going to use a tuple across classes, perhaps it would be better to create one concrete class.

    Used in moderation of course, tuples can lead to more concise and readable code. You can look to C++, STL and Boost for examples of how Tuples are used in other languages but in the end, we will all have to experiment to find how they best fit in the .NET environment.

    0 讨论(0)
  • 2020-12-02 05:26

    Similar to keyword var, it is intended as a convenience - but is as easily abused.

    In my most humble opinion, do not expose Tuple as a return class. Use it privately, if a service or component's data structure requires it, but return well-formed well-known classes from public methods.

    // one possible use of tuple within a private context. would never
    // return an opaque non-descript instance as a result, but useful
    // when scope is known [ie private] and implementation intimacy is
    // expected
    public class WorkflowHost
    {
        // a map of uri's to a workflow service definition 
        // and workflow service instance. By convention, first
        // element of tuple is definition, second element is
        // instance
        private Dictionary<Uri, Tuple<WorkflowService, WorkflowServiceHost>> _map = 
            new Dictionary<Uri, Tuple<WorkflowService, WorkflowServiceHost>> ();
    }
    
    0 讨论(0)
  • 2020-12-02 05:28

    Tuples are great if you control both creating and using them - you can maintain context, which is essential to understanding them.

    On a public API, however, they are less effective. The consumer (not you) has to either guess or look up documentation, especially for things like Tuple<int, int>.

    I would use them for private/internal members, but use result classes for public/protected members.

    This answer also has some info.

    0 讨论(0)
  • 2020-12-02 05:30

    I would personally never use a Tuple as a return type because there is no indication of what the values represent. Tuples have some valuable uses because unlike objects they are value types and thus understand equality. Because of this I will use them as dictionary keys if I need a multipart key or as a key for a GroupBy clause if I want to group by multiple variables and don't want nested groupings (Who ever wants nested groupings?). To overcome the issue with extreme verbosity you can create them with a helper method. Keep in mind if you are frequently accessing members (through Item1, Item2, etc) then you should probably use a different construct such as a struct or an anonymous class.

    0 讨论(0)
  • 2020-12-02 05:33

    Don't judge me, I'm not an expert, but with new Tuples in C# 7.x now, you could return something like:

    return (string Name1, int Name2)
    

    At least now you can name it and developers might see some information.

    0 讨论(0)
提交回复
热议问题