From: John Carter Date: 2001-12-11T06:17:26+09:00 Subject: [ruby-talk:28137] Re: The benefits of dynamic typing? On Tue, 11 Dec 2001, Roy Patrick Tan wrote quoting Wirth: > "Design languages such that most checking operations can be performed at > compile time, and need not be deferred until execution. The concept of > static data types of variables is essential in this respect, and enhance > both programming security and system efficiency." You must also remember where Wirth was coming from. Come gather round Grandfather and he will tell you of the bad old days... In the bad old days of Fortran you could swap arguments in a function call and the compiler would merrily accept it and run your program and create, _shlock_ _horror_ UTTER GARBAGE. All burning horridly expensive heavy metal machine cycles too! They had things called COMMON BLOCKS. Delightful. What you used in FORTRAN instead of globals. In fact, if you look very carefully when you link a C or C++ program, your globals and static _are_ stored by the linker in (NOooo! DREAD!) common blocks. Now you could say things like... COMMON FRED,JIM in the main routine and COMMON JIM,FRED in the subroutine... (Note the seven spaces before the C, that was vitally important, it matters not now, but it did then... (That is why Python gives me the irrational shudders, even though I see some sense in it.)) Joy! It compiled! Sorrow! It produced utter crap. Because JIM now occupied the exact same physical bits in memory in the subroutine as FRED did in the main program. It gets worse. Now remember, GOD is real unless declared integer. By default every variable starting with an A-G and Q-Z (I think, horror is fading from my mind) was REAL*8 a floating point number. Conversely every variable starting with I,J,K,L,M,N,O,P was an integer. Unless you explicitly declared it otherwise. Hence the old FORTRAN joke that GOD is real unless declared integer. Now even if you got the order of the variable names in the common block right, if you fouled up the declarations the compiler would happily stuff an integer into 32 bits and with equal glee treat it as a floating point number. Producing UTTER DISMAL HOGSWASH. Now do you see why Wirth was hailed as a conquering hero? Curiously enough in Wirth's day there was a poster child dynamic language that had been around since he was an ankle biter. Lisp. But in those days it was _slooow_. These days Moore's law has done some lovely things, and Lisp compilers and modern techniques have done even better things. But in those days, Lisp was slow and burned very expensive machine time. The final points to note are... * Early Lisp didn't have an object system. I think its the pure object system of the SmallTalk's and the Rubies of the world that really make them viable for large codes. * The idea of Unit testing replaces static typing. It took me awhile to grasp that one. Thanks to a post by Ron Jeffries that I finally did. Static typing is like saying all over your code, "this is this type", "this is that type". In hundreds and hundreds of places. What you really like to say is "this works like that". You can do that with a unit test. Now a statically typed program with unit tests would be faster and more bug free than a dynamic program with unit tests only. Except for one thing. Imagine if every time you went into a room you had to formally state, the colour of the roof, floor and walls, and announce the exact height and width of the door. Pretty soon you would find all sorts of excuses to sit in your cubicle rather than going around painfully muttering inane things. Not to mention if the toilet was four doors away you might not get there on time... A statically type language slows you down when writing, and then slows you down when refactoring or making changes. (You then have to go back through the entire code saying this wall is now blue, this wall is now blue, this wall is now blue, whoopsy, thats a different wall its still a red type wall, this wall is red, this wall is now blue....) Thus although static typing saves you from the horrors of ye olde FORTRAN, so does dynamic typing. It also inserts mounds of crusty assertions in your code about things you don't really care about, but make your code more inflexible. Dynamic typing is slower than static typing, but the first two orders of magnitude in speed of a code is in the choice of algorithm! If dynamic typing gives you the time to write a better algorithm, then a dynamic language will be faster than a static one, John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand Zeitgeist shutting down for corrective maintenance, please log off.