Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualCornstalks

Posted 31 March 2013 - 04:22 PM

We had a thread about this once, and one thing that someone suggested (which I started doing) was using normalized to represent the non-mutating normalize function.
 
That is:
 
v.normalize(); // mutating function, modifies v
v.normalized(); // non-mutating function, does not modify v (returns a new vector)
 
Another suggestion someone brought up was to use free functions instead of member functions. So then I do something like:
 
// Personally, I hate overloading operators for cross and dot
// (if my keyboard doesn't have the mathematically right symbol, I write it out)
u = cross(a, b);
v = dot(a, b);
w = normalized(a);
 
In then end, I combine these two. Free functions are always non-mutating (and in ambiguous cases, the past-tense form of the word is used). Member functions that mutate do not use the past-tense. Member functions that obviously don't mutate are allowed (like magnitude()). Member functions that don't modify the object, but is not obvious, are not allowed (like a normalized() member function). Something like:
 
a = normalized(b); // b is not modified
b.normalize(); // b is modified (note: I don't define b.normalized() (i.e. a non-mutating member version of normalize))
c = cross(a, b); // a and b are not modified
c = dot(a, b); // a and b are not modified
c = -a; // a is not modified

On top of this, I make the b.normalize() function return void, so it's clear it's meant to be called for its side effects and can't be accidentally used in the same place that the free normalized() function can.

 

Edit: and for those curious, I'm intentionally not using code tags because they're too frustrating to work with.


#5Cornstalks

Posted 31 March 2013 - 04:21 PM

We had a thread about this once, and one thing that someone suggested (which I started doing) was using normalized to represent the non-mutating normalize function.
 
That is:
 
v.normalize(); // mutating function, modifies v
v.normalized(); // non-mutating function, does not modify v (returns a new vector)
 
Another suggestion someone brought up was to use free functions instead of member functions. So then I do something like:
 
// Personally, I hate overloading operators for cross and dot
// (if my keyboard doesn't have the mathematically right symbol, I write it out)
u = cross(a, b);
v = dot(a, b);
w = normalized(a);
 
In then end, I combine these two. Free functions are always non-mutating (and in ambiguous cases, the past-tense form of the word is used). Member functions that mutate do not use the past-tense. Member functions that obviously don't mutate are allowed (like magnitude()). Member functions that don't modify the object, but is not obvious, are not allowed (like a normalized() member function). Something like:
 
a = normalized(b); // b is not modified
b.normalize(); // b is modified (note: I don't define b.normalized() (i.e. a non-mutating member version of normalize))
c = cross(a, b); // a and b are not modified
c = dot(a, b); // a and b are not modified
c = -a; // a is not modified

On top of this, I make the b.normalize() function return void, so it's clear it's meant to be called for its side effects and can't be accidentally used in the same place that the free normalized() function can.

 

Edit: and for those curious, I'm intentionally not using code blocks because they're too frustrating to work with.


#4Cornstalks

Posted 31 March 2013 - 04:20 PM

We had a thread about this once, and one thing that someone suggested (which I started doing) was using normalized to represent the non-mutating normalize function.
 
That is:
 
v.normalize(); // mutating function, modifies v
v.normalized(); // non-mutating function, does not modify v (returns a new vector)
 
Another suggestion someone brought up was to use free functions instead of member functions. So then I do something like:
 
// Personally, I hate overloading operators for cross and dot
// (if my keyboard doesn't have the mathematically right symbol, I write it out)
u = cross(a, b);
v = dot(a, b);
w = normalized(a);
 
In then end, I combine these two. Free functions are always non-mutating (and in ambiguous cases, the past-tense form of the word is used). Member functions that mutate do not use the past-tense. Member functions that obviously don't mutate are allowed (like magnitude()). Member functions that don't modify the object, but is not obvious, are not allowed (like a normalized() member function). Something like:
 
a = normalized(b); // b is not modified
b.normalize(); // b is modified (note: I don't define b.normalized() (i.e. a non-mutating member version of normalize))
c = cross(a, b); // a and b are not modified
c = dot(a, b); // a and b are not modified
c = -a; // a is not modified

