This vs. that
-
I have a question about your preferred programming style. Whenever I write code in C# I make extensive use of the 'this' keyword and I use class names when referencing statics. So:
this.SomeProperty = someValue;
this.SomeMethod();
ThisClass.SomeStaticMethod();Instead of:
SomeProperty = someValue;
SomeMethod();
SomeStaticMethod();My reasoning behind this is that I can tell something is an instance method or a static method. And to be completely honest it's also something I started doing in VB because VB has Modules and Modules don't make you specify the Module's name. So
Me.SomeMethod()
can be an instance method, a Shared/static method on the current class or a method in some Module! Now there's a new guy at work and he really hates this style of programming because he thinks it's redundant. I'm not asking for right or wrong (unless I'm the one who's right ;p), but I want to know personal preferences.public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
}this
is good and I use it normally. The fact that it seems to annoy some pooples just encourages me to use it more. :-D -
I have a question about your preferred programming style. Whenever I write code in C# I make extensive use of the 'this' keyword and I use class names when referencing statics. So:
this.SomeProperty = someValue;
this.SomeMethod();
ThisClass.SomeStaticMethod();Instead of:
SomeProperty = someValue;
SomeMethod();
SomeStaticMethod();My reasoning behind this is that I can tell something is an instance method or a static method. And to be completely honest it's also something I started doing in VB because VB has Modules and Modules don't make you specify the Module's name. So
Me.SomeMethod()
can be an instance method, a Shared/static method on the current class or a method in some Module! Now there's a new guy at work and he really hates this style of programming because he thinks it's redundant. I'm not asking for right or wrong (unless I'm the one who's right ;p), but I want to know personal preferences.public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
} -
My preference is to never use this unless there is a reason to do so (like conflicting names). My methods are small enough that it is rarely a problem to see what is local and what is a property and anyway, properties begin with a Capital and locals don't. For static methods I use the classname. option, as there is no other obvious differentiator. That said, I don't work with many classes with static methods, so not something I have formed a habit out of. When I see this.SomeProperty = this.SomeMethod(this.SomeOtherProperty) I cringe, and think "this? What else?)
PooperPig - Coming Soon
_Maxxx_ wrote:
(like conflicting names)
:thumbsup::thumbsup::thumbsup:
-
this
is good and I use it normally. The fact that it seems to annoy some pooples just encourages me to use it more. :-Dsuch a trouble maker and non-conformist. :-D
-
"this." triggers any auto-completion or Intellisense in the world. Yes I'm lazy - I wouldn't train machines to do men's work if I wasn't ;)
-
I have a question about your preferred programming style. Whenever I write code in C# I make extensive use of the 'this' keyword and I use class names when referencing statics. So:
this.SomeProperty = someValue;
this.SomeMethod();
ThisClass.SomeStaticMethod();Instead of:
SomeProperty = someValue;
SomeMethod();
SomeStaticMethod();My reasoning behind this is that I can tell something is an instance method or a static method. And to be completely honest it's also something I started doing in VB because VB has Modules and Modules don't make you specify the Module's name. So
Me.SomeMethod()
can be an instance method, a Shared/static method on the current class or a method in some Module! Now there's a new guy at work and he really hates this style of programming because he thinks it's redundant. I'm not asking for right or wrong (unless I'm the one who's right ;p), but I want to know personal preferences.public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
}It has become a habit for me to use 'this,' to type in access modifiers even when not required, and to never create duplicate names, but, over the last two years, I have transitioned to using 'var freely which I know causes some people to break out in a poison sweat. I don't think these are particularly "bad" habits, but they are not any saving-grace of poor-quality design, or coding, either. At best, a syntactic sugar that may contribute to maintainability and future re-use ? But, I work alone, not in a team-setting. If (gods forbid) I was managing a commercial software dev team, oh yeah, I'd plump for standards.
«OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. » Alan Kay's clarification on what he meant by the term "Object" in "Object-Oriented Programming."
-
It has become a habit for me to use 'this,' to type in access modifiers even when not required, and to never create duplicate names, but, over the last two years, I have transitioned to using 'var freely which I know causes some people to break out in a poison sweat. I don't think these are particularly "bad" habits, but they are not any saving-grace of poor-quality design, or coding, either. At best, a syntactic sugar that may contribute to maintainability and future re-use ? But, I work alone, not in a team-setting. If (gods forbid) I was managing a commercial software dev team, oh yeah, I'd plump for standards.
«OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. » Alan Kay's clarification on what he meant by the term "Object" in "Object-Oriented Programming."
-
It has become a habit for me to use 'this,' to type in access modifiers even when not required, and to never create duplicate names, but, over the last two years, I have transitioned to using 'var freely which I know causes some people to break out in a poison sweat. I don't think these are particularly "bad" habits, but they are not any saving-grace of poor-quality design, or coding, either. At best, a syntactic sugar that may contribute to maintainability and future re-use ? But, I work alone, not in a team-setting. If (gods forbid) I was managing a commercial software dev team, oh yeah, I'd plump for standards.
«OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. » Alan Kay's clarification on what he meant by the term "Object" in "Object-Oriented Programming."
BillWoodruff wrote:
At best, a syntactic sugar
Almost agreed. The
var
keyword can actually be quite dangerous as I have witnessed a few times myself. I work with Entity Framework a lot, which usesIQueryable
to create queries that run on the database and return a result asIEnumerable
that can be used in your application. Interface-wise there is little difference betweenIQueryable
andIEnumerable
, but the function of the two differs greatly! A few times I changed myIQueryable
toIEnumerable
and nothing broke, because theIQueryable
was declared usingvar
. The type of my variable just 'silently' changed toIEnumerable
and all of a sudden my app wouldn't perform very well. Thing is, in order to create anIEnumerable
the query was executed on the database. This was supposed to happen a few lines later, but because I put aToList()
function somewhere in between this was done to early. All my filter functions were done on the in-memory collection instead of the database. Of course you'd also have this problem if you declared yourIQueryable
asIEnumerable
(anIQueryable
IS anIEnumerable
), but at least you'd explicitly choose to define it as such. I haven't seen this problem with other .NET interfaces, probably because none of them are so common and so costly asIQueryable
, but I could imagine it happening when working withStreams
or some such base class which has a few subclasses that do different things.public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
} -
If you just type one or two letters and then press Ctrl + Space, you would also get Intellisense and have to type even less. ;)
The good thing about pessimism is, that you are always either right or pleasently surprised.
-
By that same reasoning, stop using any "using" and reference only via fully qualified "System.Windows.Forms.ListCtrl.Item item" :rolleyes: who needs any shortening of a line, just buy BLACK FRIDAY 50" MONITOR THATS IT?
Yep, I know. But I prefer not to include any Namespaces, only class names (the class name should be clear enough anyway). I understand that in the new version of C# you can create a using for a class so you can use its static members without specifying the class. It's already my least favourite feature!
public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
} -
I have a question about your preferred programming style. Whenever I write code in C# I make extensive use of the 'this' keyword and I use class names when referencing statics. So:
this.SomeProperty = someValue;
this.SomeMethod();
ThisClass.SomeStaticMethod();Instead of:
SomeProperty = someValue;
SomeMethod();
SomeStaticMethod();My reasoning behind this is that I can tell something is an instance method or a static method. And to be completely honest it's also something I started doing in VB because VB has Modules and Modules don't make you specify the Module's name. So
Me.SomeMethod()
can be an instance method, a Shared/static method on the current class or a method in some Module! Now there's a new guy at work and he really hates this style of programming because he thinks it's redundant. I'm not asking for right or wrong (unless I'm the one who's right ;p), but I want to know personal preferences.public class SanderRossel : Lazy<Person>
{
public void DoWork()
{
throw new NotSupportedException();
}
} -
Got a link?
I did some searching but could not find that any more. Looks like they don't have a single document any more. This was from several years back. Once you installed the StyleCop plugin (was not part of VS then), it would issue warnings when you accessed a member without prefixing
this.
Regards, Nish
Blog: voidnish.wordpress.com
-
Got a link?
Managed to dig out the old StyleCop page : http://stylecop.soyuz5.com/SA1101.html[^] Could not locate the original full document, sorry.
Regards, Nish
Blog: voidnish.wordpress.com
-
I did some searching but could not find that any more. Looks like they don't have a single document any more. This was from several years back. Once you installed the StyleCop plugin (was not part of VS then), it would issue warnings when you accessed a member without prefixing
this.
Regards, Nish
Blog: voidnish.wordpress.com
Nish Sivakumar wrote:
StyleCop
X|
-
Nish Sivakumar wrote:
StyleCop
X|