What's the golden code/comment ratio? [closed]

2020-02-26 03:40发布

Is there a code/comment ratio that you consider to be a sign of good (bad) code health?

Can you give examples of open source projects that are considered to be well coded and their respective comment ratio?

(I realize that no ratio is "true" for every project and there may very well be crappy projects that exhibit this theoretical golden ratio. Still...)

26条回答
Summer. ? 凉城
2楼-- · 2020-02-26 04:05
LOC / LOcomment = yearsofexp / 5
查看更多
姐就是有狂的资本
3楼-- · 2020-02-26 04:05

The golden code/comment ratio is the intersection of

  • comments needed to remind yourself of things you had in mind while coding
  • comments needed to remind others of things you had in mind while coding
  • comments your customer is willing to pay for (because good comments cost time)
  • the developer's interest in producing obfuscated code because of job safety (if he or she is working for a company where a developer can get away with it)

This also shows why that ratio is different for every project and every team.

查看更多
4楼-- · 2020-02-26 04:05

If you want some real world data on comment ratios for different languages, take a look at Ohloh.

As an example, you can look at the various metrics for the Linux Kernel source.

查看更多
倾城 Initia
5楼-- · 2020-02-26 04:06

Comments should be very rare and valuable, almost always expressing the "why" and never the "how" (the exception being when the how is complex and not easily discernible from the code).

Every comment is a hint that you may need to refactor to make the code's intent clearer. Every comment risks becoming out of date as soon as it's written.

We have almost no comments aside from XML comments in our xUnit.net project, but some people seem to find the code clear and easy to read. :)

查看更多
贼婆χ
6楼-- · 2020-02-26 04:06

I think everyone can agree that 0 comments can generally be considered sub-documented code. Remember that even the most self documenting code can only ever document what's there; never what was intentionally left out, optimized away, tried and discarded, etc. You'll always have some need for English, basically, in your source files, or you're bound to leave out important caveats and design decisions.

I'm interested in how these sound principals you guys are advocating (with which I'm in full agreement so far) translate into code statistics. In particular, what open source projects are considered to be well (not over-) documented, and what's the comment ratio there.

查看更多
beautiful°
7楼-- · 2020-02-26 04:07

There is no "golden ratio". It depends on the language and the way that you write it. The more expressive your code, the more it can be self-documenting -- and therefore the fewer comments you need. That's one advantage of fluent interfaces.

查看更多
登录 后发表回答