Solved

A question about C# style.

Posted on 2003-10-24
10
286 Views
Last Modified: 2010-04-16
OK, I know there are probably as many answers to this question as there are developers, but I'm going to ask anyway.

In looking at a lot of C# sample code I've seen some things that I have questions about and would just like to get people's thoughts:

1.)  Why include the this keyword when accessing class members?  I've seen code where every access of a member includes this.
2.)  What do people think about using the entire namespace name to define an object versus using a short form of the namespace based on a "using" directive?

Thanks in advance.
0
Comment
Question by:derekl
10 Comments
 
LVL 2

Accepted Solution

by:
smitty22 earned 25 total points
ID: 9617644
1)  The code was most likely developed in Visual Studio, where using "this" allows the developer to take advantage of intellisense, which results in less keystrokes.  It may look like more, but it's usually less in most cases.
0
 

Assisted Solution

by:LanceWLarsen
LanceWLarsen earned 25 total points
ID: 9617675
On question #1 -- As someone who does not include the "this" keyword in the situation that you described in your question -- as in using "this.foo" when using "foo" works -- I can only speculate that it is done for coding clairity, i.e. you know you're referring to the specific class in which you are within context.  To the best of my knowledge (and anyone who knows otherwise please chime in) this does not impact the compiler or in any way optimize compilation.

On question #2 -- As to fully qualifying the name space versus utilizing a "using" directive -- I very much do this in the cases where I'm only using a method in a few places -- this certainly clairfies what you're calling and allows for fewer searches for which method belongs to which "using" directive when using CPOOP (Cut-n-Pase Object Oriented Programing <g>) coding techniques.

Regards...

...Lance
0
 

Expert Comment

by:LanceWLarsen
ID: 9617688
"smitty22" has a good point on question #1 -- I'm more of a <CTRL-J> kind of coder
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9618509
I would like to start saying that its all a matter of coding style, so lets go step by step

1.)  Why include the this keyword when accessing class members?  I've seen code where every access of a member includes this.

The idea to use the this keyword is just to make clear that you're refering to the member or method of the class.
It's just in order to make it easier to know what you're talking about........

Just imagine a big code, where the this keyword is not used........ and in the middle you have something like

list = new ArrayList();        

you don't know if the list is a local variable of the method, or a class member.

Apart from that it is also used when people define their member without any particular notation and they may get confused with the method paramters.

For example:

public class Point3D
{
   public Point3D( float x, float y, float z )
   {
       this.x = x;          // In this case, the this keyword allows you to use the same names without any problem
       this.y = y;
       this.z = z;
   }

   private float x;
   private float y;
   private float z;
}



2.)  What do people think about using the entire namespace name to define an object versus using a short form of the namespace based on a "using" directive?

Its almost the same that in the previous point

just to make sure that you're refering to the right class

suppose you have the Foo class in the Graphic namespace and the Foo class in the Model namespace

if you have

using namespace Graphic;
using namespace Model;

// Code

and then you do

Foo foo = new Foo();           // What class are you refering to?

However, I do not use this style. I'd rather use the using namespaces directives and try to avoid repeating the class names.
But then again, it's just a matter of coding style.

Hope it helps

Tincho

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 6

Expert Comment

by:zrh
ID: 9618671
Ditto to what tincho said...
ZRH
0
 
LVL 1

Expert Comment

by:kellycoinguy
ID: 9625329
While the example above works... what happens when some greenhorn comes along with an eye to optimizing and does this not knowing the ramifications of his actions:

public class Point3D
{
   public Point3D( float x, float y, float z )
   {
       x = x;
       y = y;
       z = z;
   }

   private float x;
   private float y;
   private float z;
}

I prefer a very light kind of hungarian notation to keep things straight.

public class Point3D
{
   public Point3D( float pX, float pY, float pZ )
   {
       x = pX;
       y = pY;
       z = pZ;
   }

   private float x;
   private float y;
   private float z;
}

Although in reality I'd probably make all three floats properties as well.

I once asked people if they rolled toilet paper from the top or bottom? It all gets the same result, but religious wars have been fought over less controversial subjects. I guess my only real point is don't do something that could confuse someone who knows less about the language than you do, unless it's for a very good reason. Our job isn't to prove that we know more about the language than our coworkers, it's to create code that is both elegant and clear. Also, you may have local standards at your workplace that you'll want to comply with.

-Kelly
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9626371
Sorry kelly, I understand what you're talking about, but I was just answering a question

>> 1.)  Why include the this keyword when accessing class members?  I've seen code where every access of a member includes this.

Although I would also write code like you did, that way of coding wasn't illustrative for the reason of why people sometimes used the this keyword.

My explanation was based on the exampled I provided. I never said that that was what I suggested and in any case I wanted to prove I know more about the language than anybody.

Tincho
0
 
LVL 9

Expert Comment

by:tinchos
ID: 11697509
Being this one just a 50 point questions I agree with it

Tincho
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now