Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there code metrics that will cover variable scoping

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?

like image 620
krm Avatar asked Jan 15 '13 10:01

krm


1 Answers

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.

like image 101
gefei Avatar answered Sep 21 '22 16:09

gefei