//
you're reading...
.NET 2.0, Technology

Enhanced auto-format in Visual Studio 2005

I must say I’m starting to love the automated code formatting that is in Visual Studio 2005.

With it I’m able to turn this awful looking clump of code (a copy/paste from an old C project that used #defines, then added just enough to make it into C# code)

public const int OPT_USEC=	100000;		/* 10 passes per second */
public const int PASSES_PER_SEC	= (1000000 / OPT_USEC);
public const int RL_SEC	= PASSES_PER_SEC;

public const int PULSE_ZONE      =(10 * RL_SEC);
public const int PULSE_MOBILE    =(10 * RL_SEC);
public const int PULSE_VIOLENCE  =( 2 * RL_SEC);
public const int PULSE_AUTOSAVE	=(60 * RL_SEC);
public const int PULSE_IDLEPWD	=(15 * RL_SEC);
public const int PULSE_SANITY	=(30 * RL_SEC);
public const int PULSE_USAGE	=(5 * 60 * RL_SEC);	/* 5 mins */
public const int PULSE_TIMESAVE	=(30 * 60 * RL_SEC); /* truncated */

Into

public const int OPT_USEC = 100000;		/* 10 passes per second */
public const int PASSES_PER_SEC = ( 1000000 / OPT_USEC );
public const int RL_SEC = PASSES_PER_SEC;

public const int PULSE_ZONE = ( 10 * RL_SEC );
public const int PULSE_MOBILE = ( 10 * RL_SEC );
public const int PULSE_VIOLENCE = ( 2 * RL_SEC );
public const int PULSE_AUTOSAVE = ( 60 * RL_SEC );
public const int PULSE_IDLEPWD = ( 15 * RL_SEC );
public const int PULSE_SANITY = ( 30 * RL_SEC );
public const int PULSE_USAGE = ( 5 * 60 * RL_SEC );	/* 5 mins */
public const int PULSE_TIMESAVE = ( 30 * 60 * RL_SEC ); /* truncated*/

Maybe the original just looks worse in VS2005 than it does here…but I am really impressed with what it was able to do.

Update: I forgot to mention that what I really like about the auto-format is that the spacing and line breaks are all options you can set from within VS 2005. There are several different categories (indentation, new lines, spacing, and wrapping) and within those are categories for each type of code. For example:

Under Spacing there is:

Set spacing for method declarations which has the following checkboxes:

  • Insert space between method name and its opening parenthesis
  • Insert space within argument list parentheses
  • Insert space within empty argument list parentheses.

That is just one example of many options. And yes, each option shows sample code that changes for each category as you toggle the options so you know for sure what is happening.

The only option I wish were available would be a difference in spacing for declaring arrays and using the arrays. For example I want
int [] foo = new int[3]; foo[0] = 2;

but currently the number of spaces between the [] and the variable or type is the same. If I tell it to insert the space between the first int and [ I get this:
int [] foo = new int [ 3 ]; foo [ 0 ] = 2;.

Instead I settled for not inserting spaces so I wind up with this:
int[] foo = new int[ 3 ]; foo[ 0 ] = 2;

Close enough for me.

Advertisements

About James

I am a Senior Developer/Consultant for InfoPlanIT, LLC. I previously spent over 7 years as a Product Manager for what eventually became ComponentOne, a division of GrapeCity. While there, I helped to create ActiveReports 7, GrapeCity ActiveAnalysis, and Data Dynamics Reports.

Discussion

One thought on “Enhanced auto-format in Visual Studio 2005

  1. The “public const int” repetitions make a beautiful sight once the colorizer’s done its work 🙂

    Posted by Nish | June 17, 2004, 1:05 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archive

%d bloggers like this: