Best practices question: Do you use the this keyword when using instance members in a method?
-
[EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]
class Car
{
private int speed;
// ...
}If you have a class Car with an instance member speed, how do you refer to it in Car's methods?
public void SpeedUp(int delta)
{
speed += delta;
}or
public void SpeedUp(int delta)
{
this.speed += delta;
}I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006
-
[EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]
class Car
{
private int speed;
// ...
}If you have a class Car with an instance member speed, how do you refer to it in Car's methods?
public void SpeedUp(int delta)
{
speed += delta;
}or
public void SpeedUp(int delta)
{
this.speed += delta;
}I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006
See http://blog.voidnish.com/?p=106[^] for what a missing "this->" can result in :-) Regards, Nish
My blog : Nish’s thoughts on MFC, C++/CLI and .NET
-
[EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]
class Car
{
private int speed;
// ...
}If you have a class Car with an instance member speed, how do you refer to it in Car's methods?
public void SpeedUp(int delta)
{
speed += delta;
}or
public void SpeedUp(int delta)
{
this.speed += delta;
}I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006
-
See http://blog.voidnish.com/?p=106[^] for what a missing "this->" can result in :-) Regards, Nish
My blog : Nish’s thoughts on MFC, C++/CLI and .NET
Is that C++? If it is, what exactly does that code do? I've never seen "this->!CMyClass();" before, and neither have the other programmers in our shop. If it's not C++, don't respnd to this post. ------- sig starts "I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
-
Is that C++? If it is, what exactly does that code do? I've never seen "this->!CMyClass();" before, and neither have the other programmers in our shop. If it's not C++, don't respnd to this post. ------- sig starts "I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
Looks like C++/CLI. The Managed C++ replacement.
-
[EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]
class Car
{
private int speed;
// ...
}If you have a class Car with an instance member speed, how do you refer to it in Car's methods?
public void SpeedUp(int delta)
{
speed += delta;
}or
public void SpeedUp(int delta)
{
this.speed += delta;
}I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006
- I use the first thing, and have never had to worry about "name clashes". 2) In your third code block, "this." is not going to compile. ------- sig starts "I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
-
Is that C++? If it is, what exactly does that code do? I've never seen "this->!CMyClass();" before, and neither have the other programmers in our shop. If it's not C++, don't respnd to this post. ------- sig starts "I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
John Simmons / outlaw programmer wrote:
Is that C++? If it is, what exactly does that code do? I've never seen "this->!CMyClass();" before, and neither have the other programmers in our shop. If it's not C++, don't respnd to this post.
That's C++/CLI which is still C++ :-) It's the new syntax available in VC++ 2005 that lets you write managed code. Regards, Nish
My blog : Nish’s thoughts on MFC, C++/CLI and .NET
-
Looks like C++/CLI. The Managed C++ replacement.
Glenn Dawson wrote:
Looks like C++/CLI.
Yep, that's what it is. Regards, Nish
My blog : Nish’s thoughts on MFC, C++/CLI and .NET
-
- I use the first thing, and have never had to worry about "name clashes". 2) In your third code block, "this." is not going to compile. ------- sig starts "I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
John Simmons / outlaw programmer wrote:
- In your third code block, "this." is not going to compile.
John, Vikram is a C# guy :-) So that will compile. Regards, Nish
My blog : Nish’s thoughts on MFC, C++/CLI and .NET
-
- I use the first thing, and have never had to worry about "name clashes". 2) In your third code block, "this." is not going to compile. ------- sig starts "I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
C# Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.
-
[EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]
class Car
{
private int speed;
// ...
}If you have a class Car with an instance member speed, how do you refer to it in Car's methods?
public void SpeedUp(int delta)
{
speed += delta;
}or
public void SpeedUp(int delta)
{
this.speed += delta;
}I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006
NO! Marc Pensieve
-
John Simmons / outlaw programmer wrote:
Is that C++? If it is, what exactly does that code do? I've never seen "this->!CMyClass();" before, and neither have the other programmers in our shop. If it's not C++, don't respnd to this post.
That's C++/CLI which is still C++ :-) It's the new syntax available in VC++ 2005 that lets you write managed code. Regards, Nish
My blog : Nish’s thoughts on MFC, C++/CLI and .NET
[edit] This was a reply to Nish. :suss: [/edit] *whew* I thought it was regular C++ and went "What on earth is that? :wtf: Have I forgotten C++ syntax in just a year?" I also posted a question on your blog. :-O Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006
-
Is that C++? If it is, what exactly does that code do? I've never seen "this->!CMyClass();" before, and neither have the other programmers in our shop. If it's not C++, don't respnd to this post. ------- sig starts "I've heard some drivers saying, 'We're going too fast here...'. If you're not here to race, go the hell home - don't come here and grumble about going too fast. Why don't you tie a kerosene rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
I think it is possible if you overload the () and ! operators in your class. -------- "I say no to drugs, but they don't listen." - Marilyn Manson
-
NO! Marc Pensieve
An unqualified denial, eh? :) Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi.
-
I use this. because of intesence. I tend toward long descriptive variable/method names, and with intelisence can generally get away with typing 7-10 chars instead of 15-25.
dan neely wrote:
because of intesence.
In VS2005, intellisense kicks in without needing "this.". :-D Marc Pensieve
-
[EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]
class Car
{
private int speed;
// ...
}If you have a class Car with an instance member speed, how do you refer to it in Car's methods?
public void SpeedUp(int delta)
{
speed += delta;
}or
public void SpeedUp(int delta)
{
this.speed += delta;
}I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006
-
[EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]
class Car
{
private int speed;
// ...
}If you have a class Car with an instance member speed, how do you refer to it in Car's methods?
public void SpeedUp(int delta)
{
speed += delta;
}or
public void SpeedUp(int delta)
{
this.speed += delta;
}I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006
Former. Always. If i have to worry about name clashes, the code isn't going to be readable anyway, since either i'm using way too many global variables, or the method has become so long that i cannot see at a glance which local variables have been defined. Either way, i have bigger problems.
---- Scripts i've known... CPhog 0.9.9 - make CP better. Forum Bookmark 0.2.1 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums
-
dan neely wrote:
because of intesence.
In VS2005, intellisense kicks in without needing "this.". :-D Marc Pensieve
-
dan neely wrote:
because of intesence.
In VS2005, intellisense kicks in without needing "this.". :-D Marc Pensieve
Marc Clifton wrote:
In VS2005, intellisense kicks in without needing "this.".
Only for C# and possibly VB. It doesn't do that for C++ :-( Regards, Nish
My blog : Nish’s thoughts on MFC, C++/CLI and .NET
-
[EDIT]Code given below is in C#, but the question holds for C++ and Java too, with suitable modifications.[/EDIT]
class Car
{
private int speed;
// ...
}If you have a class Car with an instance member speed, how do you refer to it in Car's methods?
public void SpeedUp(int delta)
{
speed += delta;
}or
public void SpeedUp(int delta)
{
this.speed += delta;
}I used to practise the former style and used the this keyword only to resolve name clashes, but am now converted to the latter style. Sure, there's Intellisense, but when I'm poring over thousands of lines of code, I don't want to move my mouse to the variable in question and hover it there. Cheers, Vikram.
"When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton. i dont mind to be a stupid,better than being a moron - Adnan Siddiqi. -- modified at 10:17 Tuesday 24th January, 2006
Since no one gave my own personal answer on this: I use this in c# when I don't feel like typing out the whole variable name. So if you look in my code you will find it randomly about 30% of the time. It's a bit of a non-issue really. If intellisense didn't require it (and it sounds like it might not in vs 2k5) I wouldn't use it at all, why type more than necessary.