V
V
Vadim Remin2015-08-28 17:38:27
ASP.NET
Vadim Remin, 2015-08-28 17:38:27

Method overloading without code duplication in C#?

Good afternoon,
different overloads of the method do almost the same thing, depending on the presence of some arguments. That is, there can be 10 arguments, or maybe 8. Accordingly, 2 overloads. But the code for working on these arguments is 80% the same. The rest can be split into if. Moreover, the code is not small - queries to the database, calculations, writing to the database, return, etc. 40 lines.
What do you do in this case? Duplicate code for different overloads? Do you break the internals of the method into separate methods and take them out separately, referring to them in each of the overloads? So you can also spawn long chains of calls.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ilya, 2015-08-28
@dkudrin1

In cases where the code allows it, you can move the main functionality into one of the methods (basic for you) and call it from other overloads:

string GenerateText(string text)
{
 return GenerateText(text, "Без заголовка");
}

string GenerateText(string title, string text)
{
 return title + Environment.NewLine + text;
}

You can use optional arguments, they just should work well in case of 1-3 different parameters:
string GenerateText(string title, string text = "Без заголовка")
{
 return title + Environment.NewLine + text;
 }

No one can forbid you to pass null instead of unused arguments and process in code:
string GenerateText(string title, string text)
{
 return title + Environment.NewLine + (text ?? "Без заголовка");
}

And in the case of an indefinite number of arguments of the same type, we use params:
string GenerateText(string title, params string[] text)
{
 return title + Environment.NewLine + string.Join(Environment.NewLine, text);
}

Combine this base set and it should be enough for neat code. And of course, separate the repeatable functionality into methods and classes for later reuse.

T
tex0, 2015-08-28
@tex0

What do you do in this case? ... Break the internals of the method into separate methods and take them out separately, referring to them in each of the overloads?

It's better than duplicating the code, which then ripples in the eyes =).
Personally, having a long method, I always cut it into subtasks, thereby highlighting the main functionality, and the rest of the kit is obtained as special cases (this is also useful for developing the skill of data classification).

D
Dmitry Makarov, 2015-08-29
@DmitryITWorksMakarov

I select the general functionality in a separate method. I call methods from each other. There are also default settings.

M
Mrrl, 2015-08-28
@Mrl

I write a generic method (perhaps with a slightly different set of parameters) that I call from each of the overloads. However, more often it is possible to get by with calling the second method from the first.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question