itpresidents / thesis-archive-2020

10 stars 1 forks source link

Make Vector immutable #51

Closed oveddan closed 4 years ago

oveddan commented 4 years ago

Currently - a lot of the operations on the Vector change the vectors values, like, add for example:

  scale(ratio: number): number[] {
    if (typeof ratio === "number")
      for (let i in this) {
        this[i] *= ratio;
      }
    return this.toArray();
  }

It is better to not modify the vector itself, but to just return a new one.

Doing something like:

const a = new Vector(2, 2);
const b = a.scale(10);

should not modify a - because even though a will have a different value inside, it will be considered the same as before.

Generally in react land you don't want to mutate the variable, but just return a new one.

EonYang commented 4 years ago

When I was writing the class, it was a hard decision to make. Personally I like OOP and prefer modifying values in place.

Generally in react land you don't want to mutate the variable, but just return a new one.

However, as you said, this is a React convention, I will follow it~