Home
Username:
Password:
Introduction to Microsoft C# 2008 Tutorials

Visual Studio 2008 / Commenting Your Code

Subtitles of the Movie

Now, in this video let's take a look at commenting our code. Now this is one of those touchy subjects with programmers. We're going to get beat up on it no matter what you do. I'm going to show you how to comment your code, then I want to talk to you about comments just a little bit. What I want you to do, if you're following along with me, in your VTC Class program that we started, I want you to just go back to your form, right here, just click on Form 1 VS Design. If you closed it, you can open it back up. Let me show you how to do that. Let's close this and we will save everything and so to open our project back up, we're just going to open Visual Studio and you'll notice it should show you your class. If you don't see that, you can open a project or a solution and you will see it here and you can just open the SLM file. So anyway, that will get you back to where we're all in the same place if you're following along and coding. Now, I'm just going to go right back into my Code Window and I need to comment, I need to explain this useless piece of code here. So what I'm going to do is come back here and there's a couple of things I can do for comments and I'm going to give myself a little room here. First of all, I can put two forward slashes and then everything that I type after those two forward slashes is a comment. The following code is useless but is for demo purposes. Now, this tells anybody who's looking at my code later OK, he wasn't smoking anything; he knew this was useless, he was just showing somebody. Here's the big debate on comments. There's one camp of people who will tell you your code should be so clearly written and should be laid out properly and it should be obvious and evident what it's doing and I couldn't agree more. However, in the real world a lot of times it just kind of doesn't happen that way. Something goofy happens, something doesn't work and you have to write some code to get around a corner and you'll want to comment that. Other developers are going to beat you up and my best advice is remember who is signing your paycheck. Other developers will either say Mark never leaves enough code in there, he has some sort of religious issue with leaving code comments and you know, we can't tell what it's doing and we don't write comments. Now, if I write comments, very much, then I tend to get this. Mark wrote a novel inside here. There's more comments than there is code. He has some sort of religious deal about writing comments and there's always comments everywhere and he need to knock it off. OK? If you're going to get beat up on it, write too many comments because trust me; you're going to come back and look at this code that you wrote about three, four, five weeks from now and you're going to wonder what kind of sinus medicine was I taking that morning? This is not making any sense. So the first thing you're going to do is the double lines. Now, there's something you can do cool with the double lines. OK? I can get on the line that I just coded and I can put double lines and say this line is useless, OK? And that'll run because all I've done is I've put my line of code here and then I've put a comment on that line. Now, you have to be careful here because if you do too many of the in-line comments, it gets messy in your code but just know that you can do that. Now, here's the best way to do this if you're going to write comments. Notice above this method and we haven't talked about methods. We will later. But that's what this is. Some people might call it a subroutine or a function. All those are somewhat interchangeable. But I need to leave a comment here that's going to take more than one line and the way to do that is put a single/ and then put an asterisk. Now, everything that I put after this, notice I'm going to hit Return and now I can type the following code is useless and then I can just continue to type. And notice if I hit Return and continue to type, Return, it just automatically continuing my comments and it's going to continue until I back right up to it and put another/. And at that point I'm out of commenting. OK? What most people do just to make it look cool and easily readable is they will put at asterisks all the way out and then down here on the end they will extend them out again. Now, you can see how nice and easy it is to read. It's obvious and if I pull that up by deleting, it's obvious that that goes with that. I can totally describe, make excuses, whatever you want to call it. This can all describe what's going on programmatically in here if I need it. And so remember to do that. This is called delimiting code. The single /or the double slashes are called single-line comment. This is called delimited commenting. So if you ever hear that, that's what it is. And we start it with a/and an asterisk and we end it with an asterisk and a/; no spaces. OK? Let me show you I got you here. It'll make you think you've lost your mind. If it puts a space and we put a/, it'll keep commenting so you got to back up and put that/right by that little guy there. So that's commenting. Use comments. Trust me, you and everybody else will appreciate it three weeks from now when you have to come back and adjust this code. So that's how to comment your code in C#.

Tutorial Information

Course: Introduction to Microsoft C# 2008
Author: Mark Long
SKU: 34046
ISBN: 1-935320-78-5
Release Date: 2009-10-09
Duration: 7 hrs / 76 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available