Does creating many static classes impact system performance?

Asked

Viewed 201 times

15

I’ve been creating many static classes to facilitate and clear code, such as a Google Translate API call.

public static class GoogleTranslate
{
    public static string Translate(string word){
        //código de chamada
    }
}

Doing so is much easier because just call:

GoogleTranslate.Translate("StackOverflow é a melhor universidade do mundo");

I have enough such classes to call APIS, as this is seen before professionals?

  • 1

    +1 great question. If it’s not too much to ask, whoever answers takes more into account the concept, it would be nice for those who are from other languages to understand better too.

  • 2

    Since we are talking about api and static methods, I suggest you read this article.: YOU'RE USING HTTPCLIENT WRONG AND IT IS DESTABILIZING YOUR SOFTWARE, does not answer your question, but may come to help you.

  • @Tobiasmesquita I’ll give a read Valeuu

  • Related: https://answall.com/a/287792/101

1 answer

14


Yes, it gets faster, static classes do not need to instantiate, which has a non-trivial cost.

Static methods are like normal functions of any language, it is there ready for use. The advantage is that it has a surname (it is practically a namespace), then better organize the code. No need to allocate anything (normal class there is allocation even without being), no sophisticated mechanisms (except when it has been, but in general should not use global state). In static classes all methods shall be static.

It looks like something very different, but all languages that have not tried to sell themselves as 100% object-oriented, which is a fallacy, have always had, has always been useful and worked.

The gain is more by the static method than by the class. It is also true for static class methods who can be instantiated.

There are other characteristics of static classes, but they are not relevant and often even bad.

One of them is its construction. It occurs sometime before it is needed (it may not be on the load, but it may be, so a technique of Lazy loading can be interesting). Need to make sure that the coded form meets everything well, that does not exaggerate in the system load (rarely occurs), It is necessary to understand all implications to use correctly. Ideally it should not have a construction, but it can if necessary, useful and know what it is doing.

Those who like OOP say you should not use, I come from a more pragmatic school that does what it should do.

It’s harder to test, but not impossible. I’d rather make the test harder than make the main code harder to test.

If you ever need to change the strategy dynamically, it can become a problem, which is the same question as the test. If it is not something universal and unique the instance may be a better option.

Beware of variable global state in static or normal class. This works well in rare cases and needs to know what you are doing. Do not create a static class when the proper one is an instance. If you have been potentially varying at each run, create an instance.

Me and Stack Overflow abuse static classes :) Many say this is a terrible mistake, even if they don’t know our context. When it has to be an instance, I do it. Most of the time if one day I need to change, I refactor (I can do this, not everyone can), even in cases that give work refactoring, usually compensate for the gain because in almost all cases I never needed to refactor.

  • 4

    Simple like that? No downside? :

  • 1

    I imagined it would be more harmful than beneficial :o.

  • Nor for example the fact that static classes are instantiated by Runtime and that have static constructors that may have nontrivial code in them, thus creating a slower system inialization?

  • 1

    @Maniero as always bringing answers fucks and demonstrating a little of his experience. Thanks! (I know it’s against the rules to thank, but I can’t help).

  • @Omni answered now?

  • @Leonardobonetti is not against the rules, is not necessary, is not positive, but is also not too negative.

  • I always thought I had serious downsides.. Thanks @Maniero for clarifying several points.

  • A very interesting point is what you said about the test... sometimes we see very difficult codes to understand and maintain.. however the tests are very simple.. ie.. spend more time on testing or development? good point.

  • @Thiagoloureiro is what I say and many people don’t understand. And worse, often the test is done bureaucratically, does not test anything useful and fails to test what you need. Then the person thinks I am against tests. I am against senseless testing. I am against making the code worse to make the test easier. Nor is it a matter of time spent, it is of complexity. Of course anything is bad if done wrong.

  • @Perfect Maniero, I understand that we have to follow standards, principles (SOLID) for example, and so on.. but it goes exactly to the point you said.. from the point that you start to complicate your code to test something.. and in the end it’s something that doesn’t make sense, for what? :)

  • @Maniero It’s different from Java, right? What has the ClassLoader that instance the static class in the same way (only changes the time of allocation, which is more at the beginning of the application execution). So I assume that in Java it is not so much advantage to use static classes.

  • @Piovezan I imagine so. C# 9 must have a way of setting up when the process happens, because they realized that there are costs to leave to happen later and most people don’t need it, so forcing to boot on startup, would be cheaper.

Show 7 more comments

Browser other questions tagged

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