The best way to overload a method when all you want to change is a return type

Since the return type cannot be used to disambiguate the methods, what is the cleanest / best way to overload the method when all you want to change is the return type? The following is an example of code code;

public static string Get(string url, Guid id, bool logResponse = true, bool baseKey = false) { Tuple<string, int> response = Get(url, id, true, logResponse, baseKey); if (response.Item2 > 399) return null; return response.Item1; } public static Tuple<string, int> Get(string url, Guid id, bool returnStatus, bool logResponse = true, bool baseKey = false) { // leaving out lots of code in this method, you should be able to get the point without it int http_status; string response = CallApi(url, key, "GET", out http_status); return new Tuple<string, int>(response, http_status); } 

The above code works, however I have an additional parameter (returnStatus) that has no purpose, it is only there, so the compiler can distinguish two methods. Is there a better way to do this, or am I just stuck by adding useless options?

+4
source share
3 answers

Change the name of the method, for example

 string Get(string url, Guid id, bool logResponse) Tuple<string, int> GetWithStatus(string url, Guid id, bool logResponse) 

The main goal of programming is not to distinguish the compiler from the differences, but to tell the difference to the developers who will read your code. Other parameters are return status as an out parameter:

 string Get(string url, Guid id, bool logResponse, out int status) 

I really don't like the out parameters, but I like the tuples even less - what will the name Item2 developer who uses your method? Is this the status, or the number of attempts, or the length of the response? Neither the method name nor the return type can say what it is.

So, even for the first case with the renamed method, I also changed the return type to something like

 public class ServerResponse { public string Content { get; set; } public HttpStatusCode Status { get; set; } // enum // use this in first method to check if request succeed public bool IsError { get { return (int)Status > 399; } } } 
+10
source

I see three options.

  • Return object and eliminate the call in your call method.
  • Make the method general, then determine the type you want using reflection.
  • Rename this method.

I would choose No. 3. Make them "GetOne" and "GetTuple" and you're done.

+2
source

In my humble opinion, the separation of problems, if the method performs different functions, we separate the two methods (the name of another method).

But I will make one of them a private method for the reflection cycle, the first method will return a generic type T or just T (I may not be from the Overloading topic, what I want to say above is an return string, but for a complex object it can There are many overload methods for returning different types, why not just return T, let the caller get the object T).

Overload is good, depending on requirements.

0
source

Source: https://habr.com/ru/post/1494847/


All Articles