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.

This came up in a discussion with a friend, and I found myself hard-pressed to think up an any good arguments. What benefits do weak typing confer?

share|improve this question
7  
Cooper and Torczon's Engineering a Compiler defines weak typing as employing a poorly designed type system. That sure doesn't sound like it would benefit anyone. –  Corbin March Jan 19 '11 at 21:42
    
@Corbin March: Nice one. I need to add that to my list. –  Jörg W Mittag Mar 19 '11 at 15:33
add comment

5 Answers

Remember there are two major concepts that are commonly confused:

Dynamic typing

A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. In dynamic typing, values have types but variables do not; that is, a variable can refer to a value of any type.

The advantages here are often dismissed as just for "new" programmers, but can also be convenient for any programmer:

if (!(arr is Array)) arr = [arr]; // is, instanceof, .constructor ==, whatever

Less code in any case where you'd otherwise have to cast or assign a new value:

if (data is Array)) {
    i = data.length; // no i = ((Array)data).length or Array myArr=(Array)data;
}

Loose or weak typing

Weak typing means that a language implicitly converts (or casts) types when used.

Benefits:

  • Pass any type value as a parameter to a function. Useful for callbacks, flexible APIs, and makes for a simpler implementation of closures.
  • Implicit boolean evaluation. Any type can be evaluated as a boolean. This also has side benefits such as a part of an || can be used in assignment without conversion to boolean:

    var a = param || defaultValue;
    
  • Again, less code:

    var num = 5;
    var str = "Hello";
    input.innerHTML = input.value = num;
    for (var i=0; i < input.value; i++) { ... }
    

    Even Java had to go partway, with the implicit call to .toString() when combining objects with a String; otherwise Java programmers would be cursing it all day long (log statements would be out of control).


Both definitions are from http://en.wikipedia.org/wiki/Type_system. It said it better than I could.

share|improve this answer
1  
Dynamic typing is also beneficial where lots of code generation would be required in a statically-typed language. Compare the amount of code generation required for, say, Entity Framework in C# to ORM libraries in dynamically-typed languages (PHP, Python, etc). Though some may argue that the benefits in terms of IDE IntelliSense outweighs the cost of all that code generation... –  Dean Harding Jan 19 '11 at 6:52
3  
Your comment "// no i = ((Array)data).length or Array myArr=(Array)data;" isn't really anything to do with dynamic typing, because the array-ness of data is provable at compile-time. A statically typed language could propagate knowledge obtained from instanceof. –  Peter Taylor Jan 19 '11 at 7:11
    
@Peter Taylor, I suppose that's true in simple cases, do you know of any that do so? It does have to do with dynamic typing in that whether an if block is used or some other more complex (even runtime or dynamic) logic, the next line will be legal and safe from errors. –  NickC Jan 19 '11 at 7:16
    
@Renesis, I don't, no. But the ML family of languages is statically typed and uses type inference so you very rarely have to explicitly state the type of a variable. –  Peter Taylor Jan 19 '11 at 7:26
1  
@Peter Taylor is correct. Many of the benefits of dynamic typing are available in languages with better static typing systems, like Haskell, Scala or even C# in some cases. I would say the major advantage of dynamic typing is dealing with things that are inherently typeless, like HTML DOM. Why write node["attr"] instead of node.attr? They are always resolved at runtime anyway. –  Matt Olenik Jan 19 '11 at 19:41
show 4 more comments

The problem with this kind of discussion is simply that the terms "weak typing" and "strong typing" are undefined, unlike for example the terms "static typing", "dynamic typing", "explicit typing", "implicit typing", "duck typing", "structural typing" or "nominal typing". Heck, even the terms "manifest typing" and "latent typing", which are still open areas of research and discussion are probably better defined.

So, until your friend provides a definition of the term "weak typing" that is stable enough to serve as the basis of a discussion, it doesn't even make sense to answer this question.

Unfortunately, apart from @Renesis, nobody of the answerers bothered to provide their definition either, and you can see the confusion that generates in some of the comments. It's hard to tell, since nobody actually provides their definitions, but I think I count at least three different ones, just on this very page.

Some of the more commonly used definitions are (and yes, I know that pretty much none of them makes any sense, but those are the definitions I've seen people actually use):

  • weak typing = unsafe typing / strong typing = safe typing
  • weak typing = dynamic typing / strong typing = static typing
  • weak typing = duck typing / strong typing = nominal typing
  • weak typing = structural typing / strong typing = nominal typing
  • weak typing = implicit typing / strong typing = explicit typing
  • weak typing = latent typing / strong typing = manifest typing
  • weak typing = no typing / strong typing = typing
  • weak typing = implicit casts / strong typing = only explicit casts
  • weak typing = implicit or explicit casts / strong typing = no casts at all
  • weak typing = implicit conversions / strong typing = only explicit conversions
  • weak typing = implicit or explicit conversions / strong typing = no conversions at all
  • weak typing = interpretation / strong typing = compilation
  • weak typing = slow / strong typing = fast
  • weak typing = garbage collection / strong typing = manual memory management
  • weak typing = manual memory management / strong typing = garbage collection
  • … and many others

The three definitions that seem to be used most widely, however, are

  • weak typing = your stupid crappy programming language / strong typing = my super-awesome programming language
  • weak typing = every other programming language / strong typing = the only programming language I ever bothered to learn (usually either Java, C# or C++; strangely, people who learn e.g. Haskell or Scheme as their first and only language don't seem to share this worldview)
  • weak typing = every language I don't understand / strong typing = Java (substitute with C# or C++ at will)

Unless everybody agrees on a definition of what "weak typing" even is, it doesn't even make sense to think about what its advantages might be. Advantages of what? Even worse, if there is no definition at all, then everybody can just shift their definitions to fit their arguments, and every discussion is pretty much guaranteed to devolve into a flamewar.

I myself have personally changed my own definition several times over the years and have now reached the point where I don't even consider the terms useful any more. I also used to think that weak typing (in its various definitions) has a place in shell scripting, but whenever I have to solve the same problem in Bash and PowerShell, I am painfully reminded how wrong I was.

share|improve this answer
1  
Pretty cynical answer! I get where you're coming from but I feel it's "good enough" to guess that anyone who doesn't provide a definition is referring to "weak and/or dynamic typing", which is why I included both in my answer. I know it's not perfect, but most people seem to tune out when it comes to defining type systems. –  NickC Jan 19 '11 at 21:37
    
I don't consider wikipedia a perfect source, but it acknowledges the mess in Strong Typing: "Note that some of these definitions are contradictory, others are merely orthogonal, and still others are special cases..." and Weak Typing: "...opposite of strong typing, and consequently (...) has as many different meanings..." –  Corbin March Jan 19 '11 at 21:51
4  
@Renesis: I'd call it "realism" :-) I have seen enough discussions about type systems to realize that half the people don't realize they are talking about completely different things and the other don't know what they are talking about at all. A couple of months ago, there was a discussion on the core development list of a language I won't name, about adding an optional type system to improve performance. That discussion went on for a week, involved dozens of people and hundreds of mails. Not one realized that an optional type system by definition can't improve performance. … –  Jörg W Mittag Jan 19 '11 at 21:52
1  
+1 for cynism! You might add "strong typing=when the IDE knows the types of my variables (Intellisense etc.), weak typing=when it doesn't)" –  user281377 Aug 21 '12 at 7:32
1  
The better-defined names for typing systems lack the implicit value judgement weak and strong have. –  Eva Aug 23 '13 at 10:08
show 1 more comment

The main argument for weak typing is one of performance. (this is to answer the OPs question as stated). There's a lot of good discussion about dynamic vs. static, implicit vs. explicit. etc.

C is the most famous weakly typed language, and it does not perform any run time checking or compile time checking of the variables type. In essence you can cast a char * to an int * and the language wouldn't care. So why would you do this?

C programming is pretty close to the way you would do things with assembly, so there are times where you only care about an address. It's not uncommon to cast or pass a void * reference for that very reason. If you know how the memory is organized (again a C and assembly concern), you can do some pretty cool calculations based on the address in the void * to get at the information you need. This can let you short-circuit the process you would have to go through in Java for instance.

While run-time type checking doesn't have an extraordinary bit of overhead, there are times when it is just enough to cause a critical section to be too slow. I'm thinking mostly about embedded programming and real time systems in this case.

That said, in most cases having a strong type system that is either compile time checked or runtime checked helps more often than it hurts.

share|improve this answer
1  
I don't see why a strongly-typed language can't compile as efficiently as C. But the compiler should be vastly more complex. –  9000 Jan 19 '11 at 22:10
2  
C++ is an example of one such language. In C++ all type checking is done at compile time, and none are done at run time... UNLESS you have RTTI enabled. –  Berin Loritsch Jan 20 '11 at 1:40
2  
Not sure I agree with this performance argument, but it is an interesting view. –  Martin Ba Aug 21 '12 at 7:18
add comment

Weak typing is typically easier for newbies to grasp, for instance in things like excel,javascript and vbscript. You also trade some development speed for potential errors.

Good article on the subject: Strong typing vs Strong testing

share|improve this answer
1  
I don't think there's a strong correspondence between weakly typed languages and newbie friendly languages. Ruby and python are strongly typed and generally considered newbie friendly. C is weakly typed and generally considered newbie hostile. –  sepp2k Jan 19 '11 at 6:46
    
No, not the language in general, I was just talking about the typing –  konrad Jan 19 '11 at 6:50
2  
Dynamic and strongly typed are the newbie friendly combination. Although I have seen that when people move from a statically typed language to a dynamically typed one, they also get really confused. I've never seen someone who started with dynamically typed though since the schools end up teaching C or Java first most of the time. –  jsternberg Jan 19 '11 at 7:28
3  
@Mchl: that doesn't have anything to do with either static/dynamic or strong/weak typing. Having to declare types is about explicit/implicit typing. –  Jörg W Mittag Jan 19 '11 at 20:51
1  
Might just as well be ;) So I moved also from explicitly to implicitly typed language. Correct me if I'm wrong Pascal is statically, strongly and explicitly typed, while PHP is dynamically, weakly and implicitly typed, aren't they? –  Mchl Jan 19 '11 at 20:54
show 2 more comments

How much does it cost for a month to hire average developer? Probably around $2000-$10000 /month.

State of the art server = $50-$100/month. So who cares if the code will run slower when you will be able to write much more in much less time?

Strong typing is incompatible with rapid changes and tight schedules.

and I found myself hard-pressed to think up an any good arguments

You should get on the web sometimes :) SQL, JS, PHP, XML, HTML, perl... all weak typing.

