Quoted from the PHP manual comment, someone says:
12-Sep-2005 06:17 |
Be ULTRA careful when multiplying numbers like '1,000'. If it's a string with a thousands separator like that, PHP ignores everything after the comma.
echo "2,500" * 2; // displays 4
echo "20,50" * 2;
echo "2,500.00" * 2; // displays 4
is_numeric("2,500"); // false?>
I was developing a shopping cart, and had items worth over $1,000 advertised as $1! Luckily we realised before launch.
With weak, loose, and or dynamic typing, there are errors that slip through like this. Humans make these mistakes. Advanced programmers make these mistakes.
With a strongly statically typed language (not php), the compiler or run time conversion catches these errors for you. Or your Val() and Str() or Itoa() functions make it clear what you are doing explicitly. You don't just assume that a comma will work, you are informed by the compiler or converter function that you made a boo boo. i.e. strong typing and safety.
Strong and static typing helps you learn and master your language, when you are exploring new areas of your language that you have not tested out before. Strong and static typing allows you to worry less about these stupid mistakes since you are more forced to catch these stupid mistakes.
Dynamic/weak typed language advocates will argue that you can simply write more unit tests to make programs robust and secure from these sorts of errors. Well, if you are smart enough to write the unit test to check your type mistakes, you wouldn't have written the bad code in the first place since you would have already known about the comma issue. This is a chicken and egg problem. The compiler and strong typing languages solve the chicken and egg problem.. whereas the dynamic/weak typed language advocates don't understand the chicken and egg problem.
Think about it for a minute. If you are smart enough to write a unit test to catch errors, then you wouldn't have written the effing error in the code in the first place. Generally tests show the presence of bugs after you've already had the bug infect your code and cause your shopping cart item at $1000 to be sold for $1.00.
You should be focusing on programming, not writing unit tests that reinvent strong and static typing. Yes, many unit tests that people write are reinventing a compiler and runtime language checks. Better spend your time writing unit tests that don't reinvent strong typing. Unit tests are not all bad (formal verification and proving is extremely difficult and virtually impossible), but using unit tests to reinvent a compiler and reinvent runtime checks, is a waste of time.
It is impossible for even the most advanced programmer to know everything about his language. The argument that comes from weak/dynamic type advocates is that all programmers should know every little thing about their language - like whether or not PHP decides to chop off everything after the comma? Maybe in some other situation it trims the comma rather than chopping everything off.. and you end up with a different result in a different situation.
The reality: the human being cannot hold all information about his language in his head, even if he is a master of his programming language. If a comma is no good, the compiler or run time conversion check bitches at you, and you fix the problem immediately. In a weak typing language, the error may go unnoticed for several days or months.
Strong Typing Blows!
And the disadvantage of strong typing? It takes a bit more extra keystrokes. Advocates of weak typing proclaim that strong typing "is too much work for them because it takes more typing". Well, that is true laziness. A weak typing programmer should be doing his own run time checks for bugs like this, but he is too lazy to since he has more important code to write.
And couldn't the weaker type language programmers just write more unit tests? Laziness is the problem. Programmers don't write enough tests that check boring things which could have been checked automatically with strong typing. Strong typing still allows you to write unit tests, on more important things other than type safety..Programmers should spend time writing more interesting and useful tests rather than duplicating boring type check ones that a compiler or run time environment could have done for you.
Weak typing is like stepping back in technology and having a car with no "tachometer", no "horn", and no "burnt out headlight" warning lamp. Why this is an advantage? Ahh, because driving around in the dark without headlights and without a horn is safer and more enjoyable!
Even the most intelligent masters of programming make stupid unintentional typo or conversion mistakes (such as the above comma/integer conversion mistake) that can bring a website down.
Remember the days when doctors did not sterilize their utensils before surgery because only incompetent doctors would need to do that? Yes in history man has done some stupid things... is your code sterile? Do you here people advocating for our code not to be sterile? (any language with dynamic, loose, and/or weak typing). Because only real men can program without a proper type system... and only real men use utensils without sterilizing them?
"Strong opposition came from the surgeons themselves. The Royal Surgical Society even issued a strongly worded Manifesto against sterilization, arguing, firstly, that imposing such artificial disciplines in the operating rooms would stifle the surgical creativity of its members, and, secondly, that, if sterilization of the knife really helped, everybody would be able to operate and that would be totally intolerable. Furthermore, what was then known as operational research had shown that the proposed sterilization procedures would be much too complicated for the average nurse." --EWD
Sound similar to those who argue dynamic and loose/weak type systems allow the programmer to have more "creativity"...? You've heard all the arguments defending dynamic and weakly typed languages. In fact there is even debate whether or not dynamic can ever be strong typing, despite the attempts to make it so. Oh it allows geniuses to get away with all sorts of.. errors. At run time. No sense stopping them at compile time, right? because only incompetent people who don't know what they are doing would bother with strong static type systems. Static strong type systems would let pretty much anyone program, wouldn't it, taking away the careers of genius shoot yourself in the foot programmers who have to maintain trickery based code...
This is a real world example of loose/weak/dynamic type systems failing at the top of this page. The gray snippet above was taken from a real person in the PHP manual comments. It's not a made up example or hypothetical.