我应该完全申报丢弃物品吗?

发布于 2024-09-12 02:17:25 字数 450 浏览 2 评论 0原文

如果我需要做这样的事情:

var connection = new Connection(host);
connection.Execute(Commands.Delete);

这样做有什么问题吗:

(new Connection(host)).Execute(Commands.Delete);

第一个示例可能更具可读性,但如果我需要多次执行此操作,第二个示例效果更好:

(new Connection(anotherHost)).Execute(Commands.Create);
(new Connection(someOtherHost)).Execute(Commands.Update);
(new Connection(host)).Execute(Commands.Delete);

If I need to do something like this:

var connection = new Connection(host);
connection.Execute(Commands.Delete);

Is there anything wrong in doing this:

(new Connection(host)).Execute(Commands.Delete);

The first example may be more readable, but the second works better if I need to do this multiple times:

(new Connection(anotherHost)).Execute(Commands.Create);
(new Connection(someOtherHost)).Execute(Commands.Update);
(new Connection(host)).Execute(Commands.Delete);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感受沵的脚步 2024-09-19 02:17:25

您的 Connection 类是否实现了 IDisposable ?然后:

using (var connection = new Connection(host))
{
    connection.Execute(Commands.Delete);
}

Does your Connection class implement IDisposable? Then:

using (var connection = new Connection(host))
{
    connection.Execute(Commands.Delete);
}
倾`听者〃 2024-09-19 02:17:25

首先我是一个Java人,我没有使用过C#
但是根据您的代码并了解与 java 的相似性,我可以说的是 -

如果您的 Connection 类维护状态信息,那么每次创建新对象都是有意义的。但如果它是无状态的,那么创建多个对象的效率相当低。您可以创建一个并重复使用它。

即,如果您无法将“主机”设置为创建后的连接,那么您提到的两种方法应该没有任何区别。

First thing I am a Java person and I havent used C#
But based on your code and knowing similarity with java what I can say is -

If your Connection class maintains a state information then its meaningful to create new object every time. But if it is stateless then its pretty inefficient to create multiple objects. You can create one and re-use the same.

i.e. If you cannot set the 'host' to a connection once created, then both approaches you mentioned should not make any difference.

随波逐流 2024-09-19 02:17:25

越详细,调试就越容易。

对代码可读性的影响实际上取决于您尝试在一行中包含多少内容 - 如果您有一个想法,只需要很多不同的单词来表达,那么将其放在一行中并不是什么大问题我认为交易。但如果你试图将多种想法塞进一行,你就会失去清晰度。

例如。我们将从一个简单的想法开始,只需要一些空间来表达:

transactionTime = generationTime + retrievalTime + processingTime + loggingTime

这里我们有一个更复杂的想法,我们碰巧用一行表达:

logTransactionData(processTransaction(retrieveTransaction(generateTransactionQuery())))

我认为第一个例子比第二个例子更容易理解,即使它们的字符长度大致相同。

因此,总的来说:考虑您需要调试线路的可能性有多大,以及您的想法复杂性与线路的比率。

The more verbose you are, the easier of a time you will have debugging.

The effect on your code readability really depends on how much you're trying to wrap into one line - if you've got a single idea that just takes a lot of different words to express, putting it in one line isn't a big deal in my opinion. But if you're trying to cram multiple ideas into one line you'll lose clarity.

For instance. We'll start with a simple idea that just takes some space to express:

transactionTime = generationTime + retrievalTime + processingTime + loggingTime

And here we have a more complex idea that we happen to express in one line:

logTransactionData(processTransaction(retrieveTransaction(generateTransactionQuery())))

I think that the first example is easier to understand at a glance than the second, even though they're about the same in character length.

So in general: consider how likely it is you'll need to debug the line, as well as your idea-complexity-to-line ratio.

2024-09-19 02:17:25

是的,您正在初始化一个新对象,使用它,然后丢失它!你不能再次重用它,它会在某个地方,直到 GC 收集它!因此,将新的初始化对象存储在变量中并没有什么坏处,并且在另一行中声明它们可以使代码更具可读性。

Yes, you're initializing a new object, using it, and loosing it! you cannot reuse it again and it's gonna be there somewhere, until GC collects it! So, there's no harm in storing new initialized objects in a variable, and declaring them in another line makes your code more readable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文