In trying to get an overview of how difficult some legacy C++ and C# code is to maintain, and the risk of introducing bugs to it, it has been suggested that it would be useful to measure how widely or narrowly variables are scoped. The code uses a lot of globals, or widely scoped variables, where local ones would be better. A common occurrence is to find that these variables are used for 2 or 3 lines of code several scope levels in from where they are declared.
I know static code analysis tools usually try to quantify coupling and cohesion, but is there anything more specifically measuring variable/data scope?
Yes, that's a standard technique of static analysis. It's called analysis of variable liveness. In this book, the introduction example is doing such an analysis.
From the Wikipedia article about it:
In compiler theory, live variable analysis (or simply liveness analysis) is a classic data flow analysis performed by compilers to calculate for each program point the variables that may be potentially read before their next write, that is, the variables that are live at the exit from each program point.
Stated simply: a variable is live if it holds a value that may be needed in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With