5

With Java on one side and Ruby/Groovy on the other, I know that in the second camp I'm free to make typos which will not get caught until run-time. Is this true of all dynamically-typed languages?

Edit: I've been asked to elaborate on the type of typo. In Ruby and in Groovy, you can assign to a variable with an accidental name that is never read. You can call methods that don't exist (obviously your tests should catch this, it's been said). You can refer to classes that don't exist, etc. etc. Basically any valid syntax, even with typographical errors, is valid in both Ruby and Groovy.

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
  • 1
    You may want to elaborate on what kind of typo, specifically where it occurs. – NomeN Mar 11 '10 at 22:15
  • 2
    Did you mean - until run time? – Seva Alekseyev Mar 11 '10 at 22:20
  • 6
    You seem to consider "friendly" as "doesn't complain when I make a typo until it needs to be executed". I would think that the opposite definition is better. A "friendly" interpreter is one that parses and the code and looks for typos and gives me an error before the actual execution. – shoosh Mar 11 '10 at 22:20
  • @Seva Alekseyev, of course :)... thanks for catching that! – Dan Rosenstark Mar 11 '10 at 22:22
  • @shoosh, that's just some semantics and irony in the title. Java is not-typo friendly because it does not welcome code with typos, meaning it will not run that code. A criminal-friendly country is not one that weeds them out but one that doesn't bother them, typically. – Dan Rosenstark Mar 11 '10 at 22:23
  • This has nothing to do with the language. It has to do with the IDE/compiler. No language is more 'typo-friendly', or whatever you want to call it, than any other. Some IDE's are though. – Cam Mar 11 '10 at 23:39
  • 1
    @incrediman Nothing prevents you from defining a language in which you have to declare names before using them. You could even invent a language that only accepts English words as identifiers though the language definition would be huge ;-) – Alex Jasmin Mar 12 '10 at 00:15
  • @incrediman, in fact it is the IDE/compiler and not a basic linguistic structure. However, this is not a philosophy course: languages have their IDE options and compiler options, and the limitations of those grow out of language features. I don't think that you CAN do typo checking in Ruby, because the only typos are syntax errors which all Ruby IDEs handle perfectly. – Dan Rosenstark Mar 12 '10 at 00:49
  • @Alex and yar - I misunderstood the question - reading the "Edit:" I now understand what you mean, and obviously my comment is incorrect in the context of what you were actually referring to :) – Cam Mar 12 '10 at 04:21

4 Answers4

3

In Perl, if you declare use strict in your code, then you must declare your variables with my. Typos in variable names will then be caught at compile-time. This is one of the biggest things I miss when coding in Python.

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
  • By the way, I think `use strict` makes Perl's usability far far better than otherwise. :) – Paul Nathan Mar 12 '10 at 00:36
  • The strangest thing about all this is, wasn't there an option to do that in QuickBasic or maybe the early Visual Basics? Insist that you wanted to use types? – Dan Rosenstark Mar 12 '10 at 00:51
  • @yar: You're not insisting on using types. You're insisting on using variable declaration. The type of the variable has nothing to do with whether you mis-spell its name. Fortran is a strongly-typed language that allows you to avoid declaring variables. You need `IMPLICIT NONE` at the top, just like Perl's `use strict`. If you don't use `IMPLICIT NONE`, you can make typos with your variable names just as easily as in a dynamic language. – ire_and_curses Mar 12 '10 at 01:02
  • The combination of case-sensitivity and implicit variable creation were sufficient to eliminate any interest I might have had in learning Python. I program in C, and usually get upper/lowercase right, but not absolutely always. A good language should try when practical to avoid having a typo turn a valid program which does the right thing into a valid program which does the wrong thing. Obviously if one types `i=11` when one meant `i=1` a language can't catch that, but upper/lowercase mistypings are sufficiently common that they should generate compiler squawks rather than wrong behavior. – supercat May 16 '14 at 17:08
3

Python is typo-friendly in the way you described in your question.

But this does not mean that these 'typos' can only be caught @ runtime. When using a code analyzer like pylint (ideally integrated into your development environment) you'll catch 'most' of these consistently before hitting 'run'.

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
  • Really? That's neat, I didn't know that. In Ruby there's no such thing, I think. Netbeans catches a few things, but it gets confused quite quickly – Dan Rosenstark Mar 12 '10 at 01:09
  • Some tools exist according to this question (http://stackoverflow.com/questions/286564/can-anyone-recommend-a-ruby-source-code-analyzer-something-like-pylint) although none of the mentioned tools seems as fully-featured as pylint at the moment. – ChristopheD Mar 12 '10 at 06:52
2

For the most part, yes. Dynamic typing and not requiring declaration of variables are language properties that are frequently found together.

However, these are not inherently related. A language can easily have dynamic typing while requiring variable names to be declared before use. As ire_and_curses mentions, this can be achieved in Perl via the "use strict" directive.

nobody
  • 19,814
  • 17
  • 56
  • 77
  • 2
    The other way around happens, too - Objective C is dynamic re: method names, but variable declaration rules are those of C. – Seva Alekseyev Mar 11 '10 at 23:59
  • Great answer. I think it's true for other things, and not just variable declaration. My question is, aside from SmallTalk, what is the biggest dynamic language rulebreaker? Scala? – Dan Rosenstark Mar 12 '10 at 00:54
  • I'm not a Scala programmer but my understanding is that Scala is not a dynamic language by most definitions. – Alex Jasmin Mar 12 '10 at 01:50
2

Here's what happens when I try to get into the pitfalls you mentioned in Squeak and Dolphin, two implementations of the dynamic language Smalltalk 80.

You can assign to a variable with an accidental name that is never read

The Smalltalk language requires temp and instance variables to be declared. If I try to compile a method containing an undefined variable I get a compile-time error.

| anArray |
anArrray := Array with: 2 with: 1. "Unknown variable anArrray"

Creating variables dynamically is not something dynamic languages have to allow. There's a difference between typeless declarations and no declaration at all.


You can call methods that don't exist

The compiler issue a warning if you use a selector (i.e. method name) that is entirely unknown.

The compiler won't bother if I call the method paint on an array because there's another class in the system implementing paint. That error will only be caught at runtime.

If however I call the method sortt (while I intend to call sort) the compiler generates a warning. When developing top-down you can proceed pass these warnings.

| anArray |
anArray := Array with: 2 with: 1.
anArray paint. "Runtime error. You can't paint an array but perhaps a Shape"
anArray sortt. "Compile-time warning"

You can refer to classes that don't exist

This is not allowed. Though in Squeak you can quickly create a new class from the error dialog if needed.

Euan M
  • 1,126
  • 11
  • 20
Alex Jasmin
  • 39,094
  • 7
  • 77
  • 67
  • Fascinating. I don't know if Smalltalk is gaining in popularity, but I do hear about it on all of my dynamic-language questions :) – Dan Rosenstark Mar 12 '10 at 01:13