是否存在 foo=foo 有意义的有效情况?

发布于 2024-10-19 23:41:17 字数 685 浏览 5 评论 0原文

清理我继承的 C# 项目上的一些警告,我发现了这个代码片段:

private bool _WriteValue(object FieldValue,..,..)
  ...
  if(MultipFactor!=1)
     FieldValue=((double)FieldValue)*MultipFactor;
  else
    FieldValue=FieldValue;

我显然已经烧掉了 else 块,没有考虑太多,只是想知道为什么以前的程序员已经离开了那部分。

  • 是不是自己懒得删了?
  • 对于一些未来的程序员来说,在发生特定更改时节省一些打字是一种礼貌吗?
  • 是不是隐藏了什么危险的东西?

您认为,是否存在 foo=foo 有意义的有效情况?


有关 _WriteValue 方法的更多详细信息:

_WriteValue 方法被包装到传递给 对象 FieldValue 的不同重载 WriteValue 方法中code> 参数,以下类型的值:intlongstringDatetime

Cleaning a handful of warnings on a C# project I have inherited, I found this code snippet:

private bool _WriteValue(object FieldValue,..,..)
  ...
  if(MultipFactor!=1)
     FieldValue=((double)FieldValue)*MultipFactor;
  else
    FieldValue=FieldValue;

I've obviously burninated the else block without thinking too much, just wondering why the previous programmer has left that part.

  • Was it just too lazy to delete it?
  • Was it a courtesy for some future programmers to save some typing in case of specific changes?
  • Is it hiding something dangerous?

In your opinion, are there any valid circumstances where foo=foo makes sense?


Some more details on the _WriteValue method:

The _WriteValue method is wrapped into different overloaded WriteValue methods that pass to the object FieldValue parameter, values of the following types: int, long, string and Datetime.

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

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

发布评论

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

评论(7

一百个冬季 2024-10-26 23:41:17

如果 FieldValue 是一个属性,则 set 运算符可能会触发一些代码,因此在这种情况下自分配可能有意义?!

一个例子是:

public string FieldValue
{
    get
    {
        return _fieldValue;
    }
    set
    {
        _fieldValue = value;
        Trace.WriteLine( string.Format("Received value '{0}'.", value ) );
    }
}

(我的答案是在发布者添加 FieldValue 实际上是一个方法参数,而不是我首先假设的属性的信息之前给出的)

If FieldValue is a property, the set operator could trigger some code, so a self-assignment could make sense in such a case?!

An example would be:

public string FieldValue
{
    get
    {
        return _fieldValue;
    }
    set
    {
        _fieldValue = value;
        Trace.WriteLine( string.Format("Received value '{0}'.", value ) );
    }
}

(My answer was given before the poster added the information that FieldValue actually is a method parameter, not a property as I assumed first)

清秋悲枫 2024-10-26 23:41:17

有一些糟糕的程序员,他们通常会留下一些垃圾......

There are some bad programmers, and they usually leave some garbage behind...

蓝海似她心 2024-10-26 23:41:17

程序员可能不知道条件断点的存在,并利用该语句作为放置有条件触发的断点的位置。

澄清一下,我并不是说这是一个好主意,而是在没有条件断点的环境中这是一个技巧。

The programmer was probably unaware of the existence of conditional breakpoints and leveraged that statement as a location to place a breakpoint that triggered conditionally.

Just to make it clear, I'm not saying that this is a good idea, but it's a trick in environments without conditional breakpoints.

花开浅夏 2024-10-26 23:41:17

如果 FieldValue 后面有一个 getter 或 setter,那么它可能会产生副作用。例如:

private double myFieldValue;

public double FieldValue
{
    get { return myFieldValue; }
    set { myFieldValue = value; ReformatSystemVolume(); }
}

使用带有副作用的吸气剂是非常糟糕的做法。然而,设置器产生广泛的副作用是很常见的,尽管这些副作用不像我的例子中那么严重!

If there was a getter or a setter behind FieldValue then it could have side effects. For example:

private double myFieldValue;

public double FieldValue
{
    get { return myFieldValue; }
    set { myFieldValue = value; ReformatSystemVolume(); }
}

It's exceedingly bad practice to have a getter with side-effects. However, it is very common to have setters wide side-effects, although less common for those side-effects to be as drastic as in my example!

圈圈圆圆圈圈 2024-10-26 23:41:17

在 C++ 中,您可以定义 operator= 来执行您想要的任何操作:)

In C++ you can define operator= to do anything you want :)

傲娇萝莉攻 2024-10-26 23:41:17

在某些低级硬件情况下,设置该值会产生(所需的)副作用,而这些副作用无法通过其他方式调用。这很愚蠢,但超出了程序员修复的范围。我只在纯 C 代码中见过这种情况,所以我确信这不是您在 C# 代码中看到这种情况的原因,但它确实发生了。

There are some low-level hardware situations where setting the value has (desirable) side effects that can't be invoked other ways. It's stupid, but beyond the scope of programmers to fix. I've only seen this situation in bare C code, so I'm sure that's not why you're seeing this in C# code, but it does happen.

风尘浪孓 2024-10-26 23:41:17

请注意,这是一个很棒的(而且非常常见的)示例,您应该对此进行评论:任何“明显”的“修复”实际上会破坏事物的地方。确保你从挫折中吸取教训!

Please note that this is a terrific (and all-too-common) example of things you should comment: anything where the "obvious" "fix" will actually break things. Make sure you learn from your frustrations!

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