TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Does the use of Tuples in C# lead to poor code maintainability?

3 点作者 chwolfe将近 10 年前
As much as I love Tuples for rapid prototyping, I am definitely concerned about the long term readability and maintainability vs. using traditional objects in production software. For instance, it can be difficult to determine what a List<Tuple<string, string, string>> collection actually represents compared to a List<ProductInfo> where ProductInfo is an object with 3 string properties with useful names (ItemNumber, ProductName, Price). Thoughts?

4 条评论

nlawalker将近 10 年前
After working for a while in Python I found myself doing the same thing, but the first-class handling for tuples in Python makes all the difference.<p>In C#, if the lifetime of your throwaway data structure is very short and tightly scoped (i.e. you only use it inside of one method), you could use an anonymous type. If it&#x27;s not, I usually find that it&#x27;s worth it to define a class. Since you&#x27;re in prototype mode, don&#x27;t get caught up early on in concerns about good object-oriented design - often I&#x27;ll just plunk out a class with public fields for use as a data structure and move on.<p>Also, if immutability is the thing that&#x27;s important to you, often in the prototype phase (especially when you&#x27;re the only one working on the code) it&#x27;s good enough to simply keep in mind that you shouldn&#x27;t be mutating state. You can always come back later and define full constructors, put &quot;readonly&quot; on members, and upgrade fields to Properties and fix the rest of the code with refactoring tools or a quick find&#x2F;replace.
评论 #10017903 未加载
WorldMaker将近 10 年前
In my opinion, as such things typically go, it depends on the scope of your tuple usage: I think tuples are perfectly readable&#x2F;maintainable when used within the context of a single class. It&#x27;s that middle point between usage within a single method (anonymous types) and usage in the external API (model classes).<p>I try not to leak Tuples between classes: it&#x27;s easy enough in the API for your class to &quot;unsplat&quot; your tuple usage in any method calls other classes might need.<p>Also, Tuples are likely to become even easier to read&#x2F;maintain in C#7 or so, as F#-like syntax for Tuples is a strongly favored proposal: <a href="https:&#x2F;&#x2F;github.com&#x2F;dotnet&#x2F;roslyn&#x2F;issues&#x2F;347" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dotnet&#x2F;roslyn&#x2F;issues&#x2F;347</a>
partisan将近 10 年前
I avoid the use of tuples outside of the scope of a function for the reasons you described. I prefer to define a type, even if that type is private to the containing class. Within a function, I prefer to use anonymous classes because of the improved code readability.
me-so-stupid将近 10 年前
I love to use tuples for rapid prototyping too. But when I need the same tuple again on some another place I do refactor my code to use Class instead of tuple.