使用语法糖/内置功能
我正忙于深入研究多线程和死锁等内容。这本书针对的是伪代码和 C 代码,我正忙于研究互斥锁和监视器等内容的实现。
这让我想起以下几点;在 C# 中,事实上在 .NET 中,我们有很多用于做事的语法糖。 例如 (.NET 3.5):
lock(obj)
{
body
}
等同于:
var temp = obj;
Monitor.Enter(temp);
try
{
body
}
finally
{
Monitor.Exit(temp);
}
当然还有其他示例,例如 using() {}
构造等。我的问题是什么时候更适用于“单独行动” ”并自己逐字编写代码,而不是使用语言中的“语法糖”?人们是否应该使用自己的方式,而不是使用对您所用的编码语言更有经验的人的方式?
我记得以前不必在 using
块中使用 Process
对象来帮助解决一些多线程问题和无限循环。我仍然因为那里没有 using 结构而感到肮脏。
谢谢,
凯尔
I was busy looking deeper into things like multi-threading and deadlocking etc. The book is aimed at both pseudo-code and C code and I was busy looking at implementations for things such as Mutex locks and Monitors.
This brought to mind the following; in C# and in fact .NET we have a lot of syntactic sugar for doing things. For instance (.NET 3.5):
lock(obj)
{
body
}
Is identical to:
var temp = obj;
Monitor.Enter(temp);
try
{
body
}
finally
{
Monitor.Exit(temp);
}
There are other examples of course, such as the using() {}
construct etc. My question is when is it more applicable to "go it alone" and literally code things oneself than to use the "syntactic sugar" in the language? Should one ever use their own ways rather than those of people who are more experienced in the language you're coding in?
I recall having to not use a Process
object in a using
block to help with some multi-threaded issues and infinite looping before. I still feel dirty for not having the using construct in there.
Thanks,
Kyle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽可能坚持语法糖。它简洁、更易于维护、不易出错、易于理解,并且他们创建它是有原因的。
如果您必须手动控制某些内容(例如操作
IEnumerator
而不是使用foreach
),那么可以,放弃语法糖。否则的话,能做到地道就是好事。Stick to the syntactic sugar as much as possible. It's concise, more maintainable, less error-prone, well understood, and they created it for a reason.
If you must have manual control over something (e.g. manipulating an
IEnumerator<T>
instead of usingforeach
), then yes, ditch the syntactic sugar. Otherwise, being idiomatic is a good thing.软件开发的最大成本是长期维护,因此答案始终是,做那些能为您提供最简单且最具成本效益的维护路径的事情(除了所有可能证明规则的例外情况,例如性能)。如果你可以使用语法糖来使你的代码更具可读性,那么这就是你的答案,如果语法糖妨碍了,那就不要使用它。
The biggest cost of software development is maintenance over the long term, so the answer is always, do the thing that will give you the easiest and most cost effective maintenance path (with all the exceptions that might prove the rule, perf for example). If you can use syntactical sugar to make your code more readable then that's your answer if the syntactical sugar gets in the way then don't use it.
在 C# 中,这个 linq 语句:
是语法糖(并且等效于):
如果您很了解 C#,则后一个版本比前一个版本更容易区分;你可以清楚地看到它在幕后正在做什么。
然而,对于典型的日常使用,大多数人发现加糖版本看起来更干净,并且更容易阅读。
In C#, this linq statement:
is syntactic sugar for (and equivalent to):
If you know C# well, the latter version is far easier to pick apart than the former; you can see exactly what it is doing under the hood.
However, for typical everyday use, most people find the sugared version cleaner to look at, and easier to read.
您无法使用
using
构造的示例是我对 .Net 语言和框架中提供的新方法最常见的偏差。在很多情况下,IDisposable 对象的范围有点超出单个函数的范围。然而,了解这些快捷方式的作用仍然和以往一样重要。我确实认为,如果许多人无法将对象包装在
using
中,他们就不会处置该对象,因为他们不理解它的作用以及它使什么变得更容易。所以我确实希望有一些类似工具提示帮助文本的东西,用于这些美妙的快捷方式,表明一些重要的事情正在发生 - 甚至可能是不同的关键字颜色。
编辑:
我一直在考虑这个问题,并且我决定我相信
using
只是选择的一个误导性关键字。foreach
的作用正如它听起来的那样,而using
对我来说并不意味着实际发生了什么。有人对此有什么想法吗?如果他们的关键字是dispose
会怎样?你认为这样会更清楚吗?Your example of not being able to use a
using
construct is my most common deviation from the new approaches made available in .Net languages and the framework. There are just a lot of cases where the scope of an IDisposable object is a bit outside of a single function.However, knowing about what these shortcuts do is still as important as ever. I do think many people simply won't dispose an object if they can't wrap it in a
using
, because they don't understand what it does and what it's making easier.So I do wish there was something like a tooltip helptext for some of these wonderful shortcuts, that indicated something important is happening - maybe even a different keyword coloring.
Edit:
I've been thinking about this, and I've decided that I believe
using
is just a misleading keyword to have chosen.foreach
does exactly what it sounds like, whereasusing
doesn't imply, to me, what's actually going on. Anybody have any thoughts on this? What if they keyword had beendisposing
instead; do you think it'd be any clearer?