3

In MATLAB, I can get a quantification of how succinct my code is by using the following code:

t = mtree('myCode.m','-file');
length(t.nodesize)

As CODY (http://www.mathworks.com/matlabcentral/about/cody/) describes it:

Cody uses a node-count calculation to determine the solution size based on the number of nodes in the parse tree. Think of size as code length. Writing succinct code earns you a better result. Comments do not contribute to code size.

To illustrate how Cody determines size, here are two solutions to the plus-one problem.

Solution 1 has a size of 12:

function y = plus_one(x)
  y = x+1;
end

Solution 2 has a size of 16:

function y = plus_one(x)
  z = x;
  y = z+1;
end

Is there any way to get a similar calculation for R code? Are there any other quantifiable metrics for code quality in R? MATLAB also can measure McCabe complexity, for example:

mlint -cyc myCode.m
Community
  • 1
  • 1
jmainland
  • 43
  • 3

1 Answers1

2

Maybe this is useful:

#   line1 col1 line2 col2 id parent          token terminal     text
#33     1    1     1   34 33      0           expr    FALSE         
#1      1    1     1    8  1      3         SYMBOL     TRUE plus_one
#3      1    1     1    8  3     33           expr    FALSE         
#2      1   10     1   11  2     33    LEFT_ASSIGN     TRUE       <-
#32     1   13     1   34 32     33           expr    FALSE         
#4      1   13     1   20  4     32       FUNCTION     TRUE function
#5      1   21     1   21  5     32            '('     TRUE        (
#6      1   22     1   22  6     32 SYMBOL_FORMALS     TRUE        x
#7      1   23     1   23  7     32            ')'     TRUE        )
#29     1   25     1   34 29     32           expr    FALSE         
#9      1   25     1   25  9     29            '{'     TRUE        {
#10     1   26     1   26 10     12         SYMBOL     TRUE        y
#12     1   26     1   26 12     29           expr    FALSE         
#11     1   27     1   27 11     29      EQ_ASSIGN     TRUE        =
#19     1   28     1   30 19     29           expr    FALSE         
#13     1   28     1   28 13     15         SYMBOL     TRUE        x
#15     1   28     1   28 15     19           expr    FALSE         
#14     1   29     1   29 14     19            '+'     TRUE        +
#16     1   30     1   30 16     17      NUM_CONST     TRUE        1
#17     1   30     1   30 17     19           expr    FALSE         
#18     1   31     1   31 18     29            ';'     TRUE        ;
#24     1   33     1   33 24     26         SYMBOL     TRUE        y
#26     1   33     1   33 26     29           expr    FALSE         
#25     1   34     1   34 25     29            '}'     TRUE        }

sum(getParseData(parse(text="plus_one <- function(x) {y=x+1; y}"))$terminal)
#[1] 15

sum(getParseData(parse(text="plus_one <- function(x) {z = x; y = z+1; y}"))$terminal)
#[1] 19

I'm not sure if I agree with a general statement like

Writing succinct code earns you a better result.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • 1
    To be fair, it seems that Cody is some sort of distributed code-golf game in Matlab, so maybe "better" here is meant in the spirit of fun. – joran Sep 30 '13 at 16:11