为什么 resharper 建议 const、静态操作?
我想知道为什么 resharper 建议在非静态类中使用静态方法? 是为了保存实例的创建吗?是性能问题吗? 另外,为什么它建议“const”一些参数?是性能问题吗? 我很想得到一些解释
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当编译器遇到静态方法时,它会发出
call
指令,但当遇到实例方法时,它会发出callvirt
指令。现在,callvirt
指令在调用之前检查对象是否为 null。所以它会带来性能损失。但它有助于使方法调用多态。因此,如果该方法与类的任何属性的状态更改无关,建议将该方法设为静态,因为它可以提高性能。
关于 const 的使用,它是值的编译时关联,而不是在运行时关联。因此 const 的所有变量都被编译时本身的值替换,这显然提高了性能。
When compiler encounters a static method it emits
call
instructions but when it encounters an instance method it emitscallvirt
instruction. Now thecallvirt
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.
这是可读性的问题。当您将方法设置为
静态
时,您就明确声明它不访问非静态成员变量。如果您标记一个变量const
,您就清楚地表明它不能(因此不会)在代码中更改。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 variableconst
you state it clear that it can't (and therefore won't) be changed in code.其他答案是正确的,这只是一个很好的做法。
但我想展示它如何让您受益。很多时候,当某个大方法可以变成静态时,这暗示着那里还有另一个责任,最好的处理方法可能是为该任务提取另一个对象。
它还可以产生连锁反应类型的效果 - 比如说 A 调用 B,两者都是非静态的。现在 Resharper 告诉我们 B 可以变成静态的;我们让它做它该做的事。现在也许 A 也可以变成静态的。也许 A 也完全是另一种责任。
在重构旧代码时,这种效果对我来说很方便。它让我看到了职责和要点,我可以在其中删除代码,而不必费尽心思处理每一英寸的文本。
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.
静态类不需要实例来调用该类,并且 Re Sharper 足够聪明,可以发现该方法可以是静态的,因此人们可以在没有实例的情况下使用该方法。
如果变量仅用于保存一些实时值,那么最好将它们转换为常量,这样可以避免我们意外更新该变量。这是值得遵循的良好做法,Re Sharper 也向我们建议了同样的做法。
无论如何,如果您不喜欢这些建议,那么您也可以将其关闭。
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.
实际上,我什至想说 JetBrains 应该删除其默认值,以建议使用 const 而不是 static readonly。
阅读此处: https://www.exceptionnotfound .net/const-vs-static-vs-readonly-in-c-sharp-applications/
总结是,当您处理多个程序集时,Const 变量非常混乱。如果程序集 A 具有 const X,而程序集 B 使用该 X。那么每次程序集 A 更改该 X 值时,程序集 B 都必须重新编译。
它只会给您带来您不想要的头痛!
当谈到速度时?嗯...编译器已经取得了长足的进步并且非常聪明。从长远来看,从 const 获得的速度性能可以忽略不计。
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.