Besides... checking the typing, freeing objects, etc? That's monkey coding - and to be honest you need that only on the clientside, when you write some big app used by thousands of people. Otherwise... let the compiler do this for you. And you can always code some very small part in C when you need speed...

share|improve this answer
7  
"Besides... checking the typing, [...]? That's monkey coding - let the compiler do this for you." Ehrm isn't that an argument against weak typing? If a language is weakly typed neither the compiler nor the run time system check the types, so you have to do it yourself if you want it to be done at all. –  sepp2k Jan 19 '11 at 5:11
2  
How does a compiler free an object? –  Pemdas Jan 19 '11 at 5:12
3  
Execution speed has little to nothing to do with type safety. C is weakly typed and hardly slow. How does strong typing slow down the programmer? It should increase productivity by catching errors sooner rather than later. It's easy to have an accidental type conversion in JS or PHP go unnoticed. Also, dynamic typing is not the same as weak typing. –  Matt Olenik Jan 19 '11 at 5:41
6  
XML, HTML? Since when these are even programming languages? –  Mchl Jan 19 '11 at 10:40
3  
It's certainly arguable that dynamic typing helps with changing requirements and tight deadlines, but dynamic vs. static typing is orthogonal to strong vs. weak typing. –  David Thornley Jan 19 '11 at 14:48
show 3 more comments

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.