I have a class Vector2 (which contains a double “x”, a double “y” and some methods), which is widely used by my game. At the moment some methods that use it accept/return a clone, and some a reference. This is confusing and inconsistent, so I want to change it. Now, however I don’t know which system to choose:
- Make the class immutable and always pass a reference
++ Good for memory
– Many new allocations if a vector needs to be changed every frame (because it can’t be modified)
OR
- Keep the class mutable, but always make a clone when accepting/returning an instance of it
++ It can be modified directly (for example, the position of an object which changes every frame
– Every copy takes memory, and clones need to be made every time it’s passed to another method
So, what do you think is the fastest/best way?