views:

1758

answers:

4

Can someone tell me what Strong typing and weak typing means and which one is better?

A: 

Weak typing means that you don't specify what type a variable is, and strong typing means you give a strict type to each variable.

Each has its advantages, with weak typing (or dynamic typing, as it is often called), being more flexible and requiring less code from the programmer. Strong typing, on the other hand, requires more work from the developer, but in return it can alert you of many mistakes when compiling your code, before you run it. Dynamic typing may delay the discovery of these simple problems until the code is executed.

Depending on the task at hand, weak typing may be better than strong typing, or vice versa, but it is mostly a matter of taste. Weak typing is commonly used in scripting languages, while strong typing is used in most compiled languages.

wvdschel
-1 for confusing weak typing and dynamic typing.
Sean McMillan
+6  A: 

"Strong typing" and its opposite "weak typing" are rather weak in meaning, partly since the notion of what is considered to be "strong" can vary depending on whom you ask. E.g. C has been been called both "strongly typed" and "weakly typed" by different authors, it really depends on what you compare it to.

Generally a type system should be considered stronger if it can express the same constraints as another and more. Quite often two type systems are not be comparable, though -- one might have features the other lacks and vice versa. Any discussion of relative strengths is then up to personal taste.

Having a stronger type system means that either the compiler or the runtime will report more errors, which is usually a good thing, although it might come at the cost of having to provide more type information manually, which might be considered effort not worthwhile. I would claim "strong typing" is generally better, but you have to look at the cost.

It's also important to realize that "strongly typed" is often incorrectly used instead of "statically typed" or even "manifest typed". "Statically typed" means that there are type checks at compile-time, "manifest typed" means that the types are declared explicitly. Manifest-typing is probably the best known way of making a type system stronger (think Java), but you can add strength by other means such as type-inference.

Peter Becker
+10  A: 

That'll be the theory answers taken care of, but the practice side seems to have been neglected...

Strong-typing means that you can't use one type of variable where another is expected (or have restrictions to doing so). Weak-typing means you can mix different types. In PHP for example, you can mix numbers and strings and PHP won't complain because it is a weakly-typed language.

$message = "You are visitor number ".$count;

If it was strongly typed, you'd have to convert $count from an integer to a string, usually with either with casting:

$message = "you are visitor number ".(string)$count;

...or a function:

$message = "you are visitor number ".strval($count);

As for which is better, that's subjective. Advocates of strong-typing will tell you that it will help you to avoid some bugs and/or errors and help communicate the purpose of a variable etc. They'll also tell you that advocates of weak-typing will call strong-typing "unnecessary language fluff that is rendered pointless by common sense", or something similar. As a card-carrying member of the weak-typing group, I'd have to say that they've got my number... but I have theirs too, and I can put it in a string :)

JoeBloggs
"and *I* can put it in a string" nice. +1 for the weak side of the force
annakata
"Strong-typing means that you can't use one type of variable where another is expected".Strong/weak aren't about variables, they are about values. I think you should say:"Strong-typing means that you can't use one type of *value* where another is expected".
Paul Biggar
You are confusing *static typing* and *strong typing*. "Communicating the purpose of a variable" is *static typing*. "Unnecessary language fluff" is *static typing*.Avoiding bugs is largely *static typing*, but strong typing also helps here, so half-marks.
Paul Biggar
@Paul: I would argue that "communicating the purpose of a variable" is *good naming*. A variable named `interest_rate` (or `interestRate` or `$interest_rate` or however you want to capitalize/space it) communicates its purpose quite clearly, regardless of whether it is strongly- or weakly-typed, static or dynamic.
Dave Sherohman
+3  A: 

I would like to reiterate that weak typing is not the same as dynamic typing.

This is a rather well written article on the subject and I would definitely recommend giving it a read if you are unsure about the differences between strong, weak, static and dynamic type systems. It details the differences much better than can be expected in a short answer, and has some very enlightening examples.

http://en.wikipedia.org/wiki/Type_system

Greg Sexton