开发者

pros and cons for implicit conversion in a dynamic language?

I don't really use dynamic/scripting languages. So i dont know开发者_如何学Go the drawbacks, the good parts, etc. While talking to someone he mention that python could implicitly convert an object to what you wanted but doesnt even try.

People seem to like python. So is implicit conversion in a dynamic language bad? Is it bad? (he wanted it) What are the pros and cons of implicit conversions in a dynamic language?


Pros:

  • "Does what you mean" in most cases.
  • It lets you semi-freely treat, say, strings as numbers and vice versa. I've used this a bit in Perl to do string operations on numbers, that otherwise would involve a bunch of non-obvious math.

Cons:

  • When it does fail, it often fails in mysterious ways. As an example, leading zeros can cause weird issues in languages that know about octal numbers. '00' through '07' will work fine, but '08' and '09' will break.
  • Sometimes, you really do want to know the type. Example: PHP and its habit of returning false when something fails. If strpos($a, $b) returns 0, then $b was found at the beginning of $a; false means it wasn't found at all. But implicit conversions make 0 and false compare equal. (Fortunately, PHP has the === operator to mitigate this.)
  • Conversion takes CPU cycles. It's not much, but if you don't want implicit conversion magic, why pay for it?


I like to see this in the aspect of use case; So my answer is, it really depends on what you want to do.
If you are writing a web application then this would help you to write cleaner codes (codes without lots of function and method calls for just converting types) and the downward is it would potentially create some mysterious bugs and in this situation (web application) writing cleaner and more understandable codes helps to reduce the cost of development and changes and thus the cost of fixing this potential bugs can be ignored.
But if for example, you are writing a firmware for a safety critical hardware (e.g. space shuttle) any potential bug can cost millions of dollars and years and years of time and energy, so in this case it would be appropriate to endure the pain of writing some convert functions and methods to protect the whole purpose of project.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