简体   繁体   中英

Why does resharper suggest const, static operations?

I was wondering why resharper suggests a method to be static in non static class? Is it for saving the creation of the instance? Is it a matter of performance? also, why does it suggest to 'const' some params? Is it a matter of performance? I would love to get some explanation

When compiler encounters a static method it emits call instructions but when it encounters an instance method it emits callvirt instruction. Now the callvirt instruction checks the object whether it is null before making the call. so there is a performance penalty attached to it.But it helps in making the method call polymorphycally.

so if the method is not associated with a change of state of any property of the class it is advisiable to make that method static as it improves the peformance

Regarding the use of const it is a compile time association of the value rather than at runtime. so all variables of the const get replaced by the value at compile time itself which obviously improves the performance.

It's a matter of readability. When you make a method static you state it clear that it doesn't access non-static member variables. If you mark a variable const you state it clear that it can't (and therefore won't) be changed in code.

The other answers are correct, it's just a good practice.

But I want to show how it can benefit you. Often times, when some big method can be made static, it is a hint that there's another responsibility there, which may be best handled by extracting another object for just that task.

It also can have a chain-reaction type effect - say A calls B, both non-static. Now Resharper tells us B can be made static; we let it do its thing. Now maybe A can be made static too. Maybe A is also another responsibility entirely.

This effect has come in handy for me when refactoring older code. It let me see responsibilities and points where I could cut code out without having to sweat over every inch of text.

Static class doesn't require instance to call that class and Re sharper is intelligent enough to figure out that the method can be static so people can use that method without instance.

If variable is used only for holding some real time value then it is better to convert them as constant so it save us from accidental update of that variable. It is good practice to follow and Re sharper is suggesting us the same.

Anyway if you dont like these suggestion then you can switch it off too.

It does so because it detects that you have no class member variables in use in a method body.

Actually, I would go as far to say that JetBrains should remove their default for making suggestions to have const over static readonly. Read here: https://www.exceptionnotfound.net/const-vs-static-vs-readonly-in-c-sharp-applications/

Summary is that Const variables are VERY messy when you are dealing with multiple assemblies. If assembly A has the const X, and assembly B uses that X. Then Assembly B MUST be recompiled EACH time assembly A changes that X value. It just gives you a possible headache you do not want!

When it comes to speed? well...the compiler has come a loooooooong way and is very smart. The speed performance you gain from const is negligible in the long run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM