Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Analyzer for errors in code ? (http://www.velocityreviews.com/forums/t865183-analyzer-for-errors-in-code.html)

David Unric 11-09-2010 04:00 PM

Analyzer for errors in code ?
 
Hello,

I would like to check a source code for common errors like use of unused
variables. The "ruby -c" only checks for syntax errors.

Is there some tool for Ruby like pylint or pyflakes for Python ?
It has to _not_ run the code to do analysis. At least mistypos of method
and variable names would be enough.

Thx in an advance.

--
Posted via http://www.ruby-forum.com/.


Robert Klemme 11-09-2010 04:40 PM

Re: Analyzer for errors in code ?
 
On Tue, Nov 9, 2010 at 5:00 PM, David Unric <dunric29a@gmail.com> wrote:
> I would like to check a source code for common errors like use of unused
> variables. The "ruby -c" only checks for syntax errors.
>
> Is there some tool for Ruby like pylint or pyflakes for Python ?
> It has to _not_ run the code to do analysis. At least mistypos of method
> and variable names would be enough.


Unused local variables would be doable, but detection of typos of
variable and method names is almost impossible without running the
code. The reason is that the set of valid method names may change at
will during the course of a programs execution. Plus, with
#method_missing you can properly handle methods that are never ever
defined. The usability of such a tool might be very limited.

Having said that you may want to try ruby's command line option -w.

See also
http://stackoverflow.com/questions/1...-tool-for-ruby

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/


David Unric 11-09-2010 06:55 PM

Re: Analyzer for errors in code ?
 
Robert Klemme wrote in post #960353:
> On Tue, Nov 9, 2010 at 5:00 PM, David Unric <dunric29a@gmail.com> wrote:
>> I would like to check a source code for common errors like use of unused
>> variables. The "ruby -c" only checks for syntax errors.
>>
>> Is there some tool for Ruby like pylint or pyflakes for Python ?
>> It has to _not_ run the code to do analysis. At least mistypos of method
>> and variable names would be enough.

>
> Unused local variables would be doable, but detection of typos of
> variable and method names is almost impossible without running the
> code. The reason is that the set of valid method names may change at
> will during the course of a programs execution. Plus, with
> #method_missing you can properly handle methods that are never ever
> defined. The usability of such a tool might be very limited.
>
> Having said that you may want to try ruby's command line option -w.
>
> See also
>

http://stackoverflow.com/questions/1...-tool-for-ruby
>
> Kind regards
>
> robert


I understand it is difficult because the dynamic nature of Ruby, but
Python is in the same league and PyLint tool does "wonders" at detecting
possible errors without code execution
(http://www.logilab.org/card/pylintfeatures). It uses Abstract Syntax
Tree parser and Ruby also has its own AST so it would be possible.

But I asked for something more simplistic, like revealing trivial errors
like mentioned variable name typo.

See the following example:

Running with 'ruby -c' returns 'Syntax OK'.
Running with 'ruby -w' also doesn't warn about undefined 'mymsg'
variable.

Only when the condition becomes true, execution aborts with `<main>':
undefined local variable or method `mymsg' for main:Object (NameError)

if __FILE__ == $0
my_msg = 'Hello'
if ARGV[0] == 'doit'
puts mymsg
else
puts 'Nope'
end
end


How to avoid such pitfalls with writing more complex ruby code ? I can't
believe there is no other way how to verify the code without 'waiting
for exception at proper conditions' and then solve the bug.

--
Posted via http://www.ruby-forum.com/.


Tony Arcieri 11-09-2010 07:07 PM

Re: Analyzer for errors in code ?
 
[Note: parts of this message were removed to make it a legal post.]

There are many, many tools of this nature. A great place to start might be
the metric_fu gem. It combines a number of different tools to do code
quality checking:

http://metric-fu.rubyforge.org/

<http://metric-fu.rubyforge.org/>Take a look at the tools it uses and see if
any fit your needs.

On Tue, Nov 9, 2010 at 9:00 AM, David Unric <dunric29a@gmail.com> wrote:

> Hello,
>
> I would like to check a source code for common errors like use of unused
> variables. The "ruby -c" only checks for syntax errors.
>
> Is there some tool for Ruby like pylint or pyflakes for Python ?
> It has to _not_ run the code to do analysis. At least mistypos of method
> and variable names would be enough.
>
> Thx in an advance.
>
> --
> Posted via http://www.ruby-forum.com/.
>
>



--
Tony Arcieri
Medioh! A Kudelski Brand


Robert Klemme 11-09-2010 08:46 PM

Re: Analyzer for errors in code ?
 
On 11/09/2010 07:55 PM, David Unric wrote:
> Robert Klemme wrote in post #960353:
>> On Tue, Nov 9, 2010 at 5:00 PM, David Unric<dunric29a@gmail.com> wrote:
>>> I would like to check a source code for common errors like use of unused
>>> variables. The "ruby -c" only checks for syntax errors.
>>>
>>> Is there some tool for Ruby like pylint or pyflakes for Python ?
>>> It has to _not_ run the code to do analysis. At least mistypos of method
>>> and variable names would be enough.

>>
>> Unused local variables would be doable, but detection of typos of
>> variable and method names is almost impossible without running the
>> code. The reason is that the set of valid method names may change at
>> will during the course of a programs execution. Plus, with
>> #method_missing you can properly handle methods that are never ever
>> defined. The usability of such a tool might be very limited.
>>
>> Having said that you may want to try ruby's command line option -w.
>>
>> See also
>>

> http://stackoverflow.com/questions/1...-tool-for-ruby


> I understand it is difficult because the dynamic nature of Ruby, but
> Python is in the same league and PyLint tool does "wonders" at detecting
> possible errors without code execution
> (http://www.logilab.org/card/pylintfeatures). It uses Abstract Syntax
> Tree parser and Ruby also has its own AST so it would be possible.
>
> But I asked for something more simplistic, like revealing trivial errors
> like mentioned variable name typo.
>
> See the following example:
>
> Running with 'ruby -c' returns 'Syntax OK'.
> Running with 'ruby -w' also doesn't warn about undefined 'mymsg'
> variable.
>
> Only when the condition becomes true, execution aborts with `<main>':
> undefined local variable or method `mymsg' for main:Object (NameError)
>
> if __FILE__ == $0
> my_msg = 'Hello'
> if ARGV[0] == 'doit'
> puts mymsg
> else
> puts 'Nope'
> end
> end
>
>
> How to avoid such pitfalls with writing more complex ruby code ? I can't
> believe there is no other way how to verify the code without 'waiting
> for exception at proper conditions' and then solve the bug.


Well, actually you would catch the bug during testing which you have to
do anyway. If you don't catch it during testing you have bad test
coverage. :-)

The problem with this is that "mymsg" might be a method call or a local
variable read. Ruby recognizes local variables (via assignment) so it
knows that "my_msg" is a local variable. But there is no easy way I am
aware of to reliably detect that "mymsg" is a typo from "my_msg". ;aybe
one could do some fuzzy pattern matching.

Kind regards

robert

David Unric 11-09-2010 09:00 PM

Re: Analyzer for errors in code ?
 
Robert Klemme wrote in post #960402:

>>
>> puts mymsg
>> else
>> puts 'Nope'
>> end
>> end
>>
>>
>> How to avoid such pitfalls with writing more complex ruby code ? I can't
>> believe there is no other way how to verify the code without 'waiting
>> for exception at proper conditions' and then solve the bug.

>
> Well, actually you would catch the bug during testing which you have to
> do anyway. If you don't catch it during testing you have bad test
> coverage. :-)
>
> The problem with this is that "mymsg" might be a method call or a local
> variable read. Ruby recognizes local variables (via assignment) so it
> knows that "my_msg" is a local variable. But there is no easy way I am
> aware of to reliably detect that "mymsg" is a typo from "my_msg". ;aybe
> one could do some fuzzy pattern matching.
>
> Kind regards
>
> robert


I see but such lint tool would parse whole source and included modules
and just warn if didn't find an asignment in the scope it's used in. In
the above case it wouldn't find any mymsg variable assignment or
function/method definiton and warn me about it.

--
Posted via http://www.ruby-forum.com/.


Kirk Haines 11-09-2010 09:03 PM

Re: Analyzer for errors in code ?
 
[Note: parts of this message were removed to make it a legal post.]

On Nov 9, 2010 11:56 AM, "David Unric" <dunric29a@gmail.com> wrote:

Robert Klemme wrote in post #960353:

> On Tue, Nov 9, 2010 at 5:00 PM, David Unric <dunric29a@gmail.com> wrote:
>> I would like to check ...

I understand it is difficult because the dynamic nature of Ruby, but
Python is in the same league and PyLint tool does "wonders" at detecting
possible errors without code execution
(http://www.logilab.org/card/pylintfeatures). It uses Abstract Syntax
Tree parser and Ruby also has its own AST so it would be possible.

But I asked for something more simplistic, like revealing trivial errors
like mentioned variable name typo.

See the following example:

Running with 'ruby -c' returns 'Syntax OK'.
Running with 'ruby -w' also doesn't warn about undefined 'mymsg'
variable.

Only when the condition becomes true, execution aborts with `<main>':
undefined local variable or method `mymsg' for main:Object (NameError)

if __FILE__ == $0
my_msg = 'Hello'
if ARGV[0] == 'doit'
puts mymsg
else
puts 'Nope'
end
end


How to avoid such pitfalls with writing more complex ruby code ? I can't
believe there is no other way how to verify the code without 'waiting
for exception at proper conditions' and then solve the bug.


--
Posted via http://www.ruby-forum.com/.


David Wright 11-09-2010 09:36 PM

Re: Analyzer for errors in code ?
 
David Unric wrote in post #960342:

> Is there some tool for Ruby like pylint or pyflakes for Python ?
> It has to _not_ run the code to do analysis.


It sounds like you are asking if Ruby has any static analysis tools.

The answer is yes, Tony pointed out a good resource, here is some more
reading.


http://www.klocwork.com/blog/2009/06...or-rubypython/
http://stackoverflow.com/questions/286564

--
Posted via http://www.ruby-forum.com/.


Ryan Davis 11-09-2010 10:02 PM

Re: Analyzer for errors in code ?
 

> But I asked for something more simplistic, like revealing trivial =

errors
> like mentioned variable name typo.


What should a ruby lint tool do with the below code?

def method_missing(*) # at _any_ scope: instance, class, module, =
global...
# ...
end

def x
a =3D 20
b =3D 30
puts c
end

my vote: nothing...

It is impossible to determine in a language like ruby w/o executing it. =
This is why we write tests. I know you're looking for something to catch =
"simple" errors... but you also want it to be correct. You can't have =
both. For every correctness checker attempt, I can make it wrong.

From: http://www.cl.cam.ac.uk/teaching/091...y/scooping.pdf




Ryan Davis 11-09-2010 10:12 PM

Re: Analyzer for errors in code ?
 

On Nov 9, 2010, at 14:02 , Ryan Davis wrote:

> From: http://www.cl.cam.ac.uk/teaching/091...y/scooping.pdf


(damnit, sorry... I'm post workout and fairly braindead... here is the =
text)

Scooping the Loop Snooper
an elementary proof of the undecidability of the halting problem

No program can say what another will do.
Now, I won't just assert that, I'll prove it to you:
I will prove that although you might work till you drop,
you can't predict whether a program will stop.

Imagine w e have a procedure called P
that will snoop in the source code of programs to see
there aren't infinite loops that go round and around;
and P prints the word "Fine!" if no looping is found.

You feed in your code, and the input it needs,
and then P takes them both and it studies and reads
and computes whether things will all end as they should
(as opposed to going loopy the way that they could).

Well, the truth is that P cannot possibly be,
because if you wrote it and gave it to me,
I could use it to set up a logical bind
that would shatter your reason and scramble your mind.

Here's the trick I would use=97and it's simple to do.
I'd define a procedure=97we'll name the thing Q=97
that would take any program and call P (of course!)
to tell if it looped, by reading the source;

And if so, Q would simply print "Loop!" and then stop;
but if no, Q would go right back up to the top,
and start off again, looping endlessly back,
till the universe dies and is frozen and black.

And this program called Q wouldn't stay on the shelf;
I would run it, and (fiendishly) feed it itself.
What behavior results when I do this with Q?
When it reads its own source code, just what will it do?

If P warns of loops, Q will print "Loop!" and quit;
yet P is supposed to speak truly of it.
So if Q's going to quit, then P should say, "Fine!"=97
which will make Q go back to its very first line!

No matter what P would have done, Q will scoop it:
Q uses P's output to make P look stupid.
If P gets things right then it lies in its tooth;
and if it speaks falsely, it's telling the truth!

I've created a paradox, neat as can be=97
and simply by using your putative P.
When you assumed P you stepped into a snare;
Your assumptions have led you right into my lair.

So, how to escape from this logical mess?
I don't have to tell you; I'm sure you can guess.
By reductio, there cannot possibly be
a procedure that acts like the mythical P .

You can never discover mechanical means
for predicting the acts of computing machines.
It's something that cannot b e done. So we users
must find our own bugs; our computers are losers!

GEOFFREY K. PULLUM
STEVENSON COLLEGE
UNIVERSITY OF CALIFORNIA SANTA CRUZ
MATHEMATICS MAGAZINE, 10/2000=



All times are GMT. The time now is 02:37 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.