0

I wonder if this ever was or still is a case in .Net.

Use exp += val instead of exp = exp + val. Since exp can be arbitrarily complex, this can result in lots of unnecessary work. This forces the JIT to evaluate both copies of exp, and many times this is not needed. The first statement can be optimized far better than the second, since the JIT can avoid evaluating the exp twice.

That's coming from an ancient article in codeproject.

And there's another one in cpp:

However, the compound-assignment expression is not equivalent to the expanded version because the compound-assignment expression evaluates expression1 only once, while the expanded version evaluates expression1 twice: in the addition operation and in the assignment operation.

Dai
  • 141,631
  • 28
  • 261
  • 374
Daniel B
  • 3,109
  • 2
  • 33
  • 42
  • I think it may have been in reference to using `+=` on an `int` property instead of a local or field. – Dai Aug 17 '20 at 20:59
  • Do you have a link to the codeproject article or a wayback-machine copy? I’d like to know who the author was because historically there were a lot of non-experts pontificating flat-out incorrect information on CodeProject. What does the C# specification say? – Dai Aug 17 '20 at 21:01
  • @Dai, I couldn't find in c# specification – Daniel B Aug 17 '20 at 21:14
  • 2
    I think this needs a language tag because `+=` is an operator in both C# and VB.NET. The [C# documentation](https://learn.microsoft.com/dotnet/csharp/language-reference/operators/addition-operator#addition-assignment-operator-) says "`x += y` is equivalent to `x = x + y` except that `x` is only evaluated once", but I don't see how assigning to `x` requires evaluating it (again). Perhaps it's only an optimization when doing something like `collection[index] += value` or `SomeObject.SomeMethod().SomeProperty += value`; otherwise, it's just shorthand. – Lance U. Matthews Aug 17 '20 at 21:28

1 Answers1

5

Yes it was, and still is, a best practice in .NET. Imaging you want to add 4 to

myObject.MyExpensiveMethod().MyProperty

Obviously you wouldn't want to do:

myObject.MyExpensiveMethod().MyProperty = 
    myObject.MyExpensiveMethod().MyProperty + 4;

since it calls the expensive method twice, whereas += only calls it once. You could do:

var temp = myObject.MyExpensiveMethod();
temp.MyProperty = temp.MyProperty + 4;

which is less expensive, but it's cheapest to do:

myObject.MyExpensiveMethod().MyProperty += 4;

Since the expensive method is only called once.

Another case is when you use a method that has side effects that you only want to call once:

myFactory.GetNextObject().MyProperty += 5;

You _certainly wouldn't do

myFactory.GetNextObject().MyProperty = myFactory.GetNextObject().MyProperty + 5;

You could again use a temp variable, but the compound assignment operator is obviously more succinct.

Granted, these are edge cases, but it's not a bad habit to get into.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • 2
    @DanielB Why is `myObject.MyExpensiveMethod().MyProperty` not an expression in your sense? – Klaus Gütter Aug 18 '20 at 05:35
  • @DanielB Is the article talking about a _literal_ `` of some sort? It would be trivial to create an expression for `myObject.MyExpensiveMethod().MyProperty`, but the results would be the same. – D Stanley Aug 18 '20 at 12:42
  • @DanielB If you're talking specifically about simple `int x = 123; x += value;` scenarios, then no such requirement was included in the question. What was included, however, is the quote "Since exp can be arbitrarily complex", and I think `myObject.MyExpensiveMethod().MyProperty` certainly qualifies as "arbitrarily complex". You do realize that in the first text you quoted "exp" is short for "expression" and not simply, say, a local variable that happens to be named `exp`? – Lance U. Matthews Aug 18 '20 at 18:13