2

(In the following code, player is a of a type which contains a Vector2 called Vector)

Vector2 v = player.Vector;
v.X -= player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
player.Vector = v;

vs.

player.Vector = new Vector2(player.Vector.X - player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds, player.Vector.Y);

These both accomplish the same task (Getting around the "Cannot modify the return value because it is not a variable" Error), but is one more efficient than the other?

Does one use less memory? (No, right?) Does one execute quicker? Is there a better way?

Evorlor
  • 7,263
  • 17
  • 70
  • 141
  • Lol, you're just really racking off the XNA questions! :) – davidsbro Feb 05 '14 at 01:37
  • haha one question keeps leading to another. ur previous answer got me thinking :) – Evorlor Feb 05 '14 at 01:38
  • Good! Glad it did. (+1 for both the question and answer btw, I have actually always wondered this myself...I just always assumed the second was better) – davidsbro Feb 05 '14 at 01:40
  • I think it is really hard to say exact answer from performance point view they both quite the same (may be different in one or two or three .. but not too much, register or argument or variable stack copy) and it might be dependent on the compiler and low level stuff, in this kind of situation I prefer more readable code and go for the second one by the way in http://stackoverflow.com/questions/3729873/problem-with-struct-and-property-in-c-sharp the second answer shows why you get the error "Cannot modify... " based on that Vector2 is a value type you can try to estimate the answer – Mojtaba Feb 05 '14 at 01:59
  • everyone keeps talking about mutable structs, but keep in mind, i am not using a struct – Evorlor Feb 05 '14 at 02:00
  • 1
    @Evorlor you got this error because Vector2 is a struct so you are using struct look at here http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.vector2.aspx – Mojtaba Feb 05 '14 at 02:01
  • o excellent ty mojtaba. didnt know :) i always thought of it as a class (player) – Evorlor Feb 05 '14 at 02:03
  • 1
    @Evorlor look at the second answer of http://stackoverflow.com/questions/3729873/problem-with-struct-and-property-in-c-sharp this post it is good to know that why you get the error of "Cannot modify the return value because it is not a variable" when you are working with struct (in case you do not know) – Mojtaba Feb 05 '14 at 02:05
  • 1
    Yes, i read that one about 3-4 times. Jon Skeet rules :) – Evorlor Feb 05 '14 at 02:08
  • 1
    when I looked at your question I tried to remember the reason for the error I could not then I searched the error read that too :) – Mojtaba Feb 05 '14 at 02:14

1 Answers1

2

The second option is better because it doesn't mutate any objects. To improve readability I'd extract a variable like this:

var x = player.Vector.X - player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
player.Vector = new Vector2(x, player.Vector.Y);
Konstantin Spirin
  • 20,609
  • 15
  • 72
  • 90
  • In your first code you do `v.X = ...` which means that you are mutating your vector. Using a variable `x` allows you to construct a new vector with the values you need. – Konstantin Spirin Feb 05 '14 at 01:36
  • I am not sure I understand. What would be a good phrase to google? :) – Evorlor Feb 05 '14 at 01:38
  • 2
    @Evorlor, some people think mutating can be dangerous, like is suggested here: http://blogs.msdn.com/b/ericlippert/archive/2011/03/14/to-box-or-not-to-box-that-is-the-question.aspx?PageIndex=4 – davidsbro Feb 05 '14 at 01:56