Results 1 to 26 of 26

Thread: Comments please

  1. #1

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    Comments please

    I have to write a lot of methods, that are going to be used by people after I have moved.
    What I want is a consistent style particularly for commenting and layout.
    Would anybody care to read thro' this example and comment on my current 'style'.
    Any comments would be greatly appreciated.
    The person taking over from me may not have much experience so do you think this example would be easy to understand and use.
    Any comments on the actual working of the Sub would also be appreciated.
    VB Code:
    1. '---------------------------------------------------------------------------------------
    2. ' Procedure      : FillFlexGrid
    3. ' DateTime       : 26/03/02 08:40
    4. ' Author         : Glen [email][email protected][/email]
    5. ' Purpose        : Fills MSFlexGrid with values in ADODB.Recordset
    6. '                  If NewGrid is set to false inRes values are added
    7. '                  to existing rows
    8. '                  Else old rows are deleted
    9. ' Pre-Conditions : inGrid must have 1 or 0 FixedColumns
    10. '                  inGrid must have 1 or 0 FixedRows
    11. '---------------------------------------------------------------------------------------
    12. '
    13. Public Sub FillFlexGrid(ByRef inGrid As MSFlexGrid, _
    14.                         ByVal inRes As ADODB.Recordset, _
    15.                Optional ByVal NewGrid As Boolean = True)
    16.    
    17.     'If a new grid remove old rows
    18.     'If NewGrid
    19.     If (NewGrid) Then
    20.         'If inGrid has a fixed row
    21.         If (inGrid.FixedRows = 1) Then
    22.             'Leave FixedRow
    23.             inGrid.Rows = 1
    24.         Else
    25.             'Clear all rows
    26.             inGrid.Rows = 0
    27.         End If ' (inGrid.FixedRows = 1) Then
    28.     End If ' (NewGrid)
    29.    
    30.     'Make inGrid columns = number of fields in inRes
    31.     inGrid.Cols = inRes.Fields.Count
    32.    
    33.     Dim thisRow As String   'To create row to add to grid
    34.     Dim i As Integer        'For loop count
    35.    
    36.     'If there are records
    37.     If (Not (inRes.BOF And inRes.EOF)) Then
    38.         'Go to the first one
    39.         inRes.MoveFirst
    40.         'While not at the last one
    41.         While (Not inRes.EOF)
    42.             'If inGrid has a fixed column
    43.             If (inGrid.FixedCols = 1) Then
    44.                 'Make 1st value TAB
    45.                 thisRow = vbTab
    46.             End If ' (inGrid.FixedCols = 1) Then
    47.             'While fields left
    48.             For i = 0 To inRes.Fields.Count - 1
    49.                 'Add field value to thisRow & TAB to move to next Column
    50.                 thisRow = thisRow & inRes.Fields(i).Value & vbTab
    51.             'Next field
    52.             Next ' For i = 0 To inRes.Fields.Count - 1
    53.             'Add thisRow to inGrid
    54.             inGrid.AddItem thisRow
    55.             'Reset
    56.             i = 0
    57.             'Reset
    58.             thisRow = ""
    59.             'Next record
    60.             inRes.MoveNext
    61.         Wend ' (Not inRes.EOF)
    62.     End If ' (Not (inRes.BOF And inRes.EOF)) Then
    63.    
    64. End Sub ' FillFlexGrid

  2. #2
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Regarding the comment lines only:

    I personally like minimum comments to explain confusing or not so obvious parts of code. Reading your code, a lot of the comments aren't necessary.

    But I also know that some companies want you to comment every single line. So it probably isn't a bad habit. When I copy code, I usually delete most of the comments from code where I understand what's going on. Otherwise I leave them.

  3. #3

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    Originally posted by cafeenman
    Regarding the comment lines only:

    I personally like minimum comments to explain confusing or not so obvious parts of code. Reading your code, a lot of the comments aren't necessary.

    But I also know that some companies want you to comment every single line. So it probably isn't a bad habit. When I copy code, I usually delete most of the comments from code where I understand what's going on. Otherwise I leave them.
    I agree if I'm the only person using the code.
    But whoever takes over my projects may not have as much VB experience as I have, still I may be over doing it.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by GlenW

    I agree if I'm the only person using the code.
    But whoever takes over my projects may not have as much VB experience as I have, still I may be over doing it.

    Yes, it would be easier if you had something like this

    'now we're going to do this and this and this
    related
    lines
    of
    code


    'next comment
    more
    lines
    of
    code


    get my drift? of course your style would be for the beginners as you mentioned. You have to consider this: will the person who's coming next, be an expert or a novice? Act appropriately

  5. #5

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    Originally posted by mendhak

    You have to consider this: will the person who's coming next, be an expert or a novice? Act appropriately
    Exactly my problem I don't know, guess the best thing is to assume he's a novice and just annoy him if he isn't.

    Any comments on the actual working of the Sub.

  6. #6
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    When you have that many comments, you may also want to consider moving some more of them to the end of the code line so the code flow is a little smoother to read.

    Anyway, it's not bad. Just not my preference. Like the man said, "Act accordingly."

  7. #7
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    as said i think some of the more obvious lines don't need comments, but something i would add is at the start of the procedure add a few lines of comments explaining what each parameter in the procedure declaration does, and how it is used etc...
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  8. #8

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    Keep them coming guys.
    This is exactly what I need.

  9. #9
    Member
    Join Date
    Mar 2002
    Posts
    50
    This is the first time that me being a novice in VB comes in handy; I'm the one you want judging your comments.

    Yes, there are a lot of them and some trivial, even for me. But there's no harm in leaving them in. Allthough your example sub wasn't too hard to understand your comments helped me read through the code a lot faster.

    I think that this commenting will get you there. But do keep in mind that if the next in line is a total newbie your comments may even have to clearer; as in the earlier posted

    'now we're going to do this and this and this
    related
    lines
    of
    code


    'next comment
    more
    lines
    of
    code
    Life is as short as you make it

  10. #10
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    A rule of thumb: The comments are sufficient if you can understand what a routine does and how when the code is removed.

    VB Code:
    1. '---------------------------------------------------------------------------------------
    2. ' Procedure      : FillFlexGrid
    3. ' DateTime       : 26/03/02 08:40
    4. ' Author         : Glen [email][email protected][/email]
    5. ' Purpose        : Fills MSFlexGrid with values in ADODB.Recordset
    6. '                  If NewGrid is set to false inRes values are added
    7. '                  to existing rows
    8. '                  Else old rows are deleted
    9. ' Pre-Conditions : inGrid must have 1 or 0 FixedColumns
    10. '                  inGrid must have 1 or 0 FixedRows
    11. '----------------------------------------------------------------------------------    
    12.     'If a new grid remove old rows
    13.     'If NewGrid
    14.         'If inGrid has a fixed row
    15.             'Leave FixedRow
    16.             'Clear all rows
    17.         ' (inGrid.FixedRows = 1) Then
    18.      ' (NewGrid)
    19.    
    20.     'Make inGrid columns = number of fields in inRes
    21.    
    22.     'To create row to add to grid
    23.     'For loop count
    24.    
    25.     'If there are records
    26.         'Go to the first one
    27.         'While not at the last one
    28.             'If inGrid has a fixed column
    29.                 'Make 1st value TAB
    30.             ' (inGrid.FixedCols = 1) Then
    31.             'While fields left
    32.                 'Add field value to thisRow & TAB to move to next Column
    33.                 'Next field
    34.               ' For i = 0 To inRes.Fields.Count - 1
    35.             'Add thisRow to inGrid
    36.             'Reset
    37.             'Reset
    38.             'Next record
    39.     ' (Not inRes.EOF)
    40.  ' (Not (inRes.BOF And inRes.EOF)) Then
    41.    
    42. ' FillFlexGrid
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  11. #11

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    What about commenting Wend, Next and EndIf statements.
    I do it all the time 'cos i think it can help if you have use deeply nested constructions.

    This is really helping me.

  12. #12

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    Originally posted by MerrionComputin
    [B]A rule of thumb: The comments are sufficient if you can understand what a routine does and how when the code is removed.
    What a great idea!
    Wish I'd thought of it.

  13. #13
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    its always a good idea to comment your errorhandlers for errors that you have setup traps for too
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  14. #14
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313
    Good thread.
    I'm an obsessive commenter, and agree with MerrionComputin - I sometimes comment before I start coding - helps me rationalise what I'm going to do, and (almost) makes up for the complete lack of design...

    One thing that no-one has mentioned yet is Change History. Depending on your source code management system, it is sometimes useful to have a Change History section in your function header. Just lets you know why and when changes were made (and who made them!).

  15. #15

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    Originally posted by PilgrimPete
    One thing that no-one has mentioned yet is Change History. Depending on your source code management system, it is sometimes useful to have a Change History section in your function header. Just lets you know why and when changes were made (and who made them!).
    Good idea!
    What format would you suggest?

  16. #16
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313
    It's all down to taste really I guess, but something like this should do it...

    VB Code:
    1. 'CHANGE HISTORY
    2. '**********************************************************************************
    3. 'Date       | Changed By     | Bug ref       | Comments
    4. '**********************************************************************************
    5. '27/03/2002 | Pilgrim Pete   | 1234          | Added handler for RTE 7890

  17. #17
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    oh i get yer. so when you make a change instead of deleting the line etc just comment it out and surround it in coments or seomthing, like this:

    VB Code:
    1. 'ChangeDate: 27/Mar/2002 by Darrel Anthony
    2. 'Changed for this reason: dfhsdjkfhdsk
    3. '*********************
    4. 'MsgBox "Original"
    5. MsgBox "New Code"
    6. '*********************
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  18. #18
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    ooh, pilgrim pete's is much better
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  19. #19
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819
    As a personal prefernece, I always use this for comments:
    VB Code:
    1. '# This is a comment
    I find the # helps comments to stand out from lines of code that have been commented out...
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  20. #20

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    Originally posted by darre1
    oh i get yer. so when you make a change instead of deleting the line etc just comment it out and surround it in coments or seomthing, like this:

    VB Code:
    1. 'ChangeDate: 27/Mar/2002 by Darrel Anthony
    2. 'Changed for this reason: dfhsdjkfhdsk
    3. '*********************
    4. 'MsgBox "Original"
    5. MsgBox "New Code"
    6. '*********************
    I think the idea is to list changes it in the header, so when someone can't get a function to work 'cos its been changed the explanation of the change is easy to understand.
    Right?

  21. #21
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313
    Darre1: not really what I meant, though it's useful if you have no config management tool, it soon gets really cluttered with old code (I've seen plenty of functions with more commented out code than active code )
    What I meant was to put it in the function header like this:
    VB Code:
    1. '---------------------------------------------------------------------------------------
    2. ' Procedure      : FillFlexGrid
    3. ' DateTime       : 26/03/02 08:40
    4. ' Author         : Glen [email][email protected][/email]
    5. ' Purpose        : Fills MSFlexGrid with values in ADODB.Recordset
    6. '                  If NewGrid is set to false inRes values are added
    7. '                  to existing rows
    8. '                  Else old rows are deleted
    9. ' Pre-Conditions : inGrid must have 1 or 0 FixedColumns
    10. '                  inGrid must have 1 or 0 FixedRows
    11. '----------------------------------------------------------------------------------
    12. 'CHANGE HISTORY
    13. 'Date       | Changed By     | Bug ref       | Comments
    14. ''----------------------------------------------------------------------------------
    15. '27/03/2002 | Pilgrim Pete   | 1234          | Added handler for RTE 7890
    16. '----------------------------------------------------------------------------------

  22. #22
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    semi...

    Here's a semi related question...

    When's the best time to actually do the properly formatted commenting?

    while working, or right towards the end, when you're cleaning up your code?

  23. #23
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    When's the best time to actually do the properly formatted commenting?
    Try writing the comments before you write the code...it works.
    Like, suppose you want to want to set a new default printer by the name passed in....
    VB Code:
    1. Public Function SetDefaultPrinter(ByVal DeviceName As String) As Boolean
    2.  
    3.     '\\ Iterate through the installed printers
    4.         '\\ If it is the desired printer
    5.             '\\ set it to be the default printer
    6.             '\\ indicate that the printer was found
    7.             '\\ and stop searching
    8.          '\\ Otherwise
    9.      '\\ Next printer in list
    10.          
    11.  
    12. End Function


    See also Seven Secrets of Successful Programmers
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  24. #24
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313
    I recommend commenting as you go, because there's little chance you'll get round to, or be bothered to comment it when it's just about to go out the door...
    Also if your memory is as rubbish as mine, you need aide memoires to remind you exactly why you did implement it that way...

    Same problem occurs with documenting your design...

  25. #25
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    We're supposed to document our design? Man, I can't even keep up with the help files, etc. My biggest app has almost 50 rtf files for the help project along with two user manuals, etc. I have to comment as I go along or I'm doomed. But forget about documenting the app. When I die, it dies with me.

  26. #26
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    it's a well know fact that programmers hate documenting code anyway
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

Posting Permissions

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



Click Here to Expand Forum to Full Width