Comment to Code Ratio is one of the source-code metrics Understand computes across 17+ languages — browse them in the GUI, export and track them, or script them with the Python API.
Comment to Code Ratio
API ID: RatioCommentToCodeLanguages: Ada, Assembly, Basic, C#, C++, Fortran, Java, Jovial, Pascal, Python, Rust, VHDL, Web
Targets: Architectures, Classes, Files, Functions, Modules, Packages, Project, Subprograms
Ratio of comment lines to code lines.
Calculated as Comment Lines / Code Lines. Note that because some lines are both code and comment, this could easily yield a ratio higher than 1.
For example:
void SayHello::printHello() {
switch (i) {
case 0:
cout << "Hello World" << endl;
case 1:
cout << "HELLO WORLD!" << endl;
default: // a comment here
for (int m = 0; m < j; m++);
cout << "hello world" << endl;
}
#ifdef A_VERY_NICE_VARIABLE
cout << "Inactive Line" << endl; // Inactive
#endif
}
printHello() has 1 comment line (default: // a comment here) and 11 code lines, for a Comment to Code Ratio of 1 / 11 = 0.09.
- Ada: Project, File, Type, Entry, Function, Package, Procedure, Protected, Task
- Basic: Project, File, Method, Module, Class
- C#: Project, File, Class, Method
- C++: Project, File, Class, Struct, Union, Function
- Fortran: Project, File, Block Data, Function, Program, Subroutine
- Java: Project, File, Class, Interface, Method
- Jovial: Project, File, Module, Subroutine
- Pascal: Project, File, Class, Interface, Compunit, Function, Procedure
- Python: Project, File, Class, Function
- Rust: Project, File, Function
- VHDL: Project, File, Procedure, Function, Architecture
- Web: Project, File
- This metric can be disabled from "Project Configuration/Metrics/Lines and Statements" with the "Show line count metrics" option.
- For projects and architectures, this metric is enabled from "Project Configuration/Metrics/Projects and Architectures" with the "Show default summary metrics" option.