Automatic code quality tool for Ruby? [closed]

2019-01-30 05:22发布

问题:

One thing I really miss about Java is the tool support. FindBugs, Checkstyle and PMD made for a holy trinity of code quality metrics and automatic bug checking.

Is there anything that will check for simple bugs and / or style violations of Ruby code? Bonus points if I can adapt it for frameworks such as Rails so that Rails idioms are adhered to.

回答1:

I've recently started looking for something like this for Ruby. What I've run across so far:

  • Saikuro
  • Roodi
  • Flog

These might be places to start. Unfortunately I haven't used any of the three enough yet to offer a good opinion.



回答2:

Saikuro and Flog can be good for getting a basic idea of code complexity. You can also use a tool like rcov to look at your test coverage.
There is a plugin for Rails projects that combine all those metrics into a single rake task. It is called metric_fu.



回答3:

Projects I've found and tested recently:

  • https://github.com/railsbp/rails_best_practices
    • Seems to work, and gives sensible warnings
  • https://github.com/simplabs/excellent
    • Works, but quite a few false positives
  • https://github.com/troessner/reek
    • I disagree with most of the warnings from this tool, but it works
  • http://www.cs.umd.edu/projects/PL/druby/
    • This requires ocaml; I haven't tried it, but it looks like it might be good
  • http://roodi.rubyforge.org/
    • does not appear to be Ruby 1.9 compatible
  • https://github.com/gdb/ruby-static-checker
    • Is broken for me, and only catches name errors, so unit tests should cover that.
  • https://github.com/michaeledgar/laser
    • Doesn't compile for me


回答4:

You might want to try out RuboCop. It is a Ruby code style checker based on the Ruby Style Guide. It's maintained pretty actively and it's based on standard Ruby tooling (like the ripper library). It works well with Ruby 1.9 and 2.0 and has great Emacs integration. I hope you'll find it useful!



回答5:

Dust looks like it can help you find unused and useless code, which seems like it sort-of fits what you're after.

I'm not aware of any other such tools.
This problem is vastly harder to address in ruby than it is in java - you'll note that all those java tools brand themselves as using 'static analysis' of the code.
Static analysis of ruby code often isn't possible, because there isn't anything static that you can analyze (methods often get created at runtime and so on)

At any rate, some of these things are unneeded in ruby because the language builds them in. For example, you don't need a coding standard to enforce that your classes are all NamedLikeThis because the code won't work if they aren't.

P.S. I have to add the standard disclaimer that those kind of tools can often be a bit of a red herring. You can spend all day making your code adhere to what the tool thinks it should be, and end up with more bugs than you started with.
IMHO the best solution is to write your code fluently so you can read it more easily. No amount of static analysis is going to be as good as a human reading code which clearly states what it is meant to do. Being able to do this is where ruby is light-years ahead of many other languages. I personally would recommend you aim your efforts at learning how to write more fluently, and educating your team about such things, than spending time on static analysis.



回答6:

Another nice tool, although in early stages according to the author is reek:

http://reek.rubyforge.org/

reek currently includes very naive checks for the following code smells:

  • Long Method
  • Large Class
  • Feature Envy
  • Uncommunicative Name
  • Long Parameter List
  • Utility Function
  • Nested Iterators
  • Control Couple
  • Duplication
  • List item

Personally I think it still has too much false positives, but just looking at the output in some of my code helped me rethink some decisions about code style and architecture.



回答7:

Code Climate is a SaaS tool that integrates through git and automatically "grades" your code. It notifies you via various channels if there a sudden drop in quality. Nice UI as well.



回答8:

I didn't see this questions when asked, but a blog post I did might help as well. In it I cover a bunch of Ruby tools and specifically cover 4 code quality tools...

  1. Roodi
  2. Dust
  3. Flog
  4. Saikuro

It might also be worth checking out Towelie and Flay

http://devver.wordpress.com/2008/10/03/ruby-tools-roundup/

Now we have combined a lot of tools into an only Ruby code quality and metrics monitoring tool called Caliper. This might fit your needs well. It tracks various quality metrics over the life of a project.

Caliper - improve your Ruby code



回答9:

There is also excellent. I haven't tried it yet, but it too looks promising.