Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

A question about C# style.

Posted on 2003-10-24
10
289 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video Player 12 27
Service Controller 1 27
.NET Enums [Flags] & Bitwise Design Question 6 25
C# Linq - Join two objects into one 3 13
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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