- Joined
- Jul 10, 2007
- Messages
- 6,306
This tutorial will introduce the nested, delimited, and limited comments as well as good commenting.
This is chapter 3 of the complete guide to vJASS.
Chapter 3- Comments
Comments are little code notes that are used to describe what a line(s) of code does. Any code that is commented out is ignored by the parser. Comments are typically used by programmers to explain why they are doing what they are doing, not what they are doing. They may also go over how to use code and how code works (documentation, normally located above all of the code).
Comments may be single line or delimited.
Single Line Comment-
Delimited Comment (multiple lines)-
A singe line comment will comment out everything past it ->
A delimited comment will comment out everything between /* and */
Commenting Exercises
Good Commenting
This is chapter 3 of the complete guide to vJASS.
Chapter 3- Comments
Comments are little code notes that are used to describe what a line(s) of code does. Any code that is commented out is ignored by the parser. Comments are typically used by programmers to explain why they are doing what they are doing, not what they are doing. They may also go over how to use code and how code works (documentation, normally located above all of the code).
Comments may be single line or delimited.
Single Line Comment-
//
Delimited Comment (multiple lines)-
/*code*/
A singe line comment will comment out everything past it ->
set x = 1 //setting x to 1
. If you notice, //setting x to 1
is colored green. It happens to be a comment.A delimited comment will comment out everything between /* and */
set x /*x is a var*/ = 1 //1 is a value
. Delimited comments may also be nested /*/**/*/. Delimited comments are used for documentation and temporarily commenting out buggy code or test code.
JASS:
/*This stuff is within a delimited comment/*this stuff
is within a nested delimited comment*/
The formatting of a delimited comment doesn't matter as long as it starts with /* and ends with */
*/
Commenting Exercises
Paste the code into the map and then comment it out to get rid of the syntax errors.
Problem 1
Problem 2
JASS:Comment me to get rid of the syntax error
JASS://Comment me to get rid of the syntax error
JASS:I am your syntax error yes, you will not stop me at all
JASS:/*I am your syntax error yes, you will not stop me at all*/
Good Commenting
Bad
The code below describes what each line does, but it doesn't describe why it does it. While the code can be read, it's purpose remains very unclear. Furthermore, the comments seem pointless. For example, in the first line, it is easy to read that i is being set to 1000. What the person reading comments really wants to know is what i is and why it's being set to 1000.
JASS:local integer i = 1000 //set i to 1000 local integer x //declare x //do some loops set x = i //set x to i loop //display some text call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, I2S(x)) set x = x - 1 //decrease x exitwhen x == 0 //exit when x is 0 endloop //do some more loops set x = 0 //reset x loop //dispay some more text call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, I2S(x)) set x = x + 1 //increase x exitwhen x == i //exit when x is i endloop
Good
The code below describes why each action is taken if the action may be unclear. For example, it states that i determines how many times to repeat an action (meaning it's being set to 1000 to repeat it 1000 times). It states x is the current number of repetitions. Even someone who can't read code could understand what the code did by its comments.
JASS:local integer i = 1000 //how many times to repeat an action local integer x //current number of repetitions //repeat backwards to display the value of x in warcraft 3 from the value of i to 0 //i -> 0 set x = i loop call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, I2S(x)) set x = x - 1 exitwhen x == 0 endloop //repeat forwards to display the value of x in warcraft 3 from 0 to the value of i //0 -> i set x = 0 loop call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, I2S(x)) set x = x + 1 exitwhen x == i endloop
There are various styles to making good documentation.
JASS:/*===================================================== =====================================================*/
JASS://////////////////////////////////////////////////////// // //////////////////////////////////////////////// // //////////////////////////////////////////////// // // ////////////////////////////////////////////////////////
JASS:/*------------------------------------------------------ ------------------------------------------------------*/
There is no set way to do it, so the best way to write good documentation for code is to write what looks good.
A good thing to do with code is to list out all of the functions, variables, structs, modules, and etc at the top of the code (a simple list) and to place a descriptive list below it (a second list with maybe a paragraph of information on what each thing does and examples of how they are used).
Last edited: