使用函数进行编程练习

发布于 2024-11-30 14:31:24 字数 202 浏览 3 评论 0原文

我有一个返回整数的函数,但是我想扩展它以向其添加新参数。但是,使用此参数,该函数必须返回一个数组

  • 有一个基于参数返回数组或整数的函数是不好的做法吗?
  • 如果是这样,我该如何解决这个问题?

我认为仅仅复制粘贴整个函数额外 4-5 行也是不好的做法。

I have a function that returns an integer, however I would like to expand it to add a new param to it. With this param, however, the function would have to return an array.

  • Is it bad practice to have a function that returns either an array or an integer based on a param?
  • If so, how can I solve this?

I think it would be bad practice also to just copy-paste the entire function for 4-5 extra lines.

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

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

发布评论

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

评论(5

荒岛晴空 2024-12-07 14:31:24

如果可能的话,我会从一个函数中调用另一个函数。

function getSingle($arg)
{
    // Do whatever it is your function should do...
    return 1;
}

function getMultiple($args)
{
    $out = array();
    foreach ($args as $arg) {
        $out[] = getSingle($arg);
    }

    return $out;
}

对于您想要的功能,可能无法做到这一点,但这可能是一个不错的选择。


作为额外说明,由于这些函数彼此相关,我会将它们编写为类方法以将它们“分组”在一起。

Users为例;我可能需要一个函数来获取单个用户,另一个函数来获取多个用户。将这些方法收集在一个类中是有意义的:

class Users
{
    public function getUser($id){}

    public function getUsers(array $id = null){}
}

If at all possible, I would call one function from within another.

function getSingle($arg)
{
    // Do whatever it is your function should do...
    return 1;
}

function getMultiple($args)
{
    $out = array();
    foreach ($args as $arg) {
        $out[] = getSingle($arg);
    }

    return $out;
}

For the function you have in mind, it might not be possible to do this, but it may be a good option.


As an extra note, as the functions are related to one another, I would write them as class methods to "group" them together.

Take Users for example; I might need a function to get a single user and another to get multiple users. It makes sense to collect these methods in a class:

class Users
{
    public function getUser($id){}

    public function getUsers(array $id = null){}
}
○愚か者の日 2024-12-07 14:31:24

在 PHP 中,我会说“一个函数一种输出类型”。 (FALSE 值除外,它在 PHP 中具有特殊含义)。如果 PHP 支持重载,情况可能会有所不同,但事实并非如此。但这里有一个问题,为什么不简单地使用一个函数来包装这两个函数呢?

 function my_wrapped_function($param1, array $param2)
 {
     return $param1 * count($param2);
 }

 function get_array_from_wrapped( $param1, array $param2 )
 {
     return array( $param1, my_wrapped_function($param1, $param2));
 }
 function get_int_from_wrapped( $param1, array $param2 )
 {
     return my_wrapped_function($param1, $param2);
 }

In PHP, I would say "one function one output type." (with the exception of the value FALSE which holds special meaning in PHP). If PHP supported overloading that might be different, but it doesn't. But here's a question, why not simply have one function which both of those functions wrap?

 function my_wrapped_function($param1, array $param2)
 {
     return $param1 * count($param2);
 }

 function get_array_from_wrapped( $param1, array $param2 )
 {
     return array( $param1, my_wrapped_function($param1, $param2));
 }
 function get_int_from_wrapped( $param1, array $param2 )
 {
     return my_wrapped_function($param1, $param2);
 }
淡看悲欢离合 2024-12-07 14:31:24

在我看来,这是不好的做法,因为它可能会导致使用它时出现问题,因为您并不总是知道您是从函数中获取 int 还是数组。

我的建议

总是返回一个数组(即使它只有一项长),并且有一个更通用的函数,易于处理。

In my opinion it is bad practice, as it could cause problems working with it, because you won't always know if you are getting an int or an array from your function.

My Suggestion

Always return an array (even if it is one item long), and have a more general looking function, that is easily handle-able.

何以心动 2024-12-07 14:31:24

我反对将事物作为数组返回,如果您的函数正在修改多个值,那么您编写的函数可能不是正确的,并且可能您需要有两个相同的函数。这样做的一个主要原因干净且可维护的代码,明天一个新人不需要去想知道他会发生什么,因为在某些情况下你必须修改多个参数,在这种情况下,进行调用看看该方法做了什么并返回正确的值和另一个值通过引用传递并编辑 它。

这是通过引用传递的示例..

<?php
function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);
// $a is 6 here
?>

I am against returning things as an array if your function is modifying more than one value then probably the function that you have written isn't the right one and may be you need to have two functions for the same.. One main reason for this clean and maintainable code and tomorrow a new person need nt go and wonder what the he'll is happening having said that there are cases where you have to modify more than one argument in that case take a call see what the method does and return the right value and the other vale pass it by reference and edit it.

Here is an example of passing by reference..

<?php
function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);
// $a is 6 here
?>
生死何惧 2024-12-07 14:31:24

您在这里列出了一些原则:

  1. 使函数参数具有一种含义(并且函数理想情况下为零参数,不惜一切代价避免更多参数)。
  2. 让一个函数只做一件事。
  3. 不要重复自己。

我认为这三个都是有效的。你的问题听起来像是 3. 和 2. 或 1.. 之间的交易。我认为你不应该将它们相互交易。

您没有分享太多关于您试图通过该功能实现的目标,因此完全抽象,我可以从中看到以下路径:

  1. 您面临着设计问题。重新设计和重构您的代码。

嗯,听起来有点像聪明人的说法,但老实说,您可能没有正确使用迭代器(foreach),或者您开始​​创建一个“一个函数完成所有操作”的函数,而您不应该这样做。这最终会重复代码。

You name some principles here:

  1. Make function parameters have one meaning (and functions ideally zero parameters, avoid more at all cost possible).
  2. Make a function do one thing only.
  3. Don't repeat yourself.

I think all those three are valid. Your questions sounds like a trade between 3. and 2. or 1.. I think you should not trade them against each other.

You have not shared much about what you try to achieve with that function, so totally abstracted, I can see the following paths out of it:

  1. You're facing a design issue. Redesign and refactor your code.

Hmm, sounds a bit like clever-talking, but honestly, you might either not use iterators properly (foreach) or you're starting to create a "one function does it all"-function which you shouldn't. That will duplicate code in the end.

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