Pass complex object vs simplest object per parameter

Asked

Viewed 275 times

1

In the method Log there is the type parameter 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}");
}

Like everyone you call Log(string, HttpClient) also has access to HttpClient.BaseAddress, the method Log could be so:

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

and who calls him:

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

That way, I wouldn’t pass the more complex object of the kind HttpClient but only what I care about, which is a string. I did it the first way, passing by HttpClient in case in the future I need more information from the HTTP client in the log.

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

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

  • 2

    It is all passed by reference, memory usage is not relevant.

  • https://answall.com/questions/14490/aloca%C3%A7%C3%A3o-de-mem%C3%B3ria-em-c-types-value-e-types-refer%C3%Aancia

1 answer

4


Overall consumption makes no difference. It can have side consequences, but it depends on a lot of things and I don’t think it would still matter. At least for this case I don’t see it happening.

What changes is the question of engineering.

The more information you pass on, the more coupling.

On the other hand the more concrete the information passed the more detail of 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 what makes more sense to the case. What are the chances of something changing and creating a complication? Most of the time it is recommended to go on what has the least risk. But there may always be some guideline in the project that may require doing different.

Some people will call it obsession with primitives. Just like abusing it is wrong, never use it is too, depends on the case.

Without knowing the case I would say that passing on the more specific information is better there because it is she you need. Will that it can be obtained by sources other than the HttpClient? Of course it’s not the end of the world, you can always create an overload and if you don’t abuse primitives it’s easy to have a different signature. Maybe it was the case that I already created both. But I also doubt that it’s a problem to adopt the most complete object to abstract as it takes what it needs.

Now, that’s the theory. In practice there’s a lot of case that’s not relevant.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.