Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualServant of the Lord

Posted 31 March 2013 - 04:24 PM

Another case which I was just reviewing is differentiation of action.

Excellent example!

So with a static function you could get both behaviors in an explicit manner:

 
class vector3
{
public:
   vector3&     Normalize();  // Normalize "this" vector.
   static vector3 Normalize( const vector3& );
};
 
From the signatures there is pretty much no way you can read those incorrectly or expect the behavior to be different than it is.  Not suggesting this is a great answer for a vector, I was just thinking about this recently as mentioned and this was a use/case.


Some people try to distinguish them by their names as well. 'Normalize' (normalizes 'this'), 'Normalized' (returns a copy that is normalized). The benefit is that you see the difference when reading the function when in use, and not just when reading the function definition.
vecA.Normalize(); //Unknown whether this is returning or not.
vs:
Vector vecB = vecA.Normalized(); //Definitely returning.
Typically I just have my codebase always operate on 'this' and copy explicitely, but that's just by habit - not by consciously thinking it through - but I figure I'd throw out the naming difference as an idea that others sometimes use. I started a thread inquiring about this a year ago, which might be interesting.

Also note that with static vs non-static similarly named functions, you have to be aware of an unintuitive C++ feature: You can call static functions from a class instance.
myInstance.StaticFunction(27, "blah");
I always call my static functions using the class scope explicitly (even if called from within other class functions), as there is zero ambiguity in that method:
int result = MyClass::StaticFunction(27, "blah");

#2Servant of the Lord

Posted 31 March 2013 - 04:05 PM

Another case which I was just reviewing is differentiation of action.

Excellent example!

class vector3
{
public:
   void     Normalize();  // Normalize "this" vector without return.  Folks will be annoyed..
   // or
   vector3 Normalize() const;  // Normalize the vector and return it, not modifying "this".
   // or a poorly written library could have the above and this next one at the same time:
   vector3& Normalize(); // Normalize "this" vector and return a reference.
   // Ack, same usage completely different meaning, which gets called?  Probably the compiler would bitch
   // since it doesn't know which to call in some cases.
};

The first and third normalize won't compile together [example], because the return value of functions aren't a part of a function's 'signature', though arguments are (you can't overload on return-value alone). Const is part of the signature though, so you can overload by const.

 

So with a static function you could get both behaviors in an explicit manner:
 

 
class vector3
{
public:
   vector3&     Normalize();  // Normalize "this" vector.
   static vector3 Normalize( const vector3& );
};
 
From the signatures there is pretty much no way you can read those incorrectly or expect the behavior to be different than it is.  Not suggesting this is a great answer for a vector, I was just thinking about this recently as mentioned and this was a use/case.

Some people try to distinguish them by their names as well. 'Normalize' (normalizes 'this'), 'Normalized' (returns a copy that is normalized). The benefit is that you see the difference when reading the function when in use, and not just when reading the function definition.

vecA.Normalize(); //Unknown whether this is returning or not.
vs:
Vector vecB = vecA.Normalized(); //Definitely returning.

Typically I just have my codebase always operate on 'this' and copy explicitely, but that's just by habit - not by consciously thinking it through - but I figure I'd throw out the naming difference as an idea that others sometimes use. I started a thread inquiring about this a year ago, which might be interesting.

Also note that with static vs non-static similarly named functions, you have to be aware of an unintuitive C++ feature: You can call static functions from a class instance.

myInstance.StaticFunction();

I always call my static functions using the class scope explicitly (even if called from within other class functions), as there is zero ambiguity in that method:

int result = MyClass::StaticFunction(27, "blah");

#1Servant of the Lord

Posted 31 March 2013 - 04:04 PM

Another case which I was just reviewing is differentiation of action.

Excellent example!


class vector3
{
public:
   void     Normalize();  // Normalize "this" vector without return.  Folks will be annoyed..
   // or
   vector3 Normalize() const;  // Normalize the vector and return it, not modifying "this".
   // or a poorly written library could have the above and this next one at the same time:
   vector3& Normalize(); // Normalize "this" vector and return a reference.
   // Ack, same usage completely different meaning, which gets called?  Probably the compiler would bitch
   // since it doesn't know which to call in some cases.
};


The first and third normalize won't compile together [example], because the return value of functions aren't a part of a function's 'signature', though arguments are (you can't overload on return-value alone). Const is part of the signature though, so you can overload by const.

So with a static function you could get both behaviors in an explicit manner:

 
class vector3
{
public:
   vector3&     Normalize();  // Normalize "this" vector.
   static vector3 Normalize( const vector3& );
};
 
From the signatures there is pretty much no way you can read those incorrectly or expect the behavior to be different than it is.  Not suggesting this is a great answer for a vector, I was just thinking about this recently as mentioned and this was a use/case.


Some people try to distinguish them by their names as well. 'Normalize' (normalizes 'this'), 'Normalized' (returns a copy that is normalized). The benefit is that you see the difference when reading the function when in use, and not just when reading the function definition.
vecA.Normalize(); //Unknown whether this is returning or not.
vs:
Vector vecB = vecA.Normalized(); //Definitely returning.
Typically I just have my codebase always operate on 'this' and copy explicitely, but that's just by habit - not by consciously thinking it through - but I figure I'd throw out the naming difference as an idea that others sometimes use. I started a thread inquiring about this a year ago, which might be interesting.

Also note that with static vs non-static similarly named functions, you have to be aware of an unintuitive C++ feature: You can call static functions from a class instance.
myInstance.StaticFunction();
I always call my static functions using the class scope explicitly (even if called from within other class functions), as there is zero ambiguity in that method:
int result = MyClass::StaticFunction(27, "blah");

PARTNERS