Home
Comments - Do you know what to do with comments and Debug.Print statements
  v6.0 Posted at 2/08/2018 5:26 PM by Partho P. Das

​​When you create comments in your code, it is better to document why you've done something a certain way than to document how you did it. The code itself should tell the reader what is happening, there's no need to create "how" comments that merely restate the obvious unless you're using some technique that won't be apparent to most readers.

​What do you do with your print statements? Sometimes a programmer will place print statements at critical points in the program to print out debug statements for either bug hunting or testing. After the testing is successful, often the print statements are removed from the code. This is a bad thing to do. Debugging print statements are paths that show where the programmer has been. They should be commented out, but the statements should be left in the code in the form of comments. Thus, if the code breaks down later, the programmers (who might not remember or even know the program to start with), will be able to see where testing has been done and where the fault is likely to be - i.e., elsewhere.

private void Command0_Click() {
    rst.Open("SELECT * FROM Emp") // Open recordset with employee records
    // Exit sub if the count is greater than 1,000
    if (intCount > 1000) {
        return
    } else  {
    .....processing code
}

Bad Example​

private void Command0_Click() {
    rst.Open("SELECT * FROM Emp")
​    // Count will exceed 1,000 during eighteenth century
    // leap years, which we aren't prepared to handle.
    if (intCount > 1000) {
        return
    } else  {
    .....processing code
}

Good Example

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments:

    Note: Social Media login for Yotpo is not working in IE or Safari, please use Chrome. We are waiting for Yotpo to fix it.