[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

A question about C# style.

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
derekl
Asked:
derekl
2 Solutions
 
smitty22Commented:
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
 
LanceWLarsenCommented:
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
 
LanceWLarsenCommented:
"smitty22" has a good point on question #1 -- I'm more of a <CTRL-J> kind of coder
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
tinchosCommented:
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
 
zrhCommented:
Ditto to what tincho said...
ZRH
0
 
kellycoinguyCommented:
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
 
tinchosCommented:
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
 
tinchosCommented:
Being this one just a 50 point questions I agree with it

Tincho
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now