Skip to content

Mutable structs #3

@CodesInChaos

Description

@CodesInChaos

We all know that mutable structs are evil. But of course the performance demands of numerical/game code sometimes top good design. Still I think that you use too much mutability here.

I see several types of struct mutability in general

  1. Public mutable fields
  2. Properties with public setters
  3. Modifying structs passed in via ref/out
  4. Methods mutating this

In my experience 4 is the one that creates the most subtle bugs. Mistakes with 1, 2 and 3 are usually prevented by the compiler. But the compiler doesn't help when you make a mistake with type 4 mutations.

The typical example is that you call such a mutating function on a temporary variable without noticing you did. For example readonly fields or properties are common traps.

I noticed that most of your code avoids type 4 mutations. But there are some methods that do use them. In particular Normalize and Conjugate.

I suggest replacing them with new methods(with a different name obviously) that return their result instead of modifying this.

If you want to keep these methods around for XNA compatibility I suggest marking them as obsolete and adding an attribute to hide them from intellisense.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions