Customize your code snippets.
Someone mentioned a couple of months ago that my IDE tweaks are minor and insignificant in the larger view of things. I disagree vehemently. A combination of a lot of small tweaks can achieve very large productivity gains in the same way an ant colony can accomplish miracles. So on to my "minor" tweak. It is not a prescription, but rather an example of and a path to productivity.
If you don't use Visual Studio code snippets, that's a problem to begin with. Let's assume that you do use them and improve from there. Let's take a specific example of your daily programming work. You create classes almost every day. So let's analyze the workflow of creating a class.
- You create a class. Let's call it Monkey.
1: public class Monkey
2: { 3:
4: }
-
Then you add a couple of private variables and properties.
1: private string _Name = string.Empty;
2: private string _CountryOfOrigin = string.Empty;
3:
4: public string Name
5: { 6: get { return _Name; } 7: set { _Name = value; } 8: }
9:
10: public string CountryOfOrigin
11: { 12: get { return _CountryOfOrigin; } 13: set { _CountryOfOrigin = value; } 14: }
- Then you realize that all the properties can be set in one shot using a constructor. We will also need a couple of public and private methods that implement the details of the upcoming Ximian war against humanity.
1: public Monkey(string name, string countryOfOrigin)
2: { 3: _Name = name;
4: _CountryOfOrigin = countryOfOrigin;
5: }
6:
7: public void AttackHumans()
8: { 9: Vehicle vehicle = BoardVehicle();
10: DestroyHumans(vehicle);
11: }
12:
13: private Vehicle BoardVehicle()
14: { 15: Vehicle vehicle = Vehicle.FindNearestVehidle();
16:
17: vehicle.BreakWindow();
18: vehicle.Enter();
19:
20: return vehicle;
21: }
22:
23: private void DestroyHumans(Vehicle vehicle)
24: { 25: List<Human> humans = vehicle.ListHumansInside();
26: foreach (Human human in humans)
27: human.Destroy();
28: }
- Then you wrap the constituent pieces in nice collapsible regions and you are done. The result looks something like this:
1: public class Monkey
2: { 3: #region Private Variables
4: private string _Name = string.Empty;
5: private string _CountryOfOrigin = string.Empty;
6: #endregion
7:
8: #region Constructors
9: public Monkey(string name, string countryOfOrigin)
10: { 11: _Name = name;
12: _CountryOfOrigin = countryOfOrigin;
13: }
14: #endregion
15:
16: #region Public Methods
17: public void AttackHumans()
18: { 19: Vehicle vehicle = BoardVehicle();
20: DestroyHumans(vehicle);
21: }
22: #endregion
23:
24: #region Private Methods
25: private Vehicle BoardVehicle()
26: { 27: Vehicle vehicle = Vehicle.FindNearestVehidle();
28:
29: vehicle.BreakWindow();
30: vehicle.Enter();
31:
32: return vehicle;
33: }
34:
35: private void DestroyHumans(Vehicle vehicle)
36: { 37: List<Human> humans = vehicle.ListHumansInside();
38: foreach (Human human in humans)
39: human.Destroy();
40: }
41: #endregion
42:
43: #region Properties
44: public string Name
45: { 46: get { return _Name; } 47: set { _Name = value; } 48: }
49:
50: public string CountryOfOrigin
51: { 52: get { return _CountryOfOrigin; } 53: set { _CountryOfOrigin = value; } 54: }
55: #endregion
56: }
In this particular case, several things could have been automated:
- Creation of the class (e.g. name and the curly braces)
- Creation of the constructor
- And finally all the collapsible regions
As you see, the automation won't bring huge improvements, but it will definitely save time on mindless typing. So why not come up with a snippet that saves you a little bit of time every day. You invoke a snippet and get code below as your starting point. Your class looks beautiful right from the outset.
1: public class Monkey
2: { 3: #region Private Variables
4:
5: #endregion
6:
7: #region Constructors
8: public Monkey()
9: { 10:
11: }
12: #endregion
13:
14: #region Public Methods
15:
16: #endregion
17:
18: #region Private Methods
19:
20: #endregion
21:
22: #region Properties
23:
24: #endregion
25: }
Download and unzip this snippet to the C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C# folder. Then in VS2005 or higher, type class2 and watch the code expand.
If you want to edit your own snippets, Snippet Editor an awesome tool and with a bit of work, it supports C# as well.