检查 Perl 函数参数值得吗? [英] Is checking Perl function arguments worth it?

查看:37
本文介绍了检查 Perl 函数参数值得吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于 MooseX::Method::Signatures 甚至在此之前,诸如 Params::Validate 之类的模块旨在输入检查方法或函数的每个参数.我正在考虑将前者用于我未来的所有 Perl 代码,无论是个人还是在我的工作场所.但我不确定这样做是否值得.

There's a lot of buzz about MooseX::Method::Signatures and even before that, modules such as Params::Validate that are designed to type check every argument to methods or functions. I'm considering using the former for all my future Perl code, both personal and at my place of work. But I'm not sure if it's worth the effort.

我正在考虑我之前看过(和编写)的所有 Perl 代码,但没有执行此类检查.我很少看到一个模块这样做:

I'm thinking of all the Perl code I've seen (and written) before that performs no such checking. I very rarely see a module do this:

my ($a, $b) = @_;
defined $a or croak '$a must be defined!';
!ref $a or croak '$a must be a scalar!";
...
@_ == 2 or croak "Too many arguments!";

也许是因为没有某种辅助模块会导致工作量太大,但也许是因为在实践中我们不会向函数发送多余的参数,也不会向需要标量的方法发送数组引用 - 或者如果我们这样做,我们有 use warnings; 并且我们很快就听说了 - 鸭子打字方法.

Perhaps because it's simply too much work without some kind of helper module, but perhaps because in practice we don't send excess arguments to functions, and we don't send arrayrefs to methods that expect scalars - or if we do, we have use warnings; and we quickly hear about it - a duck typing approach.

那么 Perl 类型检查是否值得提高性能,还是它的优势主要体现在编译型强类型语言(如 C 或 Java)中?

So is Perl type checking worth the performance hit, or are its strengths predominantly shown in compiled, strongly typed languages such as C or Java?

我对任何有编写使用这些模块的 Perl 经验并且已经看到(或没有)从它们的使用中受益的人的回答感兴趣;您的公司/项目是否有任何与类型检查相关的政策;以及类型检查和性能方面的任何问题.

I'm interested in answers from anyone who has experience writing Perl that uses these modules and has seen benefits (or not) from their use; if your company/project has any policies relating to type checking; and any problems with type checking and performance.

更新:我最近阅读了一篇关于这个主题的有趣文章,名为 强测试与强打字.忽略轻微的 Python 偏差,它本质上表明类型检查在某些情况下可能令人窒息,即使您的程序通过了类型检查,也不能保证正确性 - 正确的测试是唯一确定的方法.

UPDATE: I read an interesting article on the subject recently, called Strong Testing vs. Strong Typing. Ignoring the slight Python bias, it essentially states that type checking can be suffocating in some instances, and even if your program passes the type checks, it's no guarantee of correctness - proper tests are the only way to be sure.

推荐答案

我基本上同意 brian 的观点.您需要担心多少方法的输入在很大程度上取决于您担心 a) 有人会输入错误数据,以及 b) 错误数据会破坏方法的目的.我还要补充一点,外部方法和内部方法之间存在差异.你需要对公共方法更加勤奋,因为你是在向你的类的消费者做出承诺;相反,您可以对内部方法不那么认真,因为您对访问它的代码有更大的(理论上的)控制权,如果出现问题,只能怪自己.

I basically concur with brian. How much you need to worry about your method's inputs depends heavily on how much you are concerned that a) someone will input bad data, and b) bad data will corrupt the purpose of the method. I would also add that there is a difference between external and internal methods. You need to be more diligent about public methods because you're making a promise to consumers of your class; conversely you can be less diligent about internal methods as you have greater (theoretical) control over the code that accesses it, and have only yourself to blame if things go wrong.

MooseX::Method::Signatures 是一种优雅的解决方案,可以添加一种简单的声明方式来解释方法的参数.Method::Signatures::Simple 和 Params::Validate 很好,但缺少我觉得 Moose 最吸引人的功能之一:类型系统.我已经在几个项目中使用了 MooseX::Declare 和 MooseX::Method::Signatures,我发现编写额外检查的门槛非常低,几乎是诱人的.

MooseX::Method::Signatures is an elegant solution to adding a simple declarative way to explain the parameters of a method. Method::Signatures::Simple and Params::Validate are nice but lack one of the features I find most appealing about Moose: the Type system. I have used MooseX::Declare and by extension MooseX::Method::Signatures for several projects and I find that the bar to writing the extra checks is so minimal it's almost seductive.

这篇关于检查 Perl 函数参数值得吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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