Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (8)
    As of 2014-09-19 06:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (132 votes), past polls