C# Naming Convention and Guidelines regarding "this" keyword  
Author Message
Marcos Torres





PostPosted: Visual C# General, C# Naming Convention and Guidelines regarding "this" keyword Top

Hi there!

In the software factory I'm working for, we're trying to define our C# coding standards. Now, I'm having some problems about the use of the "this" keyword.

Our standard says now that "this" should be used only to avoid indentifier names collision (classic in constructors).

Now, lot's of people in the factory say that "this" should be used everywhere, even if there is no indentifier name collision.

for example:

        public class Customers: List<Customer>
        {
            //members and properties omited.

            private void DoSomethingWithCustomers(Foo foo)
            {          
                foreach (Customer customer in this)
                {
                    this.MyFooMethod(this);  //this sucks        

                    if (customer.SomeProperty > foo.Something)
                    {
                        this.BarMethod();
                        //and many other methods with this. as prefix
                    }
                }
             }  
        }

They argue that follows the "object-message" rule. I argue that’s completely redundant, and if you abuse of the ".this", the code is less clear.
What I need is some strong arguments to show my partners that the is abuse and overuse of the ".this" keyword goes against any good coding practices and standards.

Thanks a lot in advance.




Visual C#19  
 
 
Greg Beech





PostPosted: Visual C# General, C# Naming Convention and Guidelines regarding "this" keyword Top

Personally I think it is a very good practice as it gives at-a-glance visibility of the scope. It's similar to the m_ prefixes people used to use to denote this, but without the ugly syntax. It's also helful when writing code as it gives you intellisense for the fields.

Microsoft's own internal standard is to always prefix with "this.", if that sways you in any way.



 
 
Marcos Torres





PostPosted: Visual C# General, C# Naming Convention and Guidelines regarding "this" keyword Top

Are you suggesting that every method should be prefixed with "this" or just a simple recomendation in special circumstances I think if "this" adds readeability to the code, welcome, but suggesting that every method should be prefixed there is a huge difference.

Thanks!



 
 
boban.s





PostPosted: Visual C# General, C# Naming Convention and Guidelines regarding "this" keyword Top

There is no rule about using this, but i think from lately that this should be used only to avoid name collision, and that should be avoided to happend. Also ReSharper addin from 2.0 has added such refactoing that will remove unneccessary this. statments.

 
 
Marcos Torres





PostPosted: Visual C# General, C# Naming Convention and Guidelines regarding "this" keyword Top

Yeah, exactly, ReSharper warns you about redundant "this" over the code, but my partners are happy because they can disable that option in ReSharper.

I'm almost sure that redundant "this" are wrong, but still can’t find any good anwser about the "object-message" argument, specially to guys with background in SmallTalk.

Thanks in advance.

 



 
 
RizwanSharp





PostPosted: Visual C# General, C# Naming Convention and Guidelines regarding "this" keyword Top

I can't see any purpose of using this keyword with Function names and it also looks odd because they can be just called using FunctionFoo(); without any problem. There is not a problem which is available for variables and class field. Using this,better be limited to fileds only because there me be a scenerio where passed parameters to function and class's fields have the same name. e.g

private void SomeFunction(Employee employee)

{

this.employee = employee;

}

There is not any scenerio where this is really needed for function, because functions can be called in folowing ways:

static functions:

ClassName.FunctionName();

Non static functions from other class:

someObject.FunctionName();

Function from the same class:

SomeFunction();

So....... I think we should not use this atleast with functions.....

Best Regards,

Rizwan



 
 
Greg Beech





PostPosted: Visual C# General, C# Naming Convention and Guidelines regarding "this" keyword Top

This is one of those battlegrounds like Hungarian notation where everyone will have a different opinion, and there are plenty of people who will not be swayed by any argument. I always use the "this." prefix because it's the Microsoft internal standard and they have an internal tool "StyleCop" which enforces the use of the prefix; after working on projects there for a while you just get used to it, and I believe it makes the code clearer as it indicates the scope of the method/field/whatever more clearly than no prefix.

Whatever your opinion, the important thing is that all people closely collaborating on a project use the same formatting standards, irrespective of what those standards are. This means that anyone within a group can read each others code more easily without having to worry about how it is formatted or try and read formatting they are not used to. Doesn't sound like a big deal I disagree. It's much harder to understand the semantics of code when you are distracted by how it is formatted. Decide on a standard and make sure everyone uses it, whatever that standard is.



 
 
Matthew Watson





PostPosted: Visual C# General, C# Naming Convention and Guidelines regarding "this" keyword Top

For what it's worth, I use the "this" prefix only with fields, and not with methods.

We also use the convention that private methods begin with a lowercase letter, so you can instantly tell whether a method is public or private. (This would fall down if we had private properties, but we never use private properties.)