Why does resharper suggest const, static operations?

谁都会走 提交于 2019-12-04 19:21:55

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/7021054/why-does-resharper-suggest-const-static-operations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!