44

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?

3
  • 18
    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. Jan 19, 2011 at 21:42
  • @Corbin March: Nice one. I need to add that to my list. Mar 19, 2011 at 15:33
  • 7
    The best argument can be given by corporate executives: it allows me to hire cheap people to build my systems
    – Vector
    May 31, 2014 at 0:25

4 Answers 4

50

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 Nick's answer, 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.

15
  • 7
    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.
    – Nicole
    Jan 19, 2011 at 21:37
  • 7
    @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. … Jan 19, 2011 at 21:52
  • 9
    +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, 2012 at 7:32
  • 1
    The better-defined names for typing systems lack the implicit value judgement weak and strong have.
    – Eva
    Aug 23, 2013 at 10:08
  • 1
    Gorgeous, entertaining, and accurate. The more I've learned about type systems, the more I've realized how long I suffered under the illusion that one language was different from another in ways it just wasn't at all. It's amazing just how much confusion this concept causes, and I really think it isn't necessary if only we accept that weak/strong isn't a real thing, so let's talk about something that actually is and maybe we'll learn something.
    – BrianH
    Jan 2, 2015 at 17:43
30

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.

9
  • 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... Jan 19, 2011 at 6:52
  • 4
    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. Jan 19, 2011 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.
    – Nicole
    Jan 19, 2011 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. Jan 19, 2011 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. Jan 19, 2011 at 19:41
9

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.

3
  • 3
    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, 2011 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. Jan 20, 2011 at 1:40
  • 2
    Not sure I agree with this performance argument, but it is an interesting view.
    – Martin Ba
    Aug 21, 2012 at 7:18
3

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

8
  • 2
    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, 2011 at 6:46
  • No, not the language in general, I was just talking about the typing
    – Homde
    Jan 19, 2011 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, 2011 at 7:28
  • 4
    @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. Jan 19, 2011 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, 2011 at 20:54

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