Are there any real circumstances in which foo = foo makes sense?

Clearing a few warnings in a C# project that I inherited found this piece of code:

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

I obviously burned the else block without thinking too much, just wondering why the previous programmer left this part.

  • Was it too lazy to delete it?
  • Has some future programmers considered maintaining some typing in the event of specific changes?
  • Does this hide something dangerous?

In your opinion, are there any real circumstances when foo=foo makes sense?


Additional information about the _WriteValue method:

The _WriteValue method _WriteValue enclosed in various overloaded WriteValue methods, which are passed to the object FieldValue WriteValue parameter, the values ​​of the following types: int , long , string and Datetime .

+4
source share
7 answers

If FieldValue is a property, the set operator can run some code, so self-awareness may make sense in this case ?!

Example:

 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 information that FieldValue is actually a parameter of the method, and not as a property, as I first thought)

+5
source

There are bad programmers, and they usually leave trash behind ...

+2
source

If a getter or setter appears behind FieldValue , then it can have side effects. For instance:

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

It is very bad practice to have a getter with side effects. However, it often happens that setters have wide side effects, although less common for these side effects are as severe as in my example!

+1
source

The programmer probably did not know about the existence of conditional breakpoints and used this operator as a place to place the breakpoint called by the conditional.

Just to figure it out, I'm not saying this is a good idea, but it is a trick in environments without conditional breakpoints.

+1
source

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

0
source

There are some situations with a low level of hardware in which the parameter value has (preferably) side effects that cannot be used in other ways. This is stupid, but beyond the ability of programmers to fix. I only saw this situation in the open source C code, so I'm sure not because you see this in C # code, but it happens.

0
source

Please note that this is a terrific (and too general) example of what you should comment on: everything where the β€œobvious” β€œfix” actually breaks everything. Make sure you learn from your frustrations!

0
source

Source: https://habr.com/ru/post/1342103/


All Articles