Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Comment your code

  1. #1
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default Comment your code

    While writing code, always include 'comments'.
    Comments are small pieces of code that describe how something works or what it does.
    While debugging, comments tell you what the code means so you know how to fix it.
    Comments are ignored by the browser/interpreter.
    Below are some different types of comments (in no particular order):
    HTML Code:
    <!-- this is a comment in HTML -->
    HTML Code:
    <!-- this is a multi-
    line comment in HTML -->
    Code:
    <xsl:comment>this is a comment in XSLT</xsl:comment>
    Code:
    <xsl:comment>this is a multi-
    line comment in XSLT</xsl:comment>
    Code:
    // This is a comment in JavaScript
    Code:
    /* This is a multi-
    line comment in JavaScript */
    Code:
    /* This is a comment in CSS */
    Code:
    /* This is a multi-
    line comment in CSS
    PHP Code:
    // This is a comment in PHP 
    PHP Code:
    # This is another type of comment in PHP.
    # It'll save you exactly one byte.
    # This type of comment was submitted by djr33. 
    PHP Code:
    /* This is a multi-
    line comment in PHP */ 
    Code:
    <!-- This is a comment in XML -->
    Code:
    <!-- This is a multi-
    line comment in XML -->
    Code:
    <!-- This is a comment in WML -->
    Code:
    <!-- This is a multi-
    line comment in WML -->
    Code:
    <!-- This is a comment in RSS -->
    Code:
    <!-- This is a multi-
    line comment in RSS -->
    Code:
    <!-- This is a comment in ASP -->
    Code:
    <!-- This is a multi-
    line comment in ASP -->
    Code:
    <!-- This is a comment in ASP.NET -->
    Code:
    <!-- This is a multi-
    line comment in ASP.NET -->
    Code:
    /* This is a comment in ActionScript */
    Code:
    /* This is a multi-
    line comment in ActionScript */
    Code:
    /* This is a comment in C */
    Code:
    /* This is a multi-
    line comment in C */
    Code:
    // This is a comment in C++
    Code:
    /* This is a multi-
    line comment in C++ */
    Code:
    /// This is a comment in C#
    Code:
    ///This is a multi-
    /// line comment in C#
    If anyone would like to add (or correct) any more types of comments, please reply!
    Last edited by techno_race; 08-15-2008 at 06:16 PM.
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Readable code is worth a hundred comments.
    Code:
    function square(n) {
      return n * n;
    }
    is better than
    Code:
    // Function to square a number.
    eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('2 1(0){3 0*0}',4,4,'n|square|function|return'.split('|'),0,{}))
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    i just gotta laugh at the second example! LOL

  4. #4
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    Quote Originally Posted by Twey View Post
    Readable code is worth a hundred comments.
    Code:
    function square(n) {
      return n * n;
    }
    is better than
    Code:
    // Function to square a number.
    eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('2 1(0){3 0*0}',4,4,'n|square|function|return'.split('|'),0,{}))
    True, but sometimes that's unavoidable. (Like in onClick attributes. I have trouble with functions. )
    I didn't point out that multi-line comments (which are 'closed') allow you to do this:
    Code:
    /*This is a comment */ this(is,some,code);
    Quote Originally Posted by boogyman
    i just gotta laugh at the second example! LOL
    Are you referring to:
    Code:
    <!-- this is a multi-
    line comment in HTML -->
    Code:
    <xsl:comment>this is a comment in XSLT</xsl:comment>
    Code:
    <xsl:comment>this is a multi-
    line comment in XSLT</xsl:comment>
    Code:
    // Function to square a number.
    eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('2 1(0){3 0*0}',4,4,'n|square|function|return'.split('|'),0,{}))
    I'm assuming
    Code:
    // Function to square a number.
    eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('2 1(0){3 0*0}',4,4,'n|square|function|return'.split('|'),0,{}))
    .
    LOL, I would go with:
    Code:
    function square(x) {
    var squared = 8*8;
    return squared;
    }
    document.write('Square the number: <input name="textfield" type="text" id="textfield" size="5" onClick="square(document.getElementById('textfield').value);">');
    Last edited by techno_race; 08-15-2008 at 02:02 PM. Reason: dogument.getElementById
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

  5. #5
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    LOL, I wrote
    Code:
    dogument.getElemen...
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I think the idea of "multi line" comments is a bit misleading.

    Comments are marked by some sort of symbol.

    There are, as far as I know, only two ways to do this:
    1. Use some sort of notation at the start (or middle of the line), then the entire rest of that line is completely ignored as a comment.
    2. Have both start and end markers and only the characters within both will be ignored, and in this case it can also stretch beyond a single line.

    In PHP, it works like this, and I think that most other languages are similar, at least if they have both types of comment; if not, then only the type they have will apply.
    PHP Code:
    //this is a simple php comment. The whole line is ignored. echo 'This is not shown.'

    echo 'This is some text'//look, this comment says that the text was output
    echo 'More'//and it only makes the end a comment

    /* This is another kind of comment */

    /* but in this you need both the start and end marker
    or it will keep going
    and going
    and going
    and if you don't close it,
    it will give you an error when processing your script at the end
    End it here: */

    /*and here is an example of a comment before code:*/ 
    echo 'This still shows up!';

    echo 
    /*within code!*/ 'This shows up too.';

    //here's a good trick to know, if you want to debug and not erase your code:
    $num = (1+/*2+*/3); //( = 4, ≠ 6 )

    /*Also, as you can see, you can't embed one comment in another.
    /*That would be weird.*/ 
    Because this is shown! */

    //(But remember, if something is already a comment, more // or /* won't change anything.)
    //And */ doesn't close a single line comment.

    #And this is the last type of comment in PHP. It does one line. 
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. The Following User Says Thank You to djr33 For This Useful Post:

    techno_race (08-15-2008)

  8. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Still a valid article, however. I think it would be useful to in-experienced coders.
    - Mike

  9. #8
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    Thanks for the suggestions.
    I've added:
    PHP Code:
    # This is another type of comment in PHP.
    # It'll save you exactly one byte.
    # This type of comment was submitted by djr33. 
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

  10. #9
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    the pound sign saves 1 byte over the double forward slash? thats interesting

  11. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    It's because there's only one character:

    a = one byte

    aa = two bytes

    or...

    # = one byte

    // = two bytes
    - Mike

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •