Thread: this pointer?

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by medievalelks
    Second, if the variable changes later to become, say, a file scope static variable, the name must change or it becomes misleading.
    Yes, that is akin to a problem with global variables (as in a name change in general), except that the problem is now localised to member and friend functions, which also means that it is less of a problem, especially with a minimal but complete interface.

    Quote Originally Posted by anon
    E.g do you feel tempted to use i as the counter for all nested for-loops?
    Yes! At least where it is used as an index of an array or some other container.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by medievalelks View Post
    For one, I rarely see consistency in how these are applied in any moderately sized project with more than one developer.
    That's why companies need naming & coding standards, so things stay consistent.

    Quote Originally Posted by medievalelks View Post
    Second, if the variable changes later to become, say, a file scope static variable, the name must change or it becomes misleading.
    And how often does that situation actually happen?
    And when it does, how long does a search & replace take?
    You'd need to change it anyways if you're prefixing your member variables with this->
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #18
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes! At least where it is used as an index of an array or some other container.
    I would be more tempted with nesting iterators. i, j, and k are ok (except you might get a temptation to increment i in all nested loops) but what should I call the iterators - it, jt and kt?

    That's why companies need naming & coding standards, so things stay consistent.
    As a hobbyist I have tried using m_ and _ prefix, but since I didn't find them very useful (classes are not that big and I mostly don't write setters/get the temptation to use the same name for arguments/local variables) I couldn't force myself to apply them consistently and dropped them altogether.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #19
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by cpjust View Post
    That's why companies need naming & coding standards, so things stay consistent.
    Right, so a bureacratic layer of pencil pushers can reject projects that "don't conform" to their arbitrary standards. I haven't seen them consistenly followed in over 20 years, and I don't expect I ever will.

    Also, if I had a dime for every set of coding standards I've seen that were 90% style guides with almost zero mention of safe, good, sound coding practices, I'd be rich. These "standard enforcers" will happily pass code that isn't exception safe, is nothing more than C with classes, is inefficient, or otherwise poorly designed, but scream at the first function they find that doesn't have a ten-line comment block prefix.

    And how often does that situation actually happen?
    And when it does, how long does a search & replace take?
    You'd need to change it anyways if you're prefixing your member variables with this->
    I don't use the this-> prefix, so it's not an issue with me. It doesn't matter how long it takes to fix, it means that if you can never really be sure the variable starting with m_ is a member variable without checking.
    Last edited by medievalelks; 03-24-2009 at 04:07 PM.

  5. #20
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by anon View Post
    I'm not changing the argument name, I just give it a different name? Would you go out of your way to use the same name for different variables in different scopes? E.g do you feel tempted to use i as the counter for all nested for-loops?
    I "go out of my way" to make the name of a variable indicate its purpose. My example was generic. Real situations aren't.

    Suppose you are configuring the number of wozzes which should be frobbed. The name "numberOfWozzesToFrob" seems good. If some variable represents such a quantity, I want it to have such a name.

    You can be arbitrary and have the argument be called "numWozToFrob" and the member be "numberOfWozzesToFrob" but why did you do it that way? At least develop a rule which tells you what you should do.

    For a while, I prefixed argument variables with "a". Then I postfixed them with "P". Then I started prefixing members with "m". I'm not militant about any of this, but I definitely don't see how it's harmful.

    Quote Originally Posted by medievalelks
    I don't use the this-> prefix, so it's not an issue with me. It doesn't matter how long it takes to fix, it means that if you can never really be sure the variable starting with m_ is a member variable without checking.
    Because NOT applying a prefix somehow makes it possible to tell without checking?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #21
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can be arbitrary and have the argument be called "numWozToFrob" and the member be "numberOfWozzesToFrob" but why did you do it that way? At least develop a rule which tells you what you should do.
    That might be quite much to ask. Ok, in such cases the member variable shall always be called "count_of..."
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #22
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by brewbuck View Post
    Because NOT applying a prefix somehow makes it possible to tell without checking?
    I check either way, so some arbitrary, enforced standard is useless, and in fact wastes time and resources trying to enforce.

    And it's exactly because no one can be militant enough about style standards that render them useless. Sooner or later you or someone else won't follow the convention, or it will be changed (new code only, of course), and on and on.
    Last edited by medievalelks; 03-24-2009 at 05:06 PM.

  8. #23
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by medievalelks View Post
    I check either way, so some arbitrary, enforced standard is useless, and in fact wastes time and resources trying to enforce.

    And it's exactly because no one can be militant enough about style standards that render them useless. Sooner or later you or someone else won't follow the convention, or it will be changed (new code only, of course), and on and on.
    Coding standards aren't even the topic here... We're talking about how to avoid having to use "this->" while still naming variables meaningful names. Using some prefix on member variables has a pragmatic justification beyond just being mandated by a coding standard.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #24
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by brewbuck View Post
    Using some prefix on member variables has a pragmatic justification beyond just being mandated by a coding standard.
    Until it doesn't.

  10. #25
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by medievalelks View Post
    Until it doesn't.
    You still have provided no example of how it could be harmful.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #26
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by medievalelks View Post
    I don't use the this-> prefix, so it's not an issue with me. It doesn't matter how long it takes to fix, it means that if you can never really be sure the variable starting with m_ is a member variable without checking.
    If you ever work with MY code, you won't have to waste your time checking to see whether a variable is a member variable or not because I ALWAYS use m_ to denote a member variable. You can of course waste your time if you want, but that's all it will ever be.

    Would the code be easier to read if everybody uses a completely different coding style (some indenting with tabs, others with 2 spaces, others with 5 spaces... Some prefixing member variables with m_, others postfixing with _ and others not doing anything)?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #27
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by brewbuck View Post
    You still have provided no example of how it could be harmful.
    Sure I have. It is misleading if used inconsistently or incorrectly, which is inevitable on any but the smallest of toy projects. Sooner or later someone will forget, use a different prefix/convention, or change a variable and forget to rename, etc.

    And, off-topic or not, it's a resource vacuum when the standards Nazis get involved.

  13. #28
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by medievalelks View Post
    Sure I have. It is misleading if used inconsistently or incorrectly, which is inevitable on any but the smallest of toy projects. Sooner or later someone will forget, use a different prefix/convention, or change a variable and forget to rename, etc.

    And, off-topic or not, it's a resource vacuum when the standards Nazis get involved.
    When I worked at a large company that actually had such standards, I never saw anyone forget to use m_ for a member variable. Not for the 5 person QA team nor the 8-10 person Development team. Besides, that's why you make doing code reviews a part of your standards.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  14. #29
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by medievalelks View Post
    Sure I have. It is misleading if used inconsistently or incorrectly, which is inevitable on any but the smallest of toy projects. Sooner or later someone will forget, use a different prefix/convention, or change a variable and forget to rename, etc.
    I still don't grok the argument. It sounds like you're saying that coding standards are dangerous because people might forget to use them. Okay... People also forget to update comments, they rely on undocumented and fragile behavior, they don't validate input, they don't check array bounds before accessing, etc. It seems like the safest practice is to just not write any code at all because somebody might make a mistake.

    Suppose there is a coding standard in place that says "Every variable must be initialized at the point of declaration." I suppose you take issue with this as well, because somebody may forget to adhere? And the answer is to deliberately not initialize variables?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #30
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That is probably somewhat different since it is a bit more than a style question and has more relevance to code correctness.

    And in some cases, probably initializing it to any value is meaningless and the initialization doesn't make the code any more correct:

    Code:
    int n;
    std::cin >> n;
    //did user enter 57361 or did input fail?
    
    int n = 0;
    std::cin >> n;
    //did user enter 0 or did input fail?
    
    int n; //or initialized
    if (!(std::cin >> n))
    //aha!
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM