C#方法中有多少参数可以接受? [英] How many parameters in C# method are acceptable?

查看:232
本文介绍了C#方法中有多少参数可以接受?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,必须维护一个C#应用程序。
现在我发现一个方法有32个参数(不是自动生成的代码)。



从C / C ++我记得经验法则4参数。它可能是一种老式的规则,生成了旧的0x86编译器,其中4个参数可以容纳在寄存器(快速)或堆栈中。否则。



我不关心关于性能,但我确实有一种感觉,即使在C#中,每个功能的32个参数也不容易维护。



或者我完全不是最新的? >

C#的经验法则是什么?



感谢您的任何提示!

解决方案

没有一般的共识,这取决于你的问题。



可读性受损,有太多...



鲍勃·马丁说,理想的参数数是0,而3是伸展它。



32参数是一个巨大的代码气味。这意味着课程的职责太多,需要重构。即使应用参数对象也可以重构声音,因为它会隐藏错误的设计,而不是解决问题。



第10周清洁代码提示


函数应该有少量的参数。没有争论最好,其次是一,二,三。超过三个是非常有疑问的,应该以偏见为由避免。



I am new to C# and have to maintain a C# Application. Now I've found a method that has 32 Parameters (not auto-generated code).

From C/C++ I remember the rule of thumb "4 Parameters". It may be an old-fashioned rule rooting back to old 0x86 compilers, where 4 Parameters could be accommodated in registers (fast) or on stack otherwise.

I am not concerned about performance, but I do have a feeling that 32 parameters per functions are not easy to maintain even in C#.

Or am I completely not up to date?

What is the rule of thumb for C#?

Thank you for any hint!

解决方案

There is no general consensus and it depends on who you ask.

In general - the moment readability suffers, there are too many...

Bob Martin says the ideal number of parameters is 0 and that 3 is stretching it.

32 parameters is a massive code smell. It means the class has way too many responsibilities and needs to be refactored. Even applying a parameter object refactoring sounds to me like it would hide a bad design rather than solve the issue.

From Clean Code Tip of the Week #10:

Functions should have a small number of arguments. No argument is best, followed by one, two, and three. More than three is very questionable and should be avoided with prejudice.

这篇关于C#方法中有多少参数可以接受?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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