Ruby Screenshot of the Week #24: Quick Fix Previews
By Tor Norbye on Dec 03, 2007
First of all, NetBeans 6.0 (final) was released this morning. Go get it!
So let's talk about 6.1 :) I just updated the quickfix infrastructure such that we can automatically generate previews for how the hints will modify the source. I've also added some new hints.
Let's start looking at the
user.rb file in the sample Depot application. If I place the caret inside one of the if blocks, a lightbulb appears:
NetBeans offers to replace this if-block with one where the "if" is a statement modifier. This idea came from the excellent "Exploring Beautiful Languages"
by Luis Diego Fallas, where he implements NetBeans Ruby hints -- in Scala!
Here's the new preview functionality in action; instead of just applying the fix I invoke the Preview and get the following dialog which shows what the fix will do:
Preview is particularly useful for larger source changes like Extract Method.
Here's another method from the same file:
Obviously, we can apply the same "convert to statement modifier" here, but look at the first suggested fix:
NetBeans will convert "if negative condition" to "unless", and "unless negative condition" to "if" to make the code more readable. This was also shown by Luis in his blog entry. Here's the proposed fix:
and we can apply the other conditional cleanup as well to end up with a much simpler statement:
There is one more recently added hint: Check for accidental assignments.
At first I got a lot of false positives for this hint, since many Ruby programmers seem to like to intentionally assign in their conditions. But then I updated the rule to only complain if the variable being assigned to had already been seen in this scope, and that seems to do the trick perfectly. Newly assigned variables are intentional side effects of the assignment, and assignments to existing variables are likely bugs and should be avoided.