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.

Someone who's just getting started in programming asked me about the advantage of different approaches programming languages take.

For example, some allow the programmer to omit variable declarations and just use them (like PHP). Others require declaration but not necessarily with a type. And others require a full declaration of the variable (including its type).

So what's the advantage of each approach? Why is it better to (not) declare a variable and/or its type? The ones that don't require a type I believe allow for more efficiency for the programmer. You can just take a variable and use it rather than think about what type it may have at the moment of needing it. And potentially you can change its type later on.

But is there more to it than that?

share|improve this question
"But is there more to it than that?": Yes. –  Giorgio Aug 23 '12 at 15:52
Thousands of pages have been written on this subject. It is far too broad to be answered usefully here. –  kevin cline Aug 23 '12 at 16:01
add comment

closed as not a real question by gnat, Telastyn, kevin cline, Caleb, GlenH7 Aug 23 '12 at 16:08

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

But is there more to it than that?

Much more.

You can just take a variable and use it rather than think about what type it may have at the moment of needing it.

That's a great recipe for code that nobody understands or can maintain. It may work when you hack up little scripts, but when a project becomes more complex, "just using it rather than thinking about it" stops working.

And potentially you can change its type later on.

Yes. And have the code break in 50 different places that all implicitly assume that it's a certain type and which are pretty much impossible to find before they cause a runtime error (which may happen months after the change for rarely executed code paths). That's OK if you have a comprehensive suite of automated tests. If not, good luck...

Thinking about the type of variables is something you cannot avoid if you want to be a professional programmer instead of a dabbling amateur, whether or not your programming language forces you to do it everywhere.

Dynamically typed programming languages only spare you the work of doing it explicitly everywhere, which can be tedious and lead to unnecessary complexity of its own (Java Generics wildcards, anyone?).

Statically typed programming languages have the great advantage of allowing tools to be very smart. The compiler tells you about type errors. Change a type somewhere and it will tell you about those 50 places that have to change. Want to know all places where method execute of class A is called, and not bother with the 2000 other places the word appears in other classes, comments and documents? In a statically typed language, your IDE can do that.

The disadvantage is that type systems can get very complex when they try to cover every corner case, and force you to deal with the complexity even though those corner cases that necessitate it may never occur in your application.

share|improve this answer
add comment

The main advantage to static typing is that type-based errors can be detected at compile time rather than run time. That can be handy. Suppose you are updating some code written by somebody else using a dynamic language and at first glance it looks like an object is a Foo. You add some code that calls one of the Foo methods on that object (along with a whole bunch of other things. Then you compile it, deploy it and forget about it. A month later someone actually accesses the code you wrote and it blows up with some sort of Type exception.

Now, granted you should have unit tested your code well enough to have seen this coming, but unit tests can be spotty. In a statically typed language, the code wouldn't compile with the typing problem--the compiler tests it for you. In a dynamic language, you need to write the type-based tests yourself.

Of course there are a bunch of advantages with dynamically typed languages too, and there are some efficiency advantages to statically typed languages, but they start down the path of holy war.

share|improve this answer
add comment

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