Solved

A question about C# style.

Posted on 2003-10-24
10
288 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

14 Experts available now in Live!

Get 1:1 Help Now