Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I come from a PHP background and I have been considering looking at DART in more detail.

DART is an, open source, front-end/back-end, language for web development.

PHP has a special $ (dollar) character to indicate variables:

$variable
CONSTANT
'string'
"etc"

I find that dollar sign to be really useful for speeding up the proces of refactoring, especially if PHP code is mixed with HTML or content. It reduces the number of false positives when, say, doing a find and replace.

As DART is a completely fresh language (with no legacy) that can be embedded in HTML, it surprised me to see that it does not have any special identifier to indicate variables.

What's the reasoning behind that design decision?

share|improve this question
    
"Not necessary". Could you expand on that? Do you mean 'not necessary' to: (a) function, (b) 'to understand', (c) 'to automatically refactor' (d) perform static analysis? etc .Does a language only have to include necessary things? I do not know much about the theory of language design. Which is why i ask the question. –  JW01 Jan 24 '12 at 10:08
    
You might want to ask the people behind Dart at groups.google.com/a/dartlang.org/group/misc/topics?pli=1 Many languages dont have special characters to indicate variables. Marking a variable by a leading character is neither common nor uncommon. In the case of Dart there might not be an IDE that would offer refactoring (never worked with it, so i couldn't say). For other languages like java or c/c++ who don't use variable name marking, the corresponding IDE's usually offer the functionality to easily refactor variable-/function-/class-/struct-/...names –  Ingo Jan 24 '12 at 10:25
    
@Ingo It was easier to ask here (I am habituated to this interface). But, yes. I'll probably join the group at some point soon. I've never done any heavy refactoring of Java or c/c++ but always wondered how one copes without the leading variable name marking. I find it almost essential for my sanity. –  JW01 Jan 24 '12 at 11:02
4  
"What's the reasoning behind that design decision?" - It's a bit strange to ask this, as if there was a conscious design decision to not have variable names start with a special character. Most languages don't require a special character. –  Jesper Jan 25 '12 at 13:16
4  
That's generally referred to as a sigil and only a few languages use them any more. –  jiggy Jan 31 '12 at 4:36

4 Answers 4

up vote 6 down vote accepted

Actually you can use $ as a a prefix to variable names.

I tried it for you: http://try.dartlang.org/s/lKco

Please note what happens to the great string interpolation feature. Basically you can access vars in a string with ${yourvar}. If you name a variable with $, then it looks ugly: ${$yourvar}. Therefore I do not recommend the $prefix style.

I have started with PHP too. But I found out soon as I switched to Java, that $ is not really common usus. What is the sense behind - the PHP developer would probably say, easy identification of variables. The Java developer would cry in pain.

The Dart developers try to make an "unexciting" language. It should look familiar to many. Without doubt Java syntax is more widespread than PHP syntax. Maybe this is the reason why Dart developers have left out the $.

On the other hand, personally speaking, I think it is really not necessary and after getting used to Java, I find it ugly. Ruby doesn't use it. JS doesn't use it. And so on.

Anyway here are more answers to the language: http://www.dartlang.org/support/faq.html#language

If you want to follow the recent blogs on Dart I recommend you: http://www.dartosphere.org

share|improve this answer
    
Thanks for the answer. Just out of interest, when you switched from PHP to Java did you have to make any changes to the way you do find and replaces of variable names? Do you rely more on a specific tool? I guess with good naming conventions, my worries would be unfounded. –  JW01 Jan 24 '12 at 11:06
    
With Java there are some cool tools in place. For example, you can use Eclipse Refactoring features to rename a variable (works with most other editors too). You just say: rename this, give it a new name and then all references to this name are updated. I never do the "find/replace" way I needed with PHP. This was a huge improvement to me. In Dart you can have a Dart Editor based on Eclipse. Not sure if does refactoring of this style too, but for sure there are good chances. –  Christian Jan 24 '12 at 11:32

The reason why most language designers don't like constructs like starting variables with special chars is that most language designers want to minimize the amount of repetitive "stuff" that is required of the programmer, so long as the compiler / interpreter can figure it out. So, to many language designers, the code:

var x = new Something()

has less cruft than:

Something x = new Something(); 

("Why repeat the type specification? The compiler can infer it. Why have the semicolon? The compiler can figure it out.") which in turn has less cruft than:

Something $x = new Something(); 

("Why make every variable start with a '$'? The compiler knows that the token at that position must be a variable name")

This design aesthetic is something that most language designers would probably agree with in principal, but of course beauty is in the eye of the beholder. The "inferred semicolons" in JavaScript, for instance, can lead to surprising behavior.

Your opinion that there's value in quickly identifying variables by their $ is perfectly valid, but another person might say "Well, the only real semantic benefit is that you can have variables named similarly to keywords ($if) and that's a dubious benefit." In the specific case of refactoring, in languages with tighter type semantics, many refactorings can actually be done in not just a quick, but "guaranteed safe" way, since the refactoring is not being done on "just some string" but on a very specific element in the parse tree.

share|improve this answer
    
The semicolon is a really bad example. Javascript is an excellent example of how the compiler can't figure it out. It is significantly difficult to cut the semicolon from a C-derived language. –  DeadMG Jan 31 '12 at 1:19
    
@DeadMG Well, that was kind of my point: the aesthetic drives lang designers to think "Oh, we can infer that" and the reality is that inferring semicolons in C-derived languages is tricky (see also Scala) –  Larry OBrien Jan 31 '12 at 1:22
    
@DeadMG, it's actually pretty straightforward to make semicolons optional in a C-derived language. JS just has a particularly awful way of doing it. –  munificent Feb 10 '12 at 1:18

Dart doesn't use $ to denote variables because that is not familiar to JavaScript or Java developers. One of Dart's design goals is to be familiar to a vast majority of developers, which is why it looks kind of like C, Java, and JavaScript.

share|improve this answer

Very few languages use special characters to denote variables. Perl and PHP are the only ones which are commonly used that do; the value that either derives from it is dubious; nobody complains that Python, for example, is more opaque than PHP. Note that many languages have solutions which do allow easy substitution of variables in strings, which is the only place where a variable indicator is really useful:

Python

example = "The capital of {0} is {1}.".format(state, capital)

Scala

val example = "The capital of %s is %s.".format(state, capital)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.