This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
mgthomas99's profile picture

CodeMetrics - Calculate code properties and complexity

Started by mgthomas99, 04 August 2016 - 02:40 AM
mgthomas99 #1
Posted 04 August 2016 - 04:40 AM


Welcome to CodeMetrics!
CodeMetrics is a lightweight API used to calculate Lua script properties such as code line count and complexity.

Installation
pastebin get qq1X1kWw CodeMetrics

How to use CodeMetrics
Command Line
To use CodeMetrics from the command line, simply execute
CodeMetrics ["path_to_script"]
Doing so will print basic script information about "path_to_script" to the shell,

From Another Script
To use CodeMetrics from another script, we must first load the CodeMetrics API.
os.loadAPI("path/to/CodeMetrics")
To retrieve the code properties of a Lua script, you must first create a Script 'object'.
my_script = CodeMetrics.new("path/to/myscript")
There! Now we are ready to obtain information about the file "myscript"!

Here are the current functions we can execute on the object:

	my_script:get_character_count()
	my_script:get_subroutine_count()
	my_script:get_cyclo_complexity()
	my_script:get_function_count()
	my_script:get_count("if")	-- This can be any keyword
	my_script:get_line_count()
CodeMetrics also takes into account comments and quotes and does
not credit you a code line if it is a comment, so no cheating!

Cyclomatic Complexity
SpoilerThe complexity calculator used within CodeMetrics is based on the Cyclomatic Complexity
algorithm, in which the complexity increases based on the number of subroutines.

For example, the following code has a complexity of 1 as there are no subroutines:

		print("Hello Sailor!")
		sleep(1)
		print("Goodbye.")
	
The following code would have a complexity of 3 as there is one subroutine:

		name = io.read()
		if (name == "Sailor") then
			print("Hello!")
		end
	
As the depth level increases, the complexity increases at a quicker rate. Therefore, nesting
statements and using anonymous functions is discouraged as this accelerates the complexity.

SpoilerApologies for the bad thread formatting, this is my first post :)/>
Edited on 04 August 2016 - 12:58 PM
Emma #2
Posted 04 August 2016 - 05:21 AM
Neat, although using the word coroutines the way you are can be a little confusing for Lua as there are specific 'coroutine' objects that are different from what you are counting. Maybe 'subroutine' makes more sense? Cool idea though, and very nicely implemented as well.
Bomb Bloke #3
Posted 04 August 2016 - 06:17 AM
I believe "block" is the term you're after. I'm not sure that block depth really matters to performance, though, unless you're defining functions within functions or somesuch.
mgthomas99 #4
Posted 04 August 2016 - 02:57 PM
Neat, although using the word coroutines the way you are can be a little confusing for Lua as there are specific 'coroutine' objects that are different from what you are counting. Maybe 'subroutine' makes more sense? Cool idea though, and very nicely implemented as well.
You're absoultely right, I have edited CodeMetrics and renamed the 'script#get_coroutine_count' to 'script#get_subroutine_count'. The PasteBin link has also changed.

I believe "block" is the term you're after. I'm not sure that block depth really matters to performance, though, unless you're defining functions within functions or somesuch.
No, block depth does not matter to performance, but that is not what the calculator… calculates. The calculator gets complexity, not performance.