使用函数进行编程练习
我有一个返回整数的函数,但是我想扩展它以向其添加新参数。但是,使用此参数,该函数必须返回一个数组。
- 有一个基于参数返回数组或整数的函数是不好的做法吗?
- 如果是这样,我该如何解决这个问题?
我认为仅仅复制粘贴整个函数额外 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果可能的话,我会从一个函数中调用另一个函数。
对于您想要的功能,可能无法做到这一点,但这可能是一个不错的选择。
作为额外说明,由于这些函数彼此相关,我会将它们编写为类方法以将它们“分组”在一起。
以
Users
为例;我可能需要一个函数来获取单个用户,另一个函数来获取多个用户。将这些方法收集在一个类中是有意义的:If at all possible, I would call one function from within another.
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:在 PHP 中,我会说“一个函数一种输出类型”。 (
FALSE
值除外,它在 PHP 中具有特殊含义)。如果 PHP 支持重载,情况可能会有所不同,但事实并非如此。但这里有一个问题,为什么不简单地使用一个函数来包装这两个函数呢?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?在我看来,这是不好的做法,因为它可能会导致使用它时出现问题,因为您并不总是知道您是从函数中获取 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.
我反对将事物作为数组返回,如果您的函数正在修改多个值,那么您编写的函数可能不是正确的,并且可能您需要有两个相同的函数。这样做的一个主要原因干净且可维护的代码,明天一个新人不需要去想知道他会发生什么,因为在某些情况下你必须修改多个参数,在这种情况下,进行调用看看该方法做了什么并返回正确的值和另一个值通过引用传递并编辑 它。
这是通过引用传递的示例..
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..
您在这里列出了一些原则:
我认为这三个都是有效的。你的问题听起来像是 3. 和 2. 或 1.. 之间的交易。我认为你不应该将它们相互交易。
您没有分享太多关于您试图通过该功能实现的目标,因此完全抽象,我可以从中看到以下路径:
嗯,听起来有点像聪明人的说法,但老实说,您可能没有正确使用迭代器(foreach),或者您开始创建一个“一个函数完成所有操作”的函数,而您不应该这样做。这最终会重复代码。
You name some principles here:
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:
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.