Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Check spelling in perl source code.

by chrestomanci (Priest)
on Sep 27, 2013 at 15:23 UTC ( #1056001=perlquestion: print w/ replies, xml ) Need Help??
chrestomanci has asked for the wisdom of the Perl Monks concerning the following question:

Greetings wise brothers, I seek your wisdom in the matter of how fat fingers, mistakes and carelessness can corrupt our beautiful code.

I am looking for a way to spellcheck perl source code. If it can be done reliably enough then I would like to add it as a pre-commit hook to our source control, but even if that is not possible, it would be good to warn developers so that they can change things.

I have done an internet search and found modules such as Perl::Critic::Policy::Documentation::PodSpelling and Test::Spelling, which look useful, but they only check the spelling of POD documents not the rest of the source code.

What I would really like to do is to intelligently check the spelling on variable and method names as well. So that if someone names a scalar as $new_varable or $newVarable it will find the variable in the source, intelligently split the name on underscores and case changes, and spellcheck the parts. (And report that varable is wrong (it should have an i). I don't need a spellchecker to offer replacements, as my editor can usually do that once I isolate the error, but I do need a better way to find error.

The reason I am after such a tool is that developers will often search for methods or variables by name. So for example, a developer might be looking for the function to increase a variable, and miss it because it is named increease_varable()

Any suggestions? do such tools exist? I know that it is impossible to write a perfect tool of this nature, because only perl can parse perl, but something that does 90% would be a great deal better than nothing. Looking at perldebguts, I was wondering if you could hook the debugger to return all the names (variables, functions, globs etc) declared in a source file, and then parse them for possible errors.

Comment on Check spelling in perl source code.
Select or Download Code
Re: Check spelling in perl source code.
by MidLifeXis (Prior) on Sep 27, 2013 at 15:33 UTC

    I turn on flyspell-mode in my editor.

    Update: ... and ignore anything that isn't actually a problem. There are times where lots of items end up being flagged, but that is the cost of spell-checking source code. My "hey, I cannot find this in my spelling rules" indicator is pretty innocuous, so it stays out of the way for most blocks where spelling should be ignored, but is visible enough to catch my eye when I am at least using things that should be spelled properly.

    --MidLifeXis

Re: Check spelling in perl source code.
by toolic (Chancellor) on Sep 27, 2013 at 15:34 UTC
    I don't know if such a tool exists, but PPI can be used to parse Perl code.
Re: Check spelling in perl source code.
by davies (Vicar) on Sep 27, 2013 at 15:36 UTC

    Code to get the subs and variables in a package is simple. Introspection, strict, warnings and "defined" shows my attempt. The fuzzy matching is a different matter. The only thing I can think of is to produce a list of possible separators & then loop through the names, eliminating the separators. Then you could search for duplicates. This wouldn't help, though, if one developer is using SetAttribute and the other is looking for AttributeSet. I can't see how to deal with that.

    Regards,

    John Davies

Re: Check spelling in perl source code.
by daxim (Chaplain) on Sep 27, 2013 at 15:47 UTC
    That's doomed to fail. You do not account for that a huge number of method calls resolve to code in CPAN libraries, not yours. They do not conform to your sense of order and aesthetics.

    I suggest you undo this problem by investigating why you feel that programmers need to guess and search blindly for identifiers. If the identifiers are part of the public API, they should be documented in POD and already spell-checked. If they aren't, then who cares they are guessable - you would look up the name from the code after diving in from an entry point, or Ctags or similar.

Re: Check spelling in perl source code.
by wjw (Deacon) on Sep 28, 2013 at 02:09 UTC
    I have to say that spell checking code seems a little unlikely to me. It assumes a pretty stringent and specific set of rules about writing code and what I would consider to be a stupendous conformance to that set of rules. I seldom spell thing correctly when making variable for example. A typical variable I might use would be %qs_m which to me means "hash of query strings of type that return multiple values".

    I can understand the need for coding standards, really, I do! But when it gets down to it, I will occasionally provide a map or dictionary to explain variables (Or anything else in the code) if it seems needed. The level of standardization that would require spell checking my variable names and method calls would so much hinder my coding process! No matter what, if I have to look at someone else's code, the best way to figure out what is going on is to fire up a debugger and step through it. Simply reading it, regardless of how it is standardized, does not help me one bit.

    Seems to me that is what POD is for.... .

    I wish I knew of a tool to help out, but honestly, I can't imagine the ROI on such a tool.

    To be clear, this is my opinion and not a criticism!

    If I am way off, don't hesitate to tell me about it. I would honestly like to understand the value of that level of code checking...

    • ...the majority is always wrong, and always the last to know about it...
    • ..by my will, and by will alone.. I set my mind in motion

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056001]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-10-25 21:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls