Pass complex object vs simplest object per parameter

1

In the Log method there is the parameter of type HttpClient . The function only uses the parameter to access the property BaseAddress , which is a Uri .

private void Log(string verb, HttpClient httpClient) {
    var url = httpClient.BaseAddress.ToString();
    Logging.LogInfo(GetType().Name, $"Iniciou requisição HTTP {verb} no endereço {url}");
}
Since everyone calling Log(string, HttpClient) also has access to HttpClient.BaseAddress , the Log method might look like this:

private void Log(string verb, string url) {
    Logging.LogInfo(GetType().Name, $"Iniciou requisição HTTP {verb} no endereço {url}");
}

and who calls you:

Log(HttpVerbs.Post, anotherHttpClient.BaseAddress.ToString());

In this way, I would not pass the most complex object of type HttpClient but only what interests me, which is a string . I did it first, passing HttpClient in case I need more HTTP client information in the log in the future.

Considering HttpClient the most complex type and string the simplest, what I want to know is whether there is difference in performance and memory usage in both ways.

Is it the same thing to do with the complex object and the simplest object? Is it going to consume more or less memory? How is this handled in .NET?

    
asked by anonymous 09.11.2017 / 18:57

1 answer

4

In general, consumption makes no difference. It may have side effects, but it depends on a lot of things and I do not think it would still be important. At least for this case I do not see happening.

What changes is the engineering issue.

The more information you get, the more you are docking .

On the other hand, the more concrete the information is, the more detailed implementation you are going through, which is still a coupling.

That is, it is difficult to follow a rule. You have to try to find the one that makes the most sense for the case. What are the chances of something changing and creating a complication? Most often it is recommended to go at the lowest risk. But you can always have some guideline in the project that may require doing different.

Some people will call this obsession with primitives . Just as abusing this is wrong, never using it is also, it depends on the case.

Without knowing the concrete case I would say that to pass the more specific information is better there because it is the one that you need. Will it be obtained from sources other than HttpClient ? Of course it's not the end of the world, you can always create an overload and if you do not abuse primitives, it's easy to have a different signature. Perhaps it was the case even to create both. But I also doubt that it is a problem to adopt the most complete object to abstract how it gets what it needs.

Now, this is the theory. In practice there is a lot of case that this is not relevant.

    
09.11.2017 / 19:24