Thread: I suck at commenting on my code, any ideas how?

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    1

    I suck at commenting on my code, any ideas how?

    [C] //C program to accept N numbers and arrange them in an ascending order #include - Pastebin.com

    This is my code but I have to comment on the code, but I get a tad confused when commenting on code. Could anyone help me

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    If you understand the code that you wrote, you should have no trouble commenting on it. Explain what it does and how it works, and why you do what you do.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Here's an old post where commenting is discussed: how to properly comment on c code

    Prelude gives some good perspective in post #11.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I'd probably only add a single comment to that program, just before the nested loops saying what kind of ssss it is

    If I changed that part of the code to a function I wouldn't add any comments. It's all pretty self explanatory.


    Edit: Maybe I'd add a comment the the ssss is not optimized

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Yeah, commenting code is so hard when you didn't write it yourself.
    C Program to Sort the Array in an Ascending Order - Sanfoundry
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Salem View Post
    Yeah, commenting code is so hard when you didn't write it yourself.
    C Program to Sort the Array in an Ascending Order - Sanfoundry
    It's not impossible that they wrote the exact same program but, yes, it's fairly unlikely. I stand by my comment before though... why would it even need commenting? I wouldn't comment that apart from saying what type of sort it is and that the implementation of the sort is not optimised.

    Edit: Maybe I'd add another comment explaining what lines 32 and 33 are for because they don't make a whole heap of sense to me.
    Last edited by Hodor; 12-12-2015 at 01:25 AM.

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I've been nice and commented it

    Code:
    //C program to accept N numbers and arrange them in an ascending order
    #include <stdio.h>
    #include <Windows.h>    // NFI.. looks cool
    #include <stdlib.h>        // NFI.. looks cool
    void main() // I don't like it
    {
        int min = 0;        // NFI.. looks cool
        int max = 0;        // NFI.. looks cool
        int i, j, a, n, number[30];
        printf("Enter the value of N \n");
        scanf("%d", &n);
        printf("Enter the numbers \n");
        for (i = 0; i < n; ++i)            // Buffer overflow warning
            scanf("%d", &number[i]);    // Cool
        for (i = 0; i < n; ++i)            // Outer loop; Out of bounds warning
        {
            for (j = i + 1; j < n; ++j) // Inner loop; Out of bounds warning
            {
                if (number[i] > number[j]) // Condition
                {
                    a = number[i];
                                            // Blank line for readability
                    number[i] = number[j];
                                            // Blank line for readability
                    number[j] = a;
                }
            }
        }
        printf("The numbers arranged in ascending order are given below \n");
        for (i = 0; i < n; ++i)    // Out of bounds warning
            printf("%d\n", number[i]);
        number[0] = min;    // NFI
        number[a - 1] = max;    // NFI... not even properly defined in all cases; Out of bounds warning
        system("pause");    // NFI WHY
    } // Bad style (although not incorrect for the main function)

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It's rather simple. Every time you write a nontrivial block of code, you should ask yourself "Would I remember what this does if I left it for a month or more?". If the answer is no, you should definitely add comments. If the answer is mostly, then brief informative comments might be better.

    Just remember that commenting isn't everything. IMO, code formatting/spacing and meaningful names play a more important role at helping others understand your program.
    Devoted my life to programming...

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by GReaper View Post
    It's rather simple. Every time you write a nontrivial block of code, you should ask yourself "Would I remember what this does if I left it for a month or more?". If the answer is no, you should definitely add comments. If the answer is mostly, then brief informative comments might be better.
    That's kind of my point, GReaper. Why would I add comments to that program (apart from the nonsense after the sorted array is printed)? If I saw that written in a language I don't normally program in, and even one I don't particularly know, (and do that more than I like) I'd immediately understand what was going on even if the syntax was not familiar, because in this example it's all trivial. So in this example I would personally add one comment at the most and delete the weird lines after the sorted array is printed (unless they are actually doing something other than being weird, in which case I'd add a comment about them).

    Edit: It seems to me, though, that the OP really wants: "please convert this to pseudocode for me". But even then it's just another language, per se, and adding comments to the pseudocode would not provide a benefit. It'd get down to a <-- 2 * 4; // assign the result of the mathematical expression 2 * 4 to the variable a where a is a variable of type integer and both 2 and 4 are integer constants.
    Last edited by Hodor; 12-12-2015 at 05:29 AM.

  10. #10
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Anyway, I fixed it. Edit: I guess it just needs comments now

    Code:
    #include <stdio.h>
    
    #define MAX_N 30
    
    void sort(int *a, int n);
    
    int main(void)
    {
        int i, n;
        int number[MAX_N];
        
        printf("Enter the value of N \n");
        scanf("%d", &n);
        
        if (n > MAX_N)
            n = MAX_N;
            
        printf("Enter the numbers \n");
        for (i = 0; i < n; ++i)
            scanf("%d", &number[i]);
            
        sort(number, n);
        
        for (i = 0; i < n; i++)
            printf("%d\n", number[i]);
        
        return 0;
    }
        
    
    void sort(int *a, int n)
    {
        if (n < 2)
            return;
        
        if (n == 2) {
            if (a[0] > a[1]) {
                int t = a[0];
                a[0] = a[1];
                a[1] = t;
            }
        } else {
            int i;
            for (i = 0; i < n; i++) {
                sort(a + 1, n - 1);
                sort(a, 2);
            }
        }
    }
    Last edited by Hodor; 12-12-2015 at 06:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need ideas on how to improve my code
    By NumLock in forum C Programming
    Replies: 5
    Last Post: 08-10-2011, 01:53 PM
  2. C++ commenting and C#'s
    By Masterx in forum C++ Programming
    Replies: 8
    Last Post: 07-04-2010, 09:37 PM
  3. Guidelines for commenting code in C
    By cs32 in forum C Programming
    Replies: 7
    Last Post: 02-18-2008, 03:44 AM
  4. commenting
    By pseudocode in forum C Programming
    Replies: 2
    Last Post: 01-26-2003, 08:40 AM
  5. the definitive guide to code commenting
    By Aran in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2002, 01:43 PM