To return a value or not to return a value, that is the question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lukeedw
    New Member
    • Jul 2010
    • 1

    #1

    To return a value or not to return a value, that is the question

    I have a class Vector (representing a 3D Vector). I want to normalize that Vector (to give it a length of 1). From the three possibilities below (or feel free to suggest your own) what would you guys choose?

    1. Normalize function that modifies the object:
    Code:
    public void Normalize()
    2. Normalize function that returns a modified version of the object leaving the original object untouched.
    Code:
    public Vector Normalize()
    3. Do both, modify the original and return that instance.
    Code:
    public Vector Normalize()
    The question arises as I want to compare two vectors but I want to try and normalize the second vector within the equality operation:
    Code:
    v1 == v2.Normalize()
    I like option 1 as it is simple when just normalizing a vector but stumps me for the problem above.
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    If you're going to be doing things like temporarily normalizing the vector for comparison purposes, I would suggest going with the second option. It's pretty easy to say v2 = v2.Normalize(); if you want to make your changes stick. Strings work that way, so it's not all that unfamiliar.

    Comment

    Working...