i have to write a simple vector/matrix library for a small geometry related project i’m working on. here’s what i’m wondering.
when doing mathematical operations on vectors in a java environment, is it better practice to return a new instance of a vector or modify the state of the original.
i’ve seen it back and forth and would just like to get a majority input.
certain people say that the vectors should be immutable and static methods should be used to create new ones, others say that they should be mutable and normal methods should be used to modify their state. i’ve seen it in some cases where the object is immutable and normal methods are called which returns a new vector from the object without changing the state – this seems a little off to me.
i would just like to get a feel for if there is any best practice for this – i imagine it’s something that’s been done a million times and am really just wondering if there’s a standard way to do this.
i noticed the apache commons math library returns a new vector every time from the original.
How important is performance going to be? Is vector arithmetic going to be a large component so that it affects the performance of overall system?
If it is not and there is going to be lot of concurrency then immutable vectors will be useful because they reduce concurrency issues.
If there are lot of mutations on vectors then the overhead of new objects that immutable vectors will require will become significant and it may be better to have mutable vectors and do the concurrency the hard way.