从函数中避免超过五个参数 [英] Avoid More than Five arguments from a function

查看:96
本文介绍了从函数中避免超过五个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们应该避免使用函数中的五个参数以及如何
我们可以做到.
您能用一个例子解释一下这些东西吗.

Why we should avoid Five argument from a function and how
we can do.
Can you please explain this stuff with a example.

推荐答案



我认为,函数的5个参数仍然过多.
如果您想成为一名干净的代码开发人员,则应尽量不要使用两个以上的参数,否则您的函数太不具体了(或者您应该将这些参数打包到一个对象中).

您不应该使用多于x个参数的原因是可读性.
如果看到此函数调用,是否可以想象第4个参数在做什么而无需查看目标函数?

Hi,

In my opinion, 5 arguments for a function are still too much.
If you try to be a clean-code-developer, you should try to not use more than two arguments, otherwise your function is too less specific (or you should pack the arguments into an object).

The reason why you should not use more than x arguments is the readability.
If you see this function-call, can you imagine what the 4th argument is doing without looking at the destination function?

CalculateResult(3.1f, 1.75f, CalcType.Multiplication, true);



最好是这样的:



Better would be something like this:

CalculateResult(new Calculation { First = 3.1f, Second = 1.75f, Type = CalcType.Multiplication, RoundResultToInt = true });




or

CalulateResultAndRoundToInt(3.1f, 1.75f, CalcType.Multiplication);



我知道,这个示例有点糟糕,但是我希望您明白这一点:)

希望这会有所帮助!

致以诚挚的问候,并祝您编码愉快() 克里斯



I know, the example is a bit crappy, but I hope you see the point :)

Hope this helps!

Best regards and happy (clean) coding,
Chris


这篇关于从函数中避免超过五个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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