scitools.com

← Metric catalog

Changed 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.

Changed Lines

API ID: CountLineChanged
Languages: Ada, Assembly, Basic, C#, C++, Fortran, Java, Jovial, Pascal, Python, Rust, VHDL, Web
Targets: Classes, Files, Functions, Modules, Packages, Subprograms

Number of changed 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, the changed lines is the number of lines in common, regardless of line contents. So, it's the minimum of the added and deleted line counts in each changed region.

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 sample above has 4 changed regions. The first is the deletion of // Utility functions, so added is 0 and deleted is 1, and the changed lines is the smaller value: 0. The second is the change to the return statement with 1 line added and 1 line deleted. So changed lines is 1. The third change is the cube() function with 3 lines added and 2 lines deleted, giving 2 lines changed from the smaller (deleted) count. The final change is the insertion of std::cout << cube(3) << std::endl; with 1 added line and 0 deleted lines, so changed lines is 0. So, the complete CountLineChanged is 0 + 1 + 2 + 0 = 3.

See New Lines and Removed Lines for the lines a region doesn't have in common, and Percent Changed for a single score combining all three.