rake stats

rake stats is one of those less-oft used tasks when developing Rails applications, but it does provide a good breakdown of some code statistics. You’ll get total lines, lines of code, # of classes, # of methods, methods per class, lines of code per method and some ratios.

Here’s a breakdown of one project at work:

+———————————+———-+———-+————-+————-+——-+———-+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+———————————+———-+———-+————-+————-+——-+———-+
| Controllers          |  2414 |  1837 |      31 |     182 |   5 |     8 |
| Helpers              |   319 |   248 |       0 |      38 |   0 |     4 |
| Models               |  1576 |  1149 |      36 |     142 |   3 |     6 |
| Libraries            |  1275 |   832 |       5 |     109 |  21 |     5 |
| Integration tests    |   284 |   181 |       2 |       8 |   4 |    20 |
| Functional tests     |  4406 |  3364 |      27 |     268 |   9 |    10 |
| Unit tests           |  2287 |  1817 |      35 |     214 |   6 |     6 |
+———————————+———-+———-+————-+————-+——-+———-+
| Total                | 12561 |  9428 |     136 |     961 |   7 |     7 |
+———————————+———-+———-+————-+————-+——-+———-+
  Code LOC: 4066     Test LOC: 5362     Code to Test Ratio: 1:1.3

It is also aware of specs and higher-level testing tools like Cucumber. Here is the breakdown of another project:

+———————————+———-+———-+————-+————-+——-+———-+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+———————————+———-+———-+————-+————-+——-+———-+
| Controllers          |   329 |   267 |      13 |      39 |   3 |     4 |
| Helpers              |   325 |   238 |       1 |      34 |  34 |     5 |
| Models               |  1275 |   904 |      20 |     114 |   5 |     5 |
| Libraries            |    85 |    68 |       3 |       8 |   2 |     6 |
| Model specs          |  1037 |   839 |       2 |       4 |   2 |   207 |
| View specs           |   406 |   337 |       0 |       1 |   0 |   335 |
| Controller specs     |   674 |   537 |       0 |       1 |   0 |   535 |
| Helper specs         |   103 |    84 |       0 |       0 |   0 |     0 |
| Mailer specs         |    65 |    52 |       0 |       1 |   0 |    50 |
| Cucumber features    |   338 |   192 |       0 |       3 |   0 |    62 |
+———————————+———-+———-+————-+————-+——-+———-+
| Total                |  4637 |  3518 |      39 |     205 |   5 |    15 |
+———————————+———-+———-+————-+————-+——-+———-+
  Code LOC: 1477     Test LOC: 2041     Code to Test Ratio: 1:1.4

A few times a year, I run rake stats for all our projects at work to get an idea of how our applications change over time at this level. I ran rake stats back in July and today I ran rake stats again. My summary was as follows: “Where we added code, we added tests, so our test coverage remained the same or improved. Good job.”