Dynamism Has Its Place

Dynamic programming and tools have their place. Things like medical applications and accounting probably are not among them, but that does not mean there is no place for dynamic techniques. Features of dynamic techniques include:

Situations where they may be appropriate:

In the "typing debates", there seems to be an all-or-nothing assumption such that one should always use dynamic languages or always use "strict" languages. But I believe it to be a matter of PickTheRightToolForTheJob. It may also be that different personalities are suited to the different approaches.


Luckily, many strict languages offer an escape system - such as using a variant type, or even a simple pointer (untyped or typed). Other techniques used in strict languages when one needs an escape, are structs or records with several types stored inside the struct or record. An enumeration can be used to classify what type is currently set in the struct.

If anyone is interested, I can post some example programs of where I have defeated the strong/strict/static type system using several methods. Another form of dynamism in languages is when one has built in compiler magic to pass in several types such as a varargs or array of any type parameter. I feel that the best compromise is the be safe by default philosophy. Allow a (convenient) escape in emergency, but aim to be safe by default. Humans make mistakes, humans are not perfect. I have similar views on security. In addition, strings themselves in databases can be used as an escape.. or blob fields and similar.

Perhaps the problem is that there should be a way to offer a constraint system for usually dynamic languages. Why? well, because, as stated before by someoneelse, dynamic languages are typically used for RapidPrototyping and/or RapidApplicationDevelopment, and a typical problem that arises after the very nice demostration of the prototype is that it will have to become a SeriousSolution?, and often the only way to do that is to start coding from scratch Maybe what we need is a SuperLanguage? like XMF (ExtensibleProgrammingLanguage)

Generic programming, templates, polymorphism et al also help escape the strict/strong/static nature of languages but that is somewhat of a different topic.. although still it involves a bit of dynamism at say compile time or even run time in some implementations.

Just compile it with a special flag that requires it to have explicit types and declarations:

  compile myprog.prg -anal=on

Sorry, couldn't resist ;-P

That's something some programmers don't understand. In this industry, being anally retentive is far better than being easy going. I mean, the whole idea of fixing a bug.. is being up tight. Why fix it? Programmers are anal retentive, critical, precise people. As for compiling it with a switch.. it isn't that easy. Consider an array of strings in a dynamic language which you also wish to write the same routines for with numbers. With numbers you do not want to allow zeros into the array. With strings you do not want to allow empty strings ("") into the array. In both the dynamic and the static language, you still have to write routines to check whether the string is empty or not, and whether the integer is zero or not. Horribly, you could get mixed up whether you were dealing with a zero string ("0") or a zero integer (0), because you don't know what type is coming in as a parameter. For example if you wanted to allow zero (0) strings in to your string array, but not your integer array.... you cannot easily do this without confusing the fsck out of yourself in a dynamic/weak/loose language.. because darn dang it I can't remember if "0" is a string or if "0" is a number. See also IncludeFileParametricPolymorphism. So with a list of "any type", if I want to store "0" strings, but not "0" numbers, then which is it? What am I passing in and how can I remember, especially when humans make mistakes? How am I really sure that "0" is a number or a string?

You are thinking.. does it matter? What's the difference? That's where you JustDontGetIt probably. Oh Oh, but is there a IsString?() function that tells you the type being passed in, or the current type flag as you call them? And when you call this IsString?() function, is not that a Cast, bloating up your code just like with typecasts? And is not that more error prone, for you to have to remember to do all these checks (if you even know what checks to do, which many programmers don't).. versus designing your programs automatically this way since the system doesn't work without designing it that way.. Really, it is easy to mix up a zero string with a zero integer and forget to check it.. or other similar things.. because you don't always know what your flags really are.. it is all done behind your back similar to how encapsulated OOP is done behind your back. I'd argue that dynamic/weak/loose typing is a form of encapsulation, OOP-ish. Everything is just a "dynamic loose object" and not a precise "string, apple, integer, banana, etc". Next time someone asks you what type of fruit you would like to eat for a snack.. tell them I want you to surprise me.. blindfold me. But remember that eating fruit is fairly safe even with a blind fold on.. whereas, data, not so much as safe. Unless you take extra efforts to remove the blindfold each and every time - but humans are too lazy.

Eating fruit blindfold is not safe at all, after all Habanero chili with 1,000,000 Scoville units (http://en.wikipedia.org/wiki/Habanero_chili) is a fruit... Opuntia (http://en.wikipedia.org/wiki/Opuntia) is also a fruit (with spines ) and you wouldn't want to be given one while blindfolded.

You took my point and proved it further. Someone anal retentive (just like me) says.. wait! Even that can be unsafe. And you're right.


Example "Ted"

 function foo(a, b, c) {
   if(length(a) > 2) {
     bar(a, b);
   } else {
     bar(b, c);
   }
 }

What the hell is that? PHP without dollar signs? Nice pseudo language.

And how are you going to inform your users that the above Foo function accepts only certain kinds of parameters if it is working with numbers only? What does it return? Nothing, or something? Does it modify strings? Check strings? Check numbers only? Where is your contract?

Is your contract in the source comments and do people find out what the incoming parameters are by reading the source comments? Where is your interface? In the documentation? In your mind hidden somewhere from other people? Does the function return anything? Is it a procedure or a function? Do I have to look in the source code implementation just to find this info out? Did you even mark the source so I could find out in the first place? Or did you just assume people would know by playing guessing games and reading the non existent docs that you didn't write yet? (these are all rhetorical questions by the way)

Oh, and that above function is just so clear and saves you so much more red tape and typing (and time) than:

 pro foo(a, b, c: astr);
 b 
    if length(a) > 2 do bar(a, b) els bar(b, c);
 e;

--QompVandal

The example was not meant to illustrate brevity and the above is probably not representative of the verbosity that type-heaviness can result in.


AprilZeroEight


EditText of this page (last edited May 23, 2008) or FindPage with title or text search