INotifyPropertyChanged 设置器样式
为了将数据的更改反映到 UI,您必须实现 INotifyPropertyChanged,好吧。如果我在大多数情况下查看示例、文章、教程等,那么设置器看起来就像这样:
public string MyProperty
{
//get [...]
set
{
if (_correspondingField == value)
{
return;
}
_correspondingField = value;
OnPropertyChanged("MyProperty");
}
}
到目前为止没有问题,只有在必要时才引发事件,酷。但是你可以将这段代码重写为:
public string MyProperty
{
//get [...]
set
{
if (_correspondingField != value)
{
_correspondingField = value;
OnPropertyChanged("MyProperty");
}
}
}
它应该做同样的事情(?),你只有一个返回位置,它是更少的代码,它是不那么无聊的代码,它更切中要点(“仅在必要时才采取行动” vs “如果没有必要什么也不做,反之亦然”)。
那么,如果第二个版本比第一个版本有它的优点,为什么我很少看到这种风格呢?我不认为自己比那些编写框架、发表文章等的人聪明,因此第二个版本必须有缺点。还是说错了?还是我想太多了?
提前致谢
In order to reflect changes in your data to the UI you have to implement INotifyPropertyChanged, okay. If I look at examples, articles, tutorials etc most of the time the setters look like something in that manner:
public string MyProperty
{
//get [...]
set
{
if (_correspondingField == value)
{
return;
}
_correspondingField = value;
OnPropertyChanged("MyProperty");
}
}
No problem so far, only raise the event if you have to, cool. But you could rewrite this code to this:
public string MyProperty
{
//get [...]
set
{
if (_correspondingField != value)
{
_correspondingField = value;
OnPropertyChanged("MyProperty");
}
}
}
It should do the same (?), you only have one place of return, it is less code, it is less boring code and it is more to the point ("only act if necessary" vs "if not necessary do nothing, the other way round act").
So if the second version has its pros compared to the first one, why I see this style rarely? I don't consider myself being smarter than those people that write frameworks, published articles etc, therefore the second version has to have drawbacks. Or is it wrong? Or do I think too much?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我个人认为第二个例子更具可读性。我认为第一个示例变得普遍的原因之一是 Resharper 会提示使用这种样式(并自动进行转换),其理由是它“减少嵌套”。这是正确的,但在像这样的简单情况下,我认为可读性更重要。
这归结为一个根本性的意见分歧——有些程序员认为应该只存在一个“返回”——方法末尾有一个退出点。还有一些人认为,如果可能的话,应该始终有一个“提前退出”,这可能会导致整个方法中出现多次“返回”。你更喜欢哪一个? :)
I find the second example more readable personally. I think one of the reasons the first example has become widespread is that Resharper will prompt to use this style (and automatically do the conversion), with the rationale that it "reduces nesting". Which is correct, but in simple cases like these I think readability is more important.
It comes down to a fundamental difference of opinion - there are those programmers out there who think that there should only ever be one "return" - one single exit point at the end of the method. Then there are those who think that there should always be an "early exit" if at all possible, which can lead to multiple "returns" throughout the method. Which do you prefer? :)
我比第一种更频繁地看到第二种风格......但无论如何,这并不重要,行为是完全相同的。不,我认为第二种方法没有任何缺点。这只是个人喜好问题,选择您认为最可读的。
I see the second style much more often than the first... but anyway it doesn't matter, the behavior is exactly the same. And no, I don't think there is any drawback with the second approach. It's just a matter of personal preference, choose the one you find most readable.
与托马斯·莱维斯克相同。我在代码中使用了第二个,而且我经常看到它。两种方法应该执行相同的操作。
Same as Thomas Levesque. I use myself the second one in my code, and I have seen it quite often. Both approach should perform the same.
它减少了嵌套。
在您的示例中,它非常清晰,这只是一个品味问题,但是当以连续方式使用时会更加清晰,正如您在此处看到的:
反转“if”语句以减少嵌套
通常我不关心当值相同时是否收到事件,所以我把这部分省略了:
It reduces nesting.
In your example, its pretty clear, and its just a matter of taste, but it is much clearer when used in consecutive manner, as you can see here:
Invert "if" statement to reduce nesting
Usually I don't care if I get an event when the value is the same, so I leave that part out: