Program commenting how far do you need to go?

Discussion in 'General Chat' started by amazingtrade, Dec 9, 2004.

  1. amazingtrade

    amazingtrade Mad Madchestoh fan

    Joined:
    Jun 19, 2003
    Messages:
    5,139
    Likes Received:
    0
    Location:
    Manchester
    This is somthing I am never usualy shure of, I have more than 1000 lines of object oriented C++ code to comment. Some people say different things but am I right in thinking that you should not explain the code but explain the process?

    For example is commenting for this bit of code sufficient?

    Code:
    void Ccacl3Dlg::OnBnClickedBtncosine()
    {
    	//cos is a function included in the math.h header.
    	m_dValue = cos (m_dDisplay*m_dPi/180);
    	m_dDisplay = m_dValue;
            //update the display with the new result.
    	UpdateData(FALSE);
    }
    
    I know there are a few programmers here so I am just interested in finding out how you would comment that in a professional situation.

    Thanks.
     
    amazingtrade, Dec 9, 2004
    #1
  2. amazingtrade

    penance Arrogant Cock

    Joined:
    Jun 30, 2003
    Messages:
    6,004
    Likes Received:
    2
    Location:
    Bristol - armpit of the west.
    It is very useful to comment fully. I would say your example is ok, for more complex math maybe you would need to explain the operations being performed.
    It is good for code that might be maintained by others, it also seems to gain brownie points in exams/assignments.
    Just dont get lazy and stop commenting all together
     
    penance, Dec 9, 2004
    #2
  3. amazingtrade

    sideshowbob Trisha

    Joined:
    Jun 20, 2003
    Messages:
    3,092
    Likes Received:
    0
    Location:
    London
    Real programmers don't comment, except to point out something like:

    // Exceptionally elegant piece of code coming up. Admire my l33t skillz

    or

    // TO DO: fix bug causing all data to be corrupted when user hits the Cancel button

    -- Ian
     
    sideshowbob, Dec 9, 2004
    #3
  4. amazingtrade

    penance Arrogant Cock

    Joined:
    Jun 30, 2003
    Messages:
    6,004
    Likes Received:
    2
    Location:
    Bristol - armpit of the west.
    also name all variables after friends

    int Bob;
    float Bill;
    long Fred;
     
    penance, Dec 9, 2004
    #4
  5. amazingtrade

    robert_cyrus

    Joined:
    Jun 20, 2003
    Messages:
    685
    Likes Received:
    0
    Location:
    near the sea
    i include comments to punctuate the code.
    "now work out [this]"
    "now take [that] and calculate [something else]"
    and more specific comments when code (read "logic") isn't obvious.
     
    robert_cyrus, Dec 9, 2004
    #5
  6. amazingtrade

    sideshowbob Trisha

    Joined:
    Jun 20, 2003
    Messages:
    3,092
    Likes Received:
    0
    Location:
    London
    About a million years ago I used to work on a relational database OS called Pick. Pick BASIC offered the fantastic yet useless ability to redefine the meaning of some of the language elements. So you could put something like this in an include file and really confuse the hell out of people:

    define true = false
    define false = true

    -- Ian
     
    sideshowbob, Dec 9, 2004
    #6
  7. amazingtrade

    amazingtrade Mad Madchestoh fan

    Joined:
    Jun 19, 2003
    Messages:
    5,139
    Likes Received:
    0
    Location:
    Manchester
    I have explained all my variables some times in detail, the problem is what is obvious to me may not be to other people. I have gone into detail the first time a type of function comes up, for example adding a the second byte of a number, but the button functions are repeated for each decimal number (0-9) so I haven't really commented the rest of these because it gets too repeative. The only difference in code is its passing a different number into the functions according to what button was pressed.

    My biggest downfall on programming is I never comment my code, I have written entire websites and content management systems without a single line of commeting, this is because I know the only person using it is myself and its not for a client, coursework or any other professional purpose. The only probem is when I need to edit the code 6 months later it takes me an hour to work out how the hell my code works :p
     
    amazingtrade, Dec 9, 2004
    #7
  8. amazingtrade

    I-S Good Evening.... Infidel

    Joined:
    Jun 25, 2003
    Messages:
    4,842
    Likes Received:
    1
    Location:
    In a world of pain
    I don't tend to comment my code...

    However, my code usually looks something like this, and comments aren't really necessary (when you understand the language):

    Code:
    MAIN:
         BTFSC 0,5
         GOTO UP1
         BTFSC 0,4
         GOTO UP2
         GOTO MAIN
    And yes, use of goto is acceptable.
     
    I-S, Dec 9, 2004
    #8
  9. amazingtrade

    Dick Bowman

    Joined:
    May 24, 2004
    Messages:
    194
    Likes Received:
    0
    I've never come across a compiler or interpreter which uses comments. The human reader has no guarantee that comments in program source code are in any way accurate. Programmers would better use the time they spend writing asinine comments ("add A to B") by writing clear code.
     
    Dick Bowman, Dec 9, 2004
    #9
  10. amazingtrade

    amazingtrade Mad Madchestoh fan

    Joined:
    Jun 19, 2003
    Messages:
    5,139
    Likes Received:
    0
    Location:
    Manchester
    Argh goto's a lovely way to emulate the M6 in Birmingham. In fact I think the last time I used goto was QBASIC in around 1998 when I was 16.

    Another really wonderful thing I have seen people to do, instead of putting repeative elements in a function some people seem to think its clever to use cut and paste - until of course they disover a :JPS: in that part of the code and they have to repeat the whole thing over again :) My main aim when writing code is to keep at as short as possible, commenting and legability is usualy the last thing on my mind :rolleyes:

    I bet the windows source code is not well commented for good reason :D
     
    amazingtrade, Dec 9, 2004
    #10
  11. amazingtrade

    Paul Ranson

    Joined:
    Sep 4, 2003
    Messages:
    1,602
    Likes Received:
    0
    Location:
    An octopus's garden.
    Both comments in your example are restatements of the obvious and are therefore redundant. Question is what does the function do and why?

    And I think you may have a bug if you don't call 'UpdateData ( TRUE )' before using m_dDisplay, unless it's not related to user entry.

    m_dDisplay also appears to start off as a value in degrees and end up as a cos, which is obviously between -1 and +1. This could use a comment. What happens when you press the button twice? How can that make sense?

    Paul
     
    Paul Ranson, Dec 9, 2004
    #11
  12. amazingtrade

    amazingtrade Mad Madchestoh fan

    Joined:
    Jun 19, 2003
    Messages:
    5,139
    Likes Received:
    0
    Location:
    Manchester
    It is commented I just missed that off by accident when I copied into here, when you press the button is just works out the cosine of what ever the last value was etc. I will add extra comments about the cosine.

    The number is already entered and UpdateData(TRUE) is called in another part of the program before this function is called so it works fine. I admit its not the neatest calcualtor program in the world, the program is I was experimenting a lot when producing it. I think the quiality of code is worth around 20% and the 80% for what the actual calcualtor does and how well it works.

    I have used the STRCAT and STRCPY methods to add stuff to the display string thouh which apparantly can cause memory leaks, this is the way my tutor told us all to it though so he can't mark us down for that.

    A lot of people haven't even managed to get a series of numbers (i.e 4+2+6) working so I should gain extra marks there. The only major flaw of my calculator is it dosn't do presidence but that would have only added an extra 10% to my marks and the amount of work it involved meant it was not worth it, I would be better off doing that amount of work on another assignment.

    I am not a programmer and never claim to be, I am just better at programming than a lot of people on my course.
     
    amazingtrade, Dec 9, 2004
    #12
  13. amazingtrade

    Paul Ranson

    Joined:
    Sep 4, 2003
    Messages:
    1,602
    Likes Received:
    0
    Location:
    An octopus's garden.
    Unless you have a 'coding standard' to work to then I think you should just comment the unusual and not the usual. So telling me where 'cos' comes from isn't useful, and I should already know what 'cos' is. In the context of the whole program the transference from the display to the m_dDisplay variable will be clear and so it doesn't need to be commented everywhere.

    So the only comment I think this function needs is to explain m_dValue, unless this is part of another pattern and explained elsewhere.

    Does your calculator handle rounding errors neatly? I remember the Windows calculator for a long time had trouble with '3.11-0.1' or somesuch, a tenth not being exactly representable in binary.

    Paul
     
    Paul Ranson, Dec 9, 2004
    #13
  14. amazingtrade

    amazingtrade Mad Madchestoh fan

    Joined:
    Jun 19, 2003
    Messages:
    5,139
    Likes Received:
    0
    Location:
    Manchester
    Thanks, I think thats pretty much what I have been doing, I have expalined what m_value does in detail else where. I am not sure what you mean exactly by rounding errors, 3.11-0.1 gives me 3.01 which is correct.

    I haven't added any code this so I am not aware of any issues. I have fixed most the bugs I have been aware off but this has kind of made a lot of my code quite messy with many nested if statements. I am sure there must be some bugs though, the main one I know of is that the currency conversion tools do not round the value to two decimal points but this is because the floor function produced some odd results so I just left it out.
     
    amazingtrade, Dec 9, 2004
    #14
  15. amazingtrade

    Sgt Rock

    Joined:
    Jun 19, 2003
    Messages:
    873
    Likes Received:
    0
    Write code in Cobol, no need to comment :D
     
    Sgt Rock, Dec 9, 2004
    #15
  16. amazingtrade

    michaelab desafinado

    Joined:
    Jun 19, 2003
    Messages:
    6,403
    Likes Received:
    1
    Location:
    Lisbon, Portugal
    :lol:
     
    michaelab, Dec 9, 2004
    #16
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
Loading...