具有大量参数的函数是一种不好的形式吗?还有什么选择呢?

发布于 2024-10-16 19:42:27 字数 440 浏览 2 评论 0原文

我有一个查询数据库的搜索函数,并且有大约 15 个可选参数。显然这并不漂亮,而且调用它有点混乱。 PHP 不允许重载方法,所以我刚刚创建了巨大的函数签名。

在其他地方,我看到了诸如创建参数类之类的建议:使用大量的缺点参数

但这似乎太重了。我可以传递一个关联数组,但是虽然这减少了参数的数量,但我相信它不太容易遵循,因为没有内置文档说明数组中应该存在哪些键。

还有其他方法可以优雅地处理这个问题吗?通常在其他语言中,我会有一个非常丑陋的 private 方法,它需要多达十几个参数,然后创建同名的 public 方法,该方法接受这些参数的子集,并且内部调用私有方法。

I have a search function that queries the database and has ~15 optional parameters. Obviously this is not pretty and calling it is a bit of a mess. PHP does not allow overloading methods so I've just been creating huge function signatures.

Elsewhere I've seen suggestions such as creating a parameter class: Disadvantages of using a lot of parameters

But this seems too heavy. I could pass in an associative array, but while this reduces the number of parameters I believe it is less easy to follow as there is no built in documentation stating what keys should exist in the array.

Is there any other way to handle this gracefully? Typically in other languages I would have a really ugly private method that takes up to a dozen parameters and then create public methods of the same name which accept a subset of those parameters and internally call the private method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

阳光①夏 2024-10-23 19:42:27

在 PHP 中,您可以使用关联数组:

someFunction(array(
    "a" => 3243,
    "b" => 2354,
    "c" => 33453,
    "d" => 324353,
    "e" => 321243,
    "f" => 321243,
    "g" => 312243,
    "h" => 321243,
))

或调用函数的对象的属性(如果有意义)。 PHPMailer 发送邮件是这样的:

// instantiate the class
$mailer = new PHPMailer();

// Set the subject
$mailer->Subject = 'This is a test';

// Body
$mailer->Body = 'This is a test of my mail system!';

// Add an address to send to.
$mailer->AddAddress('[email protected]', 'Eric Rosebrock');

if(!$mailer->Send())
{
    echo 'There was a problem sending this mail!';
}

并且它还有更多可选参数。它也可以使用具有数百个参数的方法,但这更具可读性。

编辑:这些解决方案还更好地支持可选参数。对于属性来说很简单,对于关联数组,您可以将数组与默认值数组合并。

In PHP, you can use associative array:

someFunction(array(
    "a" => 3243,
    "b" => 2354,
    "c" => 33453,
    "d" => 324353,
    "e" => 321243,
    "f" => 321243,
    "g" => 312243,
    "h" => 321243,
))

Or properties of the object that the function is being called on (if it makes sense). PHPMailer send mails like this:

// instantiate the class
$mailer = new PHPMailer();

// Set the subject
$mailer->Subject = 'This is a test';

// Body
$mailer->Body = 'This is a test of my mail system!';

// Add an address to send to.
$mailer->AddAddress('[email protected]', 'Eric Rosebrock');

if(!$mailer->Send())
{
    echo 'There was a problem sending this mail!';
}

And it has many more optional parameters. It could as well use a method with hundred parameters, but this is much more readable.

EDIT: These solutions also better support optional parameters. In case of properties it is straightforward, in case of associative array, you can merge the array with array of default values.

浅浅淡淡 2024-10-23 19:42:27

一般来说,长参数列表是代码中所谓的“坏味道”,可以通过称为“引入参数对象”的重构来删除。请参阅以供参考。

干杯

In general the long parameter list is a so called bad smell in code which can be removed via refactoring called Introduce parameter object. See this for reference.

Cheeres

千寻… 2024-10-23 19:42:27

是的,好的经验法则是参数不超过 3-4 个。如果您需要更多,通常您应该使用数组或对象作为参数之一。但在某些情况下,如果您认为确实需要更多参数,那么当然,为什么不呢。如果它使您的代码易于理解和使用,那为什么不呢。

Yes, the good rule of thumb is to have no more than 3-4 params. If you need more then normally you should use array or object as one of the params. But in some cases if you think you really need more params, then sure, why not. If it makes your code easy to understand and use, then why not.

谎言 2024-10-23 19:42:27

您可以创建一个类,将参数存储为属性,允许您根据需要设置每个属性,然后有一个使用这些属性来查询数据库的方法。构造函数可以为这些属性设置默认值。这只是让通话变得更容易一些。

$function = ClassFunction();
$function->arg1 = 'Some value.';
$function->arg2 = true;
$function->arg3 = 5;

$result = $function->call_method();    // This uses default values for any property not set.

You could create a class which stores the parameters as properties, allowing you to set each property as you need, then have a method which uses these properties to query the database. The constructor can set default values for these properties. This just makes calling a bit easier.

$function = ClassFunction();
$function->arg1 = 'Some value.';
$function->arg2 = true;
$function->arg3 = 5;

$result = $function->call_method();    // This uses default values for any property not set.
暖伴 2024-10-23 19:42:27

在我看来,“参数太多”的问题只是一个更深层次的潜在问题的表现:糟糕的架构。如果一个函数确实需要所有这些参数值,那么它所做的事情很有可能超出了应有的范围。

这应该是一个提醒,“哦嘿,让我们重新考虑不要使用过程 X 来做所有的事情,而是想想 X 应该真正做什么,Y 和 Z 应该做什么。

The problem "too many parameters" in my opinion is only the manifestation of a much deeper underlaying problem: a bad architechture. If a function really needs all those parameter values, chances are very hight that it is doing a lot more than it should.

This should be a reminder "oh hey, let's reconsider not using procedure X to do all the stuff but to think what should really be done by X and what should be done by Y and Z.

羁拥 2024-10-23 19:42:27

将您的函数转换为类会很好。有两个主要优点:

  • 函数参数转换为属性并且可以注释

  • 我认为相当大的函数代码可以分成一组较小的私有代码方法

It'd be nice to convert your function to class. There are two major advantages:

  • Function arguments converted to properties and can be commented

  • Function code which I think is rather large can be split into set of smaller private methods

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文