Search code examples
weak-typing

When should weak types be discouraged?


When should weak types be discouraged? Are weak types discouraged in big projects? If the left side is strongly typed like the following would that be an exception to the rule?

   int i = 5
   string sz = i
   sz = sz + "1"
   i  = sz

Does any languages support similar syntax to the above? Tell me more about pros and cons to weak types and situations related.


Solution

  • I think you are confusing "weak typing" with "dynamic typing".

    The term "weak typing" means "not strongly typed", which means that the value of a memory location is allowed to vary from what it's type indicates it should be.

    C is an example of a weakly typed language. It allows code like this to be written:

    typedef struct
    {
        int x;
        int y;
    
    } FooBar;
    
    FooBar foo;
    char * pStr = &foo;
    pStr[0] = 'H';
    pStr[1] = 'i';
    pStr[2] = '\0';
    

    That is, it allows a FooBar instance to be treated as if it was an array of characters.

    In a strongly typed language, that would not be allowed. Either a compiler error would be generated, or a run time exception would be thrown, but never, at any time, would a FooBar memory address contain data that was not a valid FooBar.

    C#, Java, Lisp, Java Script, and Ruby are examples of languages where this type of thing would not be allowed. They are strongly typed.

    Some of those languages are "statically typed", which means that variable types are assigned at compile time, and some are "dynamically typed", which means that variable types are not known until runtime. "Static vs Dynamic" and "Weak vs Strong" are orthogonal issues. For example, Lisp is a "strong dynamically typed" language, whereas "C" is a "weak statically typed language".

    Also, as others have pointed out, there is a distinction between "inferred types" and types specified by the programmer. The "var" keyword in C# is an example of type inference. However, it's still a statically typed construct because the compiler infers the type of a variable at compile time, rather than at runtime.

    So, what your question really is asking is:

    What are the relative merits and drawbacks of static typing, dynamic typing, weak typing, stong typing, inferred static types, and user specified static types.

    I provide answers to all of these below:

    Static typing

    Static typing has 3 primary benefits:

    1. Better tooling support
    2. A Reduced likely hood of certain types of bugs
    3. Performance

    The user experience and accuracy of things like intellisence, and refactoring is improved greatly in a statically typed language because of the extra information that the static types provide. If you type "a." in a code editor and "a" has a static type then the compiler knows everything that could legally come after the "." and can thus show you an accurate completion list. It's possible to support some scenarios in a dynamically typed language, but they are much more limited.

    Also, in a program without compiler errors a refactoring tool can identify every place a particular method, variable, or type is used. It's not possible to do that in a dynamically typed language.

    The second benefit is somewhat controversial. Proponents of statically typed languages like to make that claim. Opponents of statically typed languages, however, contend that the bugs they catch are trivial, and that they would get caught by testing anyways. But, you do get notification of things like misspelled variable or method names up front, which can be helpful.

    Statically typed languages also enable better "data flow analysis", which when combined with things like Microsoft's SAL (or similar tools) can help find potential security problems.

    Finally, with static typing, compilers can do a lot more optimization, and so can produce faster code.

    Drawbacks:

    The main drawback for static typing is that it restricts the things you can do. You can write programs in dynamically typed languages that you can't write in statically typed languages. Ruby on Rails is a good example of this.

    Dynamic Typing

    The big advantage of dynamic typing is that it's much more powerful than static typing. You can do a lot of really cool stuff with it.

    Another one is that it requires less typing. You don't have to specify types all over the place.

    Drawbacks:

    Dynamic typing has 2 main draw backs:

    1. You don't get as much "hand holding" from the compiler or IDE
    2. It's not suitable for critical performance scenarios. For example, no one writes OS Kernels in Ruby.

    Strong typing:

    The biggest benefit of strong typing is security. Enforcing strong typing usually requires some type of runtime support. If a program can proove type safety then a lot of security issues, such as buffer overuns, just go away.

    Weak typing:

    The big drawback of strong typing, and the big benefit of weak typing, is performance.

    When you can access memory any way you like, you can write faster code. For example a database can swap objects out to disk just by writing out their raw bytes, and not needing to resort to things like "ISerializable" interfaces. A video game can throw away all the data associated with one level by just running a single free on a large buffer, rather than running destructors for many small objects.

    Being able to do those things requires weak typing.

    Type inference

    Type inference allows a lot of the benefits of static typing without requiring as much typing.

    User specified types

    Some people just don't like type inference because they like to be explicit. This is more of a style thing.