Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Scripting language designers face a temptation to drop all variable declarations. Variables can simply be created on first assignment. It seems so much easier than Java or C++ where everything must be declared before use. But there is a serious trade-off: misspelled variable names won't be caught until run-time. A restricted version of the language in which minimal declarations are required becomes necessary. Hence, Perl's "use strict" with "my" variables.

My first discovery of the value of this principle was in FORTRAN. In the old days people just started integer variables with I-N and float variables with other letters. But when I started using "IMPLICIT NONE" and found the power of compile-time typo-catching I never looked back.

Python and Ruby claim to be better-designed rivals to Perl, but neither of them has static checking for variable-name typos with the same power as Perl's. Variables on the left-hand side of assignments are particularly vulnerable to error. Python at least has PyChecker; Ruby has nothing.

Ruby users claim that everything can be caught by unit testing. That is a very poor idea because obscure paths through the code may be missed, and then a crash will happen during some crucial run later on. IMHO, unless Ruby comes up with a "strict" variant that allows some minimal static checking it will never be a serious rival for Perl for large projects.

Languages can do even more static checking without getting in the way. For example, OCaml does type inferencing based on the functions being applied to the variables. Explicit declarations are seldom needed for full compile-time type checking. The drawback is that operators have to be distinct by type: for example, "+." is used for floating-point addition and "+" for integer addition.

For me, Perl with "use strict" has a good balance between typo-catching power and simplicity of use. Additional optional "use stricter" checks might be worth looking into, for example doing the same sort of limitations on package-level variables.

use stricter; use Data::Dumper; # typo: should be $Data::Dumper::Maxdepth $Data::Dumper::MaxDepth = 3;
This might produce an error or warning about creating a new package variable in code outside the package, instead of silently failing as it does now.

In reply to The power of strict typo-checking by tall_man

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-16 20:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found