如何在Kotlin中重构具有超过6个参数的方法

huangapple go评论78阅读模式
英文:

How to refactor method with more than 6 parameters in the kotlin

问题

我需要在 API 中发起一个请求,我必须发送11个参数,但使用代码分析工具通知我最多只能发送6个。有没有一些重构的方式让我在不这样发送的情况下发送这些参数呢?我正在使用 Kotlin。

我简化了我的代码:

viewModel.requestTest(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param10)
class ViewModel

fun requestTest(param1: String, param2: String, param3: String? = "", param4: String? = "", param5: String? = "", param6: String? = "") {
    // 发起请求,并使用这些参数填充请求体
}

注意:我已经按照你的要求进行了翻译,只返回了翻译好的部分,没有包含其他内容。如果你有任何进一步的需求,请随时提问。

英文:

I need to make a request in an api, and I have to send 11 parameters, but using a code analysis tool, inform that I must send a maximum of 6. There would be some form of refactoring for me to send these parameters without being sent this way? I'm using kotlin
I simplified my code:

viewModel.requestTest(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param10)
class viewModel

fun requestTest (param1 , param2, param3 : String? = "" , param4 : String? = "", param5 : String? = "", param6 : String? = "", param7 : String? = "", param8 : String? = "", param9 : String? = "", param10 : String? = "" , param10: String? = "") {
    //do request filling the body with these parameters
}


答案1

得分: 0

将数据类用作参数的包装器

```kotlin
data class RequestParams(val param1: String, ..., val paramN: String)

fun requestTest (params: RequestParams) {
    // 使用参数如 params.param1
}

注意,您还可以为这些参数提供默认值,并在大多数情况下使用命名参数来简化此类型的调用,例如:

data class RequestParams(
  val name: String = "foo",
  val value: Int = 3,
  ...,
  val whatever: String? = null)

val params = RequestParams(
  value = 5
)

...

请注意,您仍然将超过6个参数传递给数据类的构造函数,但通常检查工具会认识到数据类是保存多个属性的惯用方式,并在检查此规则时忽略它们(正如您的问题评论中@Matt Timmermans所指出的,这确实是情况)。

如果这不能解决您的问题,那么您可以将您的10个参数分成2个数据类,其中有5+5或4+6个参数 -- 一切是否有意义都取决于参数的基础业务含义。

如果以这种方式拆分参数没有意义,我倾向于在数据类构造函数上添加一个linter忽略注释,而不是专门编码以绕过linter。


<details>
<summary>英文:</summary>

Use a data class as a wrapper for the parameters.

```kotlin
data class RequestParams(val param1: String, ..., val paramN: String)

fun requestTest (params: RequestParams) {
    // use parameters like params.param1
}

Note, you can also provide default values for those parameters and use named arguments to simplify this type of call in most cases e.g.

data class RequestParams(
  val name: String = &quot;foo&quot;,
  val value: Int = 3,
  ...,
  val whatever: String? = null)

val params = RequestParams(
  value = 5
)

...

Note that you are still passing more than 6 parameters to the constructor of the data class, but usually linting tools will recognize that data classes are an idiomatic way to hold many properties, and ignore them when checking this rule (as noted by @Matt Timmermans in the comments of your question, this is indeed the case).

If that does not solve your problem, then you could group your 10 parameters into 2 data classes, with 5+5 or 4+6 parameters -- whether that makes any sense all depends on the underlying business meaning of your parameters.

If it does not make sense to split the parameters that way, I'd tend to add a linter ignore annotation on the data class constructor rather than coding specifically to work around the linter.

答案2

得分: 0

我认为在Android系统中使用类似Bundle(https://developer.android.com/reference/android/os/Bundle?hl=en)的对象是很有用的。

英文:

i think that it is useful to use Object like Bundle(https://developer.android.com/reference/android/os/Bundle?hl=en) in Android system

huangapple
  • 本文由 发表于 2020年8月27日 05:07:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63605755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定