Search code examples
c#refactoringlegacy

Are there any valid circumstances where foo=foo makes sense?


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.


Solution

  • 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)