ENHANCING YOUR CODE WITH COMMENTS


Comments are often considered to be one of the distinguishing marks of a professional programmer. Comments are ways of making notes about what your code is doing. As your projects become larger and more complicated, the number and complexity of the programming statements that you add to them will rise. As the amount of code in an application grows, it becomes more and more difficult to understand what is happening. You might understand program code that you write today, but without comments, the code might be indecipherable a year or two later. It can be especially difficult for another programmer to follow behind you and make modifications or enhancements to your code.

By embedding comments at key points in your applications, you can leave behind notes that explain why you did things the way you did. Because commenting helps you to clarify your thought process, it can be critical in helping to deal with program mistakes and in helping other programmers to build larger projects.

Hint 

A comment is a text statement that is embedded within an application used to document the source code of a program for the programmer, but that is ignored by the application.

In Visual C++, comments start with two forward slashes, as demonstrated here:

 //Define a generic counter for the application Int16 intCounter; 

Trick 

You can create comments around any statement using the /* and */ characters. The slash and then asterisk denotes the beginning of a body of comment text, and the asterisk and then slash denotes the ending of that body. Here is an example:

 /* Type anything you want in the comment block     When you are finished, close the comment block like so: */ 

This style of commenting is a holdover from Visual C++'s roots in the C language. In general, C++ programmers usually prefer to use two forward slashes for each line of comments to make each comment line clear.

As you can see, the first statement in the previous example documents what the following statement does. When executed, Visual C++ ignores the first statement, executing only the second line of code. Visual C++ also allows you to add comments to the end of other statements, as demonstrated here:

 MessageBox::Show( "ABC123" ); //Show key 

Trick 

Another good use of comments is to temporarily comment out statements when testing your application at development time. For example, in the course of testing your application, you might place a temporary message that confirms that a function within the program has been reached, as demonstrated here:

 private: System::Void Form1_Load(System::Object^ sender,           System::EventArgs^ e)           {             MessageBox::Show( "Form_Load Successful." );           } 

However, seeing the pop-up window displayed by the MessageBox::Show method appear every time you test the execution of your application might get tiresome after a while. To speed up your testing, you might choose to comment out the MessageBox::Show statement while you are working on some other area of your application.

 private: System::Void Form1_Load(System::Object^ sender,           System::EventArgs^ e)           {             //MessageBox::Show( "Form_Load Successful." );           } 

When you are finished making changes, all you have to do is remove the comment, and your application resumes displaying the pop-up dialog box whenever it is loaded.




Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
ISBN: 735615381
EAN: N/A
Year: 2005
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net