SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Strong vs. Loose Typing

    Now here's a debate we haven't explored (could be as hot as PHP vs. NET ).

    Languages like Java, C++ and C# using strong typing. That means every variable, function and class you define must come with some description of what it is, what date types it can accept and what it returns.

    So if you have a variable called "myNumber", you must declare what type of variable it is e.g.;

    Code:
    int myNumber = 321;
    Languages like PHP, Perl and Python are loosely typed, so you don't need to bother - the language interpreter handles all these problems for you. So in PHP you can just have;

    PHP Code:
    $myNumber 321
    You don't even need to declare the variable beforehand.

    Personally I like loosely typed languages - their syntax is generally friendlier, allowing you to concentrate on solving problems rather than endless definition.

    I'd also argue that loose typing is the future while strong typing is the past, as these days loose coupling of systems (tying two systems together in a flexible fashion, so it's easy to change either without breaking the connection), particularily on the Internet, is important.

    What's brought this up in my mind again recently is looking at WSDL, the XML format for describing web services. Conceptually WSDL is like the output from tools like javadoc and phpdoc - a documentation of an API (or service in SOAP terms). While javadoc can spit out documentation like this from your code, WSDL documentation looks like this: http://api.google.com/GoogleSearch.wsdl and can be used by client to "instantly" implement the service (there's a good PHP example here).

    Unfornunately, WSDL requires you define the data types your service can receive and return. Some even argue this was a conspiracy by IBM and MS to help Java and .NET (see http://www.soapware.org/discuss/msgReader$21?mode=topic and http://groups.yahoo.com/group/soap-newbies/message/138 ).

    Anyway - strongly typed languages... dead?

  2. #2
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I quite liked strong typed languages because they teach to to type ti "set guidlines" which often makes your code + coding style better. And with the languages you mentioned, a quick compile and all of the errors are displayed so you can fix them all in one hit.

    loose languages are pretty good, but sometimes you get sloppy with them

  3. #3
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Strong vs. Loose Typing

    Originally posted by HarryF
    ..
    I'd also argue that loose typing is the future while strong typing is the past, as these days loose coupling of systems ...
    Contrary; with loose coupling you need the strong typing

    If you write a function, and the only person using it is you, then you probably know that the parameter $myNumber is an int, and you can use loose typing
    I don't think this latter situation is the future

  4. #4
    SitePoint Addict
    Join Date
    Feb 2001
    Posts
    302
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You seemed to have left out a couple of advantages of strongly typed languages, when you are developing large applications with thousands or even millions of lines of code and working with other developers, it is pretty useful to be able to instantly see what type a variable or object is and to be able to enforce that type, to stop people accidentally changing it.

    Another big advantage of strongly typed languages is debugging, errors are detected at either compile or run time, where as with PHP for example I could have an error, which still lets the application run, yet causes unexpected results, I would then have to take time searching through the code to try and find the cause.

    The fact that WSDL requires you to define the datatypes is hardly a conspiracy, the vast majority of web services are / will be written in languages that require defining of datatypes, it's simple common sense.

    Yes the syntax of some loosely typed languages may be friendlier (or easier), but then the loosely typed languages are not designed to do everything that most of the strongly typed languages are used for. (saying that the syntax for perl is definetly not friendly)

    You say strongly typed languages are dead, yet it is strongly typed languages that are used by the vast majority of programmers in their jobs today and if loosely typed languages were so wonderful micosoft would of opted for a loosely typed language when designing C#.

  5. #5
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One quick note, before I get lynched for "strongly typed are dead" - obviously the most popular langauges these days are strongly typed (but perhaps only because they've mostly been around longer).

    Contrary; with loose coupling you need the strong typing
    How about this. I get a variable $page from a third party and use it like this in a PHP script;

    PHP Code:
    function redirect ($page) {
        
    header "Location: http://www.thirdparty.com/"
                 
    .$page.".html" );

    Originally the third party uses integers for the $page values it gives me (resulting in 1.html, 2.html etc). But then one day they switch to strings instead (perhaps july.html, august.html etc.). Now if in Java say I'd started by writing that function to expect an integer value, I'd now need to re-write the script, while PHP can survive the change (in this case at least).

    Some interesting reads on loose coupling;

    http://www.fawcette.com/xmlmag/2002_...tments/endtag/
    http://radio.weblogs.com/0100812/sto...eCoupling.html
    http://lists.w3.org/Archives/Public/...2Aug/0235.html

    And of course everyone knows Nogg's postulate ));

    "Any system that depends on reliability is unreliable."
    On other thing;

    The fact that WSDL requires you to define the datatypes is hardly a conspiracy, the vast majority of web services are / will be written in languages that require defining of datatypes, it's simple common sense.
    But it could be done another way. Rather requiring the API documentation defines the data types, the strongly typed client could simply perform some kind of reflection on the data in receives and convert it into local types.

  6. #6
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by HarryF
    ...Originally the third party uses integers for the $page values it gives me (resulting in 1.html, 2.html etc). But then one day they switch to strings instead (perhaps july.html, august.html etc.). Now if in Java say I'd started by writing that function to expect an integer value, I'd now need to re-write the script, while PHP can survive the change (in this case at least).
    No, in java you would write a function that expected a string, since the only meaningful interpretation of the data is as a string; you concatenate it with "Location: http://www.thirdparty.com/" and ".html"

    Originally posted by HarryF
    ...the strongly typed client could simply perform some kind of reflection on the data in receives and convert it into local types
    How? If I send 1900 to you, how would you reflect on that? If the next value you receive is "Little Buddha", you begin to realize that I'm sending you Bernardo Bertolucci movie titles, and that the first value was a string, not a number...

    Originally posted by HarryF
    ...obviously the most popular langauges these days are strongly typed
    VB6 was (is?) quite popular, and "Option Explicit" wasn't the default

  7. #7
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    obviously the most popular langauges these days are strongly typed (but perhaps only because they've mostly been around longer).
    Edit: Woops, just realized you said 'strongly' and not 'loosely'.. Wow, and it's only 8 pm here

    Can you name a few? I don't know a lot of languages, but I do know that the most popular languages for desktop applications - if popular means most used - these days are C(++) and Java, both of which are strongly typed languages.

    But by 'popular' you're probably talking about scripting languages. The primary purpose of those languages is to accomplish simple tasks in a quick and easy way. PHP for example is after all a scripting language (not saying it can't be used for more than scripting): it's goal is to make web development easier. If it would introduce typed variables things would get a lot more complex.

    However, I think strongly typed languages have great advantages when you are using them to develop complex applications. First of all, like neil100 said, a lot of errors/bugs are detected at compile time by the compiler.

    If you call a function with a wrong type of parameter, in a loosely typed language this would lead to unexpected results most of the time. But in a stronly typed language the compiler detects this kind of errors.

    Second: performance. Because the compiler knows what type your variables are, it knows how much memory to allocate for your variables. This makes more efficient use of memory than loosely typed variables.

    To go on about PHP, even if it were possibly to truely compile a program into a stand-alone executable file, it would be much slower than the same program written in something like C. This is because of the overhead of loose typing.

    But then again I'm no expert, I'm just summing up a few things I read on the net.

    Originally the third party uses integers for the $page values it gives me (resulting in 1.html, 2.html etc). But then one day they switch to strings instead (perhaps july.html, august.html etc.). Now if in Java say I'd started by writing that function to expect an integer value, I'd now need to re-write the script, while PHP can survive the change (in this case at least).
    Just pointing out that an integer is a number, but a number doesn't have to be an integer. You can have a string '1' as well as the integer 1. So in that case you wouldn't have to rewrite your stronly typed function because it would be given a string '1'.

    In a language like Java, you could do something like this if the function would need to accept an integer anyway:

    Code:
    /* I've never written a line in Java, but I'm sure you'll get the point :) */
    void function redirect (string page) {
        header ( "Location: " + page + ".html");
    }
    
    void function redirect (int page) {
        redirect((string) page);
    }
    I think this is called overloading.

    Personally, I would like PHP to be strongly typed cause most of my functions (methods) accept and return parameters that are of a fixed type. It would save me time checking for bugs related to loosely typed variables.
    Last edited by Captain Proton; Oct 26, 2002 at 09:26.

  8. #8
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    I like loosely typed languages as well as strongly typed ones....

    With loosely typed languages the syntax is much friendlier. However, you are usually limited in what you can do (IE, no systems programming with PHP ).

    Strongly typed languages are strongly typed to force the user to watch their use of memory, operators, etc. In my opinion it is there to keep the programmer in check.

    I saw some examples of C# being used while I was in Redmond and it is extremely strongly typed (note to self, gotta get those .NET notes out)

  9. #9
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >>>strongly typed languages... dead?
    No.
    Actually the only choice for programming an 8bit micronctroller with 32kb of rom and 512byte or 1kb of ram is C or assembler...
    I remember some people using Pascal but it was many years ago ( before 1990 ).
    Some compiler are trying to implement EC++ which is a version of C++ for embedded programming that "will consume less memory".

    So my answer is no for embedded systems.



    pippo
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •