1
1
12rbah2020-04-13 20:12:54
go
12rbah, 2020-04-13 20:12:54

How to avoid a large number of arguments in functions?

In general, I wrote a program, and then it was necessary to introduce several additional features, due to which instead of 2-3 arguments there are now 5 arguments in functions. in so many arguments?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Mamonov, 2020-04-13
@EvgenyMamonov

Ideally, redo.
If it is not possible to remake, the structures will definitely be better.
There is also a "functional options" option https://golang-blog.blogspot.com/2019/10/functiona...

V
Vladislav, 2020-04-13
@ghostiam

Use functional options with prescribed default values
​​https://m.habr.com/ru/post/489296/
Or wrap optional fields in a structure and pass the structure as a function parameter.
If it is possible to remake a function so that it requires fewer arguments, then it is better to do so, for example, split it into 2 functions, if one depends on the other and because of this you have to pass more parameters than necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question