What are your “hard rules” about commenting your code?

前端 未结 21 1838
野性不改
野性不改 2020-12-31 16:09

I have seen the other questions but I am still not satisfied with the way this subject is covered.

I would like to extract a distiled list of things

相关标签:
21条回答
  • 2020-12-31 16:29

    In my opinion, TODO/TBD/FIXME etc. are ok to have in code which is currently being worked on, but when you see code which hasn't been touched in 5 years and is full of them, you realize that it's a pretty lousy way of making sure that things get fixed. In short, TODO notes in comments tend to stay there. Better to use a bugtracker if you have things which need to be fixed at some point.

    Hudson (CI server) has a great plugin which scans for TODOs and notes how many there are in your code. You can even set thresholds causing the build to be classified as unstable if there are too many of them.

    My favorite rule-of-thumb regarding comments is: if the code and the comments disagree, then both are likely incorrect

    0 讨论(0)
  • 2020-12-31 16:30

    I create a comment block at the beginning of my code, listing the purpose of the program, the date it was created, any license/copyright info (like GPL), and the version history.

    I often comment my imports if it's not obvious why they are being imported, especially if the overall program doesn't appear to need the imports.

    I add a docstring to each class, method, or function, describing what the purpose of that block is and any additional information I think is necessary.

    I usually have a demarcation line for sections that are related, e.g. widget creation, variables, etc. Since I use SPE for my programming environment, it automatically highlights these sections, making navigation easier.

    I add TODO comments as reminders while I'm coding. It's a good way to remind myself to refactor the code once it's verified to work correctly.

    Finally, I comment individual lines that may need some clarification or otherwise need some metadata for myself in the future or other programmers.

    Personally, I hate looking at code and trying to figure out what it's supposed to do. If someone could just write a simple sentence to explain it, life is easier. Self-documenting code is a misnomer, in my book.

    0 讨论(0)
  • 2020-12-31 16:30

    A really important thing to check for when you are checking header documentation (or whatever you call the block preceding the method declaration) is that directives and caveats are easy to spot.

    Directives are any "do" or "don't do" instructions that affect the client: don't call from the UI thread, don't use in performance critical code, call X before Y, release return value after use, etc.

    Caveats are anything that could be a nasty surprise: remaining action items, known assumptions and limitations, etc.

    When you focus on a method that you are writing and inspecting, you'll see everything. When a programmer is using your method and thirty others in an hour, you can't count on a thorough read. I can send you research data on that if you're interested.

    0 讨论(0)
  • 2020-12-31 16:31

    When implementing an RFC or other protocol specification, comment state machines / event handlers / etc with the section of the spec they correspond to. Make sure to list the version or date of the spec, in case it is revised later.

    0 讨论(0)
  • 2020-12-31 16:33

    If a comment is out of date (does not match the code), delete it or update it. Never leave an inaccurate comment in place.

    0 讨论(0)
  • 2020-12-31 16:36

    The only guaranteed place I leave comments: TODO sections. The best place to keep track of things that need reworking is right there in the code.

    0 讨论(0)
提交回复
热议问题