为什么静态类型语言有时更好。 [英] Why static typed languages are sometimes better.

查看:64
本文介绍了为什么静态类型语言有时更好。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是有一个有趣的小惊喜。我有一个方法,它将一个字符串作为一个参数。

。我想把它改成一个字符串

或一串字符串,所以我做了我平常的先测试。事情。


我改变了我已经用一个字符串调用它的单元测试

用两个字符串的元组调用它。然后我跑了测试,期待它失败了
。下一步是编写代码进行测试

通过。


令人惊讶的是,测试通过了(这意味着我已经完成了,对?)。嗯,我花了一些时间才意识到我在当前版本中使用它作为字典键的唯一一件事。

。由于元数为
两个字符串是一个有效的密钥,所以测试通过就好了。有时

语言太宽容了:-)

解决方案

我同意。我很欣赏Python的动态和多态性。在

另一方面,我希望能够更好地抓住我的愚蠢错误和

打字错误。如果只有像棉绒这样的东西,它会为我节省一些东西

修复运行时错误。


Wai Yip Tung


2004年7月1日星期四17:43:56 -0400,Roy Smith< ro*@panix.com>写道:

我只是有一个有趣的小惊喜。我有一个将字符串作为参数的方法。我想把它改成字符串
或字符串元组,所以我做了我平常的先测试。事情。

我改变了我已经用一个字符串调用它的单元测试,用两个字符串的元组调用它。然后我跑了测试,期待它失败。下一步是编写代码以使测试通过。

令人惊讶的是,测试通过了(这意味着我已经完成了,对吧?)。好吧,我花了一点时间才意识到我在当前版本中对论点
所做的唯一事情就是将它用作字典键。由于两个字符串的元组是一个有效的键,所以测试通过就好了。有时
语言太宽容了: - )




在文章< ro ******** ***************@reader2.panix.com> ;,

Roy Smith< ro*@panix.com>写道:

我只是有一个有趣的小惊喜。我有一个将字符串作为参数的方法。我想把它改成字符串
或字符串元组,所以我做了我平常的先测试。事情。

我改变了我已经用一个字符串调用它的单元测试,用两个字符串的元组调用它。然后我跑了测试,期待它失败。下一步是编写代码以使测试通过。

令人惊讶的是,测试通过了(这意味着我已经完成了,对吧?)。好吧,我花了一点时间才意识到我在当前版本中对论点
所做的唯一事情就是将它用作字典键。由于两个字符串的元组是一个有效的键,所以测试通过就好了。有时候
语言太宽容了: - )




静态输入也可能会产生

的有益效果,帮助你决定首先不要这样做。


你要将函数参数类型从String

更改为[String],然后重新编译。编译器会告诉

关于函数被调用的每个地方
带有String参数的
,你会去更改它们

来自''xyz'你好''''''xyz [" Hello"]''。需要10分钟

,并为您提供更简单,更易于理解的代码。


Donn Cave,做** @ u.washington.edu


2004年7月1日星期四14:54:27 -0700,< Wai Yip Tung"

< tu ******** @ yahoo.com>写道:

我同意。我很欣赏Python的动态和多态性。另一方面,我希望能够更好地抓住我的愚蠢错误和错别字。如果只有类似lint的东西,它会为我节省一些东西
修复运行时错误。




有一个lint:PyChecker。

但是由于未知原因它现在已经死了。

我们仍然没有使用手动添加类型

声明的工具。


I just had an interesting little surprise. I''ve got a method that takes
a string as an argument. I wanted to change it to take either a string
or a tuple of strings, so I did my usual "test first" thing.

I changed the unit test I already had from calling it with a string to
calling it with tuple of two strings. I then ran the test, expecting it
to fail. The next step would be to go write the code to make the test
pass.

Amazingly, the test passed (that means I''m done, right?). Well, it took
me a moment to realize that the only thing I ever do with the argument
in the current version is use it as a dictionary key. Since a tuple of
two strings is a valid key, so the test passed just fine. Sometimes the
language is just too forgiving :-)

解决方案

I agree. I appreciate the dynamic and polymorphic power of Python. On the
other hand I hope it does a better job to catch my silly mistakes and
typos. If only there is something like a lint it would save me something
fixing runtime errors.

Wai Yip Tung

On Thu, 01 Jul 2004 17:43:56 -0400, Roy Smith <ro*@panix.com> wrote:

I just had an interesting little surprise. I''ve got a method that takes
a string as an argument. I wanted to change it to take either a string
or a tuple of strings, so I did my usual "test first" thing.

I changed the unit test I already had from calling it with a string to
calling it with tuple of two strings. I then ran the test, expecting it
to fail. The next step would be to go write the code to make the test
pass.

Amazingly, the test passed (that means I''m done, right?). Well, it took
me a moment to realize that the only thing I ever do with the argument
in the current version is use it as a dictionary key. Since a tuple of
two strings is a valid key, so the test passed just fine. Sometimes the
language is just too forgiving :-)




In article <ro***********************@reader2.panix.com>,
Roy Smith <ro*@panix.com> wrote:

I just had an interesting little surprise. I''ve got a method that takes
a string as an argument. I wanted to change it to take either a string
or a tuple of strings, so I did my usual "test first" thing.

I changed the unit test I already had from calling it with a string to
calling it with tuple of two strings. I then ran the test, expecting it
to fail. The next step would be to go write the code to make the test
pass.

Amazingly, the test passed (that means I''m done, right?). Well, it took
me a moment to realize that the only thing I ever do with the argument
in the current version is use it as a dictionary key. Since a tuple of
two strings is a valid key, so the test passed just fine. Sometimes the
language is just too forgiving :-)



Static typing might also have the salutory effect of
helping you decide not to do this in the first place.

You''d change the function parameter type from String
to [String], and recompile. The compiler would tell
you about every place where the function is called
with a String parameter, and you''d go and change them
from ''xyz "Hello"'' to ''xyz ["Hello"]''. Takes 10 minutes
and leaves you with simpler, easier to understand code.

Donn Cave, do**@u.washington.edu


On Thu, 01 Jul 2004 14:54:27 -0700, "Wai Yip Tung"
<tu********@yahoo.com> wrote:

I agree. I appreciate the dynamic and polymorphic power of Python. On the
other hand I hope it does a better job to catch my silly mistakes and
typos. If only there is something like a lint it would save me something
fixing runtime errors.



There was a lint: PyChecker.
But for unkown reasons it is now dead.
And we still have no tools that makes use of manually added type
declarations.


这篇关于为什么静态类型语言有时更好。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