Are All Dynamic Languages Typo-friendly?
Asked Answered
B

4

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.

Benevento answered 11/3, 2010 at 22:12 Comment(9)
You may want to elaborate on what kind of typo, specifically where it occurs.Pleurisy
Did you mean - until run time?Elude
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.Expansionism
@Seva Alekseyev, of course :)... thanks for catching that!Benevento
@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.Benevento
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.Seldon
@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 ;-)Nicker
@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.Benevento
@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 :)Seldon
C
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.

Centroclinal answered 11/3, 2010 at 22:26 Comment(4)
By the way, I think use strict makes Perl's usability far far better than otherwise. :)Ephah
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?Benevento
@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.Centroclinal
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.Distal
B
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'.

Bulldoze answered 11/3, 2010 at 23:15 Comment(2)
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 quicklyBenevento
Some tools exist according to this question (#287064) although none of the mentioned tools seems as fully-featured as pylint at the moment.Bulldoze
F
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.

Fletafletch answered 11/3, 2010 at 22:12 Comment(1)
Fascinating. I don't know if Smalltalk is gaining in popularity, but I do hear about it on all of my dynamic-language questions :)Benevento
R
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.

Rascality answered 11/3, 2010 at 22:39 Comment(3)
The other way around happens, too - Objective C is dynamic re: method names, but variable declaration rules are those of C.Elude
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?Benevento
I'm not a Scala programmer but my understanding is that Scala is not a dynamic language by most definitions.Nicker

© 2022 - 2024 — McMap. All rights reserved.