开发者

Is weak typing not necessary anymore?

开发者 https://www.devze.com 2023-01-08 21:12 出处:网络
I come from a statically/strongly typed language background (java), and I recently started to learn python and I think I see the value of dynamic/strongly typed 开发者_JS百科language.

I come from a statically/strongly typed language background (java), and I recently started to learn python and I think I see the value of dynamic/strongly typed 开发者_JS百科language.

Now I'm wondering whether weak typing can be ever desirable.

Going through stackoverflow, I only found explanations that say it has performance benefits. Since the weight of such performance benefit are declining in today's programming world, is weak typing practically dead? For example, would any good language designer in the future consider making his/her language weak-typed?

If so, why?


Weak typing is primarily useful in low-level programming. A function to read an integer or string off a disk, for example, will have to take a sequence of bytes and come up with an integer or string. That's much harder to do with strong typing.


Weak typing was and never will be necessary. It does not effect semantics in any way what so ever. It is the equivalent of calling conversion functions automatically. An example is comparing an int to float in C: 1 == 1.0. This will compile without any complaints. There is no reason for this other than "convenience", for example if there was a function from int_to_float and doing intToFloat(1) == 1.0, it would obviously be built into C and this code would do the exact same thing as when an int is converted to a float. Weak typing is a tradeoff between safety and convenience.

Though I really don't understand what's convenient about having to memorize every single case of (operator × operand × operand) (float / int), (float == int), (string + int), (int + string), (string + boolean), (int + long), etc.


This is not a flame but just my experience in general. In more than 10 years writing and maintaining code I've only come across type related bugs less than half a dozen times. I have however come to strongly hate the idea of types when writing low level code which moves bytes around. I have long considered the idea of typing to be not necessary anymore when the culture of programming adopted the ideals of good readability.

Of course depending on the language and coding style typing can either help or impede code understanding so I don't mind typed languages too much.

So, as long as there are people out there who, like me, don't see any advantages in types there will always be languages designed to be typeless or weakly typed.

Considering the track record of bugs vs what language used (both in my experience and from what I see on open source code) strong or weak typing does not affect code quality much. Considering the code I've actually read in my experience I would also say it does not affect code readability much. To me it's just a matter of taste (though I know strong type people disagree, they just can't back up their disagreement with facts, it's just their gut telling them to disagree). OK, that last sentence was almost flaming. I'd better stop now.


Seems like you could get something working faster if you didn't have to worry about types. Some languages don't force you to worry about them.

It all lies in what you're trying to accomplish.


It trims down a lot of coding time, while not increasing debugging time all that much; Type errors are found in testing almost as quick as a compiler finds them, but you didn't have to type int fooBar = 2 It also helps not to have 4+ Number types, however...

0

精彩评论

暂无评论...
验证码 换一张
取 消