Visual Studio Hacks [Electronic resources]

Andrew Lockhart

نسخه متنی -صفحه : 172/ 46
نمايش فراداده

Hack 21. Never Forget a TODO Again

Place reminders for yourself or for other developers that are nearly impossible to miss.

The task list (Ctrl-Alt-K/View.TaskList) is a handy tool that is most often used to view errors or warnings from the compilation of your code. In Visual Studio .NET 2003, when you compile your project, any errors or warnings are displayed in the task list. You can read the message and double-click on the item to jump to the offending line in your code. (Visual Studio 2005 includes a separate error list.)

The task list also has another use; it can be used to leave reminders for yourself or other members of your team in comment form.

3.7.1. Comments

By default, the task list includes three different comments; these are //TODO, //UNDONE, and //HACK.

Visual Studio 2005 includes only the //TODO comment by default; the other comments can still be added through the process outlined later in this hack.

You can use these comments anywhere throughout your code, and they will show up in the task list when the file with the comment is open. Visual Studio .NET 2003 uses this functionality by default when you create a new class; it adds the following code to the constructor of every new class:

//
// TODO: Add constructor logic here
//

First make sure that the task list is configured to show comments, and then you will see in the task list that this comment has been added as an item, as shown in Figure 3-14.

Figure 3-14. Task list comments

The task list does not always show every comment. It needs to be configured according to what you are currently working on. If you are trying to find and fix compilation errors, chances are that you don't want to also see a bunch of comments. What the task list displays is configurable in Visual Studio .NET 2003 through the Show Tasks list under the View menu in the toolbar. There are options to show just build errors, tasks in the current file, all, or comments.

In Visual Studio 2005, the options list has moved to the task list itself. You will now see a drop-down list at the top of the task list that lets you choose what should be displayed in the task list. You will also notice that errors are no longer displayed in the task list in Visual Studio 2005; instead, there is a separate error list just for build errors and warnings.

For the purpose of this hack, options will need to be set to All or Comments.

You can then click on this comment and be taken to the place where you need to add code. The other two comments work much the same way. You simply need to use //HACK: and a task list item will be created for that comment. You can use shortcuts to step back and forth between tasks as well. The View.NextTask ( Ctrl-Shift-F12) and View.PreviousTask (no default shortcut) commands can be used to step through the tasks listed in the task list.

You can also click in the area at the top of the task list with the text "Click here to add a new task," or in Visual Studio 2005, you can click the Create User Task button. This creates a user task for you and acts much like the tasks portion of Outlook. You can also tag any line in your project as a task by using the Edit.ToggleTaskListShortcut (Ctrl-K, Ctrl-H) command. Whenever you call this command, it will add a shortcut to the task list pointing to this line of code. You can then add text that says what should be done to the line of code. It is a quick and easy way to add something to the task list to tackle later on. These tasks will appear only on your system and not on the systems of your team members.

Task list comments are a great way to remind you or others of something that still needs to be done or something that may need to be revisited for enhancement.

3.7.2. Custom Comments

You can also define and use custom comments with the task list. Custom comments can be very useful if you want to create a more specific comment. You can set a priority level higher than the other comments so you can differentiate the comments easier. I'll walk through creating a custom comment called REFACTOR. You could use this comment to mark places that you feel need to be refactored. I will also create a second custom comment called BROKEN that will have a higher priority (since you don't want broken code in your project for long).

To add a new comment, you first need to go to Tools Options and then select Task List under the Environment folder. Then simply type the name of the new comment in the Name textbox, and the Add button will become active. These comments are case sensitive, so using all caps will help to ensure that the names do not get mistypedthis is also consistent with the default Visual Studio tokens. Figure 3-15 shows an example of adding a REFACTOR comment.

Figure 3-15. New task list comment added in Options dialog

You can select a priority of Normal for this comment. After you click Add, this comment will be added to the list. Next, you need to do the same thing for the BROKEN comment by simply repeating the same process, except this time set the priority to High.

After you have added both of the custom comments, you will be able to use them in your code. Figure 3-16 shows your task list after adding both REFACTOR and BROKEN comments to your document.

Figure 3-16. Task list with custom comments

Custom comments can be a valuable tool when trying to relay information to other members on your team or just as a way to remind yourself of something that needs to be done.