On top of this, I make the b.normalize() function return void, so it's clear it's meant to be called for its side effects and can't be accidentally used in the same place that the free normalized() function can.


#3Cornstalks

Posted 31 March 2013 - 04:18 PM

We had a thread about this once, and one thing that someone suggested (which I started doing) was using normalized to represent the non-mutating normalize function.
 
That is:
 
v.normalize(); // mutating function, modifies v
v.normalized(); // non-mutating function, does not modify v (returns a new vector)
 
Another suggestion someone brought up was to use free functions instead of member functions. So then I do something like:
 
// Personally, I hate overloading operators for cross and dot
// (if my keyboard doesn't have the mathematically right symbol, I write it out)
u = cross(a, b);
v = dot(a, b);
w = normalized(a);
 
In then end, combining these two. Free functions are always non-mutating (and in ambiguous cases, the past-tense form of the word is used). Member functions that mutate do not use the past-tense. Member functions that obviously don't mutate are allowed (like magnitude()). Member functions that don't modify the object, but is not obvious, are not allowed (like a normalized() member function). Something like:
 
a = normalized(b); // b is not modified
b.normalize(); // b is modified (note: I don't define b.normalized() (i.e. a non-mutating member version of normalize))
c = cross(a, b); // a and b are not modified
c = dot(a, b); // a and b are not modified
c = -a; // a is not modified

On top of this, I make the b.normalize() function return void, so it's clear it's meant to be called for its side effects and can't be accidentally used in the same place that the free normalized() function can.


#2Cornstalks

Posted 31 March 2013 - 04:17 PM

We had a thread about this once, and one thing that someone suggested (which I started doing) was using normalized to represent the non-mutating normalize function.

 

That is:

 

v.normalize(); // mutating function, modifies v

v.normalized(); // non-mutating function, does not modify v (returns a new vector)

 

Another suggestion someone brought up was to use free functions instead of member functions. So then I do something like:

 

// Personally, I hate overloading operators for cross and dot

// (if my keyboard doesn't have the mathematically right symbol, I write it out)

u = cross(a, b);

v = dot(a, b);

w = normalized(a);

 

In then end, combining these two. Free functions are always non-mutating (and in ambiguous cases, the past-tense form of the word is used). Member functions that mutate do not use the past-tense. Member functions that obviously don't mutate are allowed (like magnitude()). Member functions that don't modify the object, but is not obvious, are not allowed (like a normalized() member function). Something like:

 

a = normalized(b); // b is not modified

b.normalize(); // b is modified (note: I don't define b.normalized() (i.e. a non-mutating member version of normalize))

c = cross(a, b); // a and b are not modified

c = dot(a, b); // a and b are not modified

c = -a; // a is not modified


#1Cornstalks

Posted 31 March 2013 - 04:13 PM

We had a thread about this once, and one thing that someone suggested (which I started doing) was using normalized to represent the non-mutating normalize function.

 

That is:

 

v.normalize(); // mutating function, modifies v

v.normalized(); // non-mutating function, does not modify v (returns a new vector)

 

Another suggestion someone brought up was to use free functions instead of member functions. So then I do something like:

 

// Personally, I hate overloading operators for cross and dot

// (if my keyboard doesn't have the mathematically right symbol, I write it out)

u = cross(a, b);

v = dot(a, b);

w = normalized(a);

 

In then end, combining these two. Free functions are always non-mutating (and in ambiguous cases, the past-tense form of the word is used). Member functions that mutate do not use the past-tense. Member functions that obviously don't mutate are allowed (like magnitude()). Member functions that don't modify the object, but is not obvious, are not allowed (like a normalized() member function). Something like:

 

a = normlized(b); // b is not modified

b.normlize(); // b is modified (note: I don't define b.normalized() (i.e. a non-mutating member version of normalize))

c = cross(a, b); // a and b are not modified

c = dot(a, b); // a and b are not modified

c = -a; // a is not modified


PARTNERS