Do you "organize" your 'using' statements?

More and more I find myself "grouping" my using statements ("Imports" in VB.NET). For example, I now try and do Microsoft framework namespaces first, then namespaces for DLL's developed at my employer and finally third party namespaces. Something like this:

using System;
using System.IO;
using System.Data;

using <company>
using <company>.<module>

using <third-party>
using <third-party>.<module>

Not sure why I do this. If anything, it gives me a rough idea of the types of references in the project. Those "groups" usually belong to a specific set of assemblies.

Or maybe I'm just not getting enough sleep... :)

Published Tuesday, March 22, 2005 10:05 PM by PSteele

Comments

# re: Do you "organize" your 'using' statements?

I always do this too, normally mine are..

Microsoft
Third Party
Company

Tuesday, March 22, 2005 10:15 PM by Scott McCulloch

# re: Do you "organize" your 'using' statements?

me too...

i've also taken to making sure there's a line of whitespace after #region, and before #endregion. it just looks better.

you can never be too anal about code style, or... well... maybe you can. ;)

Tuesday, March 22, 2005 10:25 PM by Kirk Marple

# re: Do you "organize" your 'using' statements?

Heh - I also predominantly use regions in VB.NET now, and have my code organised like ..

#Region " Controls "
#Region " Private Members "
#Region " Private Methods "
#Region " Event Handlers "

The regions are formatted much nicer in VB.NET too.

Tuesday, March 22, 2005 10:55 PM by Scott McCulloch

# re: Do you "organize" your 'using' statements?

Absolutely. I use slighltly modified order: System (Microsoft) namespaces, employer namespaces, application/solution namespaces, third-party namespaces. Within each one of these, I alpha-sort (e.g. System.Data followed by System.IO). Doing it this way makes it much easier to scan through the namespaces, IMO

Tuesday, March 22, 2005 11:28 PM by Nino Benvenuti

# re: Do you "organize" your 'using' statements?

Absolutely. I use slighltly modified order: System (Microsoft) namespaces, employer namespaces, application/solution namespaces, third-party namespaces. Within each one of these, I alpha-sort (e.g. System.Data followed by System.IO). Doing it this way makes it much easier to scan through the namespaces, IMO

Tuesday, March 22, 2005 11:29 PM by Nino Benvenuti

# re: Do you "organize" your 'using' statements?

I usually do it as follows:

using System;
using System.Data;
using System.IO;

using <third-party>
using <third-party>.<module>

using <company>
using <company>.<module>

Notice that I use alphabetical ordering withing each group and I put the third party before the company.

Tuesday, March 22, 2005 11:54 PM by Victor Boctor

# re: Do you "organize" your 'using' statements?

We do the same split.

Wednesday, March 23, 2005 1:32 AM by Thomas Tomiczek

# re: Do you "organize" your 'using' statements?

I like your style,and I also do it like this.

Wednesday, March 23, 2005 2:42 AM by na57

# re: Do you "organize" your 'using' statements?

Same here, including the alpha-sorting. Never knew this was so common.

I've gotta comment on Scott McCullogh though. I don't like it when people use regions to separately group private member variables, events ...

I find that it makes the class much harder to maintain. Just adding a property to a class will make you open many regions.

Statements that belong together, should be grouped together. Therefore, I typically use regions to group functionality within my class, not accessibility or types (members, methods, events).

I'll give a typical example of how I group things:

public class Customer : IBizObject
{
#region IBizObject implementation
private Guid id;
public Guid ID
{
get { return id; }
}
#endregion

// I always keep the private member and the property together
private string name;
public string Name
{
get { return name; }
set
{
name = value;
OnNameChanged(EventArgs.Empty);
}
}

// I keep events that deal specifically with the property close to the property
protected virtual void OnNameChanged(EventArgs e)
{
if (NameChanged != null) NameChanged(this, e);
}

public event EventHandler NameChanged;

// Any methods that are specific to Name
public void MakeNameUpper()
{
Name = Name.ToUpper();
}

// Other properties go here
}

This way of working has the added benefit that, when I don't feel the need to use regions (which would mean all members are being used together), the class is usually well-factored and does only one job, and does it well.

Wednesday, March 23, 2005 3:56 AM by Geert Baeyaert

# re: Do you "organize" your 'using' statements?

I group them too. It otherwise looks so cluttered. But then again, I also group private member variables in different regions, properties in a region, event handlers in a separate section of the code (below the form init region) and all other code above the region, class constructors as the first methods in the class, then publics, then internals/protected then private methods.

Wednesday, March 23, 2005 4:18 AM by Frans Bouma

# re: Do you "organize" your 'using' statements?

I sure do, but they also have to be ordered alphabetically within each group. System.IO before System.Data - what are you thinking!? ;-)

Anally retentive? Yeah, probably... you should see my CD collection.

Wednesday, March 23, 2005 4:29 AM by Ian

# re: Do you "organize" your 'using' statements?

I blogged about implementing such a feature here:

http://blogs.msdn.com/cyrusn/archive/2004/12/04/275043.aspx

Wednesday, March 23, 2005 5:53 AM by Cyrus Najmabadi

# re: Do you "organize" your 'using' statements?

I do a very similar thing in the namespace imports and I use my #region like this:

#region Code that does XYZ

//code here

#endregion Code that does XYZ

The label *after* the #endregion helps me when an expanded region is too long and I need to figure out where it finishes.

In VB, similar effect can be achieved like this:

#Region "Code that does XYZ"

'code here

#EndRegion 'Code that does XYZ

Wednesday, March 23, 2005 10:07 AM by Sergio Pereira

# re: Do you "organize" your 'using' statements?

I do it too!

Wednesday, March 23, 2005 1:57 PM by Paul Speranza

# re:Do you

^_^,Pretty Good!

Sunday, April 10, 2005 6:03 AM by TrackBack