Static code analysis in Ruby can be done using metric_fu. metric_fu is a nice gem which internally uses Saikuro, Flog, Flay, Rcov, Reek, Roodi, Churn, RailsBestPractices and generals useful quality metrics about the code. With the help of above it analyzes code for complexity, convention compliance, duplicate and unused code.
Saikuro
Saikuro analyzes code and reports cyclomatic complexity of each method in the analyzed code. Each method has a complexity of 1 by default. In addition Saikuro adds 1 to the cyclomatic complexity for each of the below :
- conditional and looping operator
- each when in a case
- rescue statements
- blocks like each
Flog
Flog scores an ABC(Assignment, Branches, Conditions) metric on the methods. With the help of this classes that have a high average flog score may be further looked at for optimization and refactoring.
Flay
Flay analyzes code for structural similarities. Code reported by Flay is a good candidate for refactoring.
Rcov
Rcov is a code coverage tool. It analyses the code coverage by running the unit tests in the project.
Reek
Reek reports code smells in the ruby code. Some of the code smells its reports are missing comments, long methods, long class, long parameter list, uncommunicative names, etc. A complete list of code smells reported by Reek cane be found here – https://github.com/kevinrutherford/reek/wiki/code-smells
Roodi
Roodi stands for Ruby Object Oriented Design Inferometer. It parses your Ruby code and warns you about design issues you have based on the checks that is has configured. Follwing checks are the supported checks. A yml file can be provided to include only selected checks
- AssignmentInConditionalCheck – Check for an assignment inside a conditional. It’s probably a mistaken equality comparison.
- CaseMissingElseCheck – Check that case statements have an else statement so that all cases are covered.
- ClassLineCountCheck – Check that the number of lines in a class is below the threshold.
- ClassNameCheck – Check that class names match convention.
- CyclomaticComplexityBlockCheck – Check that the cyclomatic complexity of all blocks is below the threshold.
- CyclomaticComplexityMethodCheck – Check that the cyclomatic complexity of all methods is below the threshold.
- EmptyRescueBodyCheck – Check that there are no empty rescue blocks.
- ForLoopCheck – Check that for loops aren’t used (Use Enumerable.each instead)
- MethodLineCountCheck – Check that the number of lines in a method is below the threshold.
- MethodNameCheck – Check that method names match convention.
- ModuleLineCountCheck – Check that the number of lines in a module is below the threshold.
- ModuleNameCheck – Check that module names match convention.
- ParameterNumberCheck – Check that the number of parameters on a method is below the threshold.
Churn
Gives a number of times a file, class, method has been changed during the life of the project. Churn evaluates this by looking at the commits done in the SCM repository. Chrun supports Git, Mercurial and SVN (partially)
Rails Best Practices
Analyses code for Rails Best Practices. The default supported checks are here https://github.com/flyerhzm/rails_best_practices/wiki
Other Tools
There are some other tools that may also be useful