New Lines 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.
New Lines
API ID: CountLineNewLanguages: Ada, Assembly, Basic, C#, C++, Fortran, Java, Jovial, Pascal, Python, Rust, VHDL, Web
Targets: Classes, Files, Functions, Modules, Packages, Subprograms
Number of new lines, compared to the comparison database.
Understand does a textual diff of the entity's contents and processes each
changed region, meaning a run of adjacent added and deleted lines bounded by
unchanged lines. For each changed region, if lines added is greater than lines deleted, then added - deleted is added to the new line count. So, insertions (deleted=0) are new lines, and also added lines in a changed region that exceed the number of lines deleted.
Note that because this is specific to the entity's contents, things like reordering every function in a file while leaving function contents unchanged means that each individual function will be unchanged, even though the file changed. Also, an entity's lines are counted the same way they are for the Lines metric, so a C++ Class considers all the defined method bodies even when the definition is outside the class definition.
For example, diffing a file's current version against the comparison database's version:
--- sample.cpp (comparison database)
+++ sample.cpp (current)
@@ -1,15 +1,16 @@
#include <iostream>
-// Utility functions
int square(int x) {
- return x * x;
+ return x * x * x;
}
-int unused() {
- return 0;
+// Compute a cube
+int cube(int x) {
+ return x * x * x;
}
int main() {
std::cout << square(4) << std::endl;
+ std::cout << cube(3) << std::endl;
return 0;
}
The added std::cout << cube(3) << std::endl; is a new line from a simple insertion (no removed lines). There's also a new line from the cube function because the new region is 3 lines while the old region was only 2, so the difference counts as a new line. That gives CountLineNew = 2.
See Changed Lines for the lines a region has in common, Removed Lines for the reverse of this metric, and Percent Changed for a single score combining all three.