CodeRush 建议将具有参数的方法设为静态,同意还是不同意?

发布于 2024-12-15 04:04:11 字数 293 浏览 5 评论 0原文

我使用 Developer Express 的 CodeRush 产品,该产品有一个名为“代码问题”的功能,可以提出优化代码的建议。我注意到,如果您有一个带有参数的方法,它总是建议将此方法设为静态。本着尝试编写最好的代码并进行优化(我认为这就是 DevExpress 试图帮助我们做的事情)的精神,我听到了关于使方法静态是否实际上明智的不同意见。

您对于什么时候方法应该是静态的有何看法?这样做有什么好处吗?影响?我没有看到它有什么问题,因为它需要参数来运行该方法,因此这不会成为跨多个用户/用途的问题。

好还是坏?

谢谢。

I use Developer Express's CodeRush product which has a feature called Code Issues which makes suggestions to optimize your code. I've noticed that if you have a method that has parameters it will always suggest making this method static. In the spirit of trying to write the best code and optimized which I assume is what DevExpress is trying to help us do, I've heard mixed opinions on whether making the method static is in fact wise.

What is your opinion on when a method should be static? Is there any benefit to doing this? Impact? I don't see anything wrong with it as it is requiring parameters to run the method so it is not something that would be an issue across multiple users/uses.

Good or Bad?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一人独醉 2024-12-22 04:04:11

方法是否有参数与它无关。唯一的考虑因素是该方法是否尝试访问类的任何非静态(即基于实例的)成员,或者类中的其他静态成员是否调用它......

例如

 private static int Add(int a, int b) 
 { return a + b; }

可以是静态的 - 它不访问其容器类的任何实例成员,即使它有参数

但在下面的类中,PrintHello() 不能声明为静态,因为它访问基于实例的字段 useCount >,即使它没有参数。

public class myClass
{
    private int useCount = 0;

    private void PrintHello()
    { 
        useCount = useCount + 1;
        Console.Write("Hello");
    }
}

Whether or not a method has parameters has nothing to do with it. The only consideration is whether the method attempts to access any non-static (i.e., instance-based ) members of the class, or if some other static member in the class calls it....

For example

 private static int Add(int a, int b) 
 { return a + b; }

can be static - it does not access any instance members of its container class, even though it has parameters

But in the following class, PrintHello() cannot be declared as static since it accesses the instance-based field useCount, even though it has no parameters.

public class myClass
{
    private int useCount = 0;

    private void PrintHello()
    { 
        useCount = useCount + 1;
        Console.Write("Hello");
    }
}
难以启齿的温柔 2024-12-22 04:04:11

正如其他人所指出的,参数的存在不是一个考虑因素。
当该方法不访问任何实例成员时,就会出现提示。

什么时候有用?

当候选方法不需要状态来执行其功能时,不应要求该函数的调用者拥有或创建父类的实例。

通过删除此要求,调用代码将显示错误(或在某些语言中为警告),指示相关方法“无法通过实例引用访问”,需要类型引用。

当重写调用语句以使用类型引用时,可能会发现不需要该类型的原始实例,并且可以进一步重构代码以消除相同类型的创建。

消除该实例类型将节省 cpu 和内存。

调用方法(或其自身的调用者)可能会更容易阅读,因为相关方法不需要实例化代码。

此外,如果缺少实例化代码,这将增加一个或多个方法的可读性。

例如,System.Math 类是静态的,因此充满了静态函数。如果这些函数的调用代码必须在执行之前实例化数学类的实例,那么其可读性就会较差。

As others have indicated, the presence of parameters is not a consideration.
The hint is present when the method does not access any instance members.

When is this useful?

When a candidate method requires no state to perform it's function, it should not be required that the caller of this function possess or create an instance of the parent class.

By removing this requirement, the calling code will surface errors (or in some languages warnings) indicating that the method in question "cannot be accessed with an instance reference" requiring a Type Reference instead.

When the calling statement is rewritten to use a Type reference, it may be discovered that the original instances of the type is not required and code may be refactored further to eliminate he creation of same.

The elimination of this instance type will save cpu and memory.

The calling method (or it's own callers) will likely be easier to read since instantiation code is not needed for the method in question.

Additionally if instantiation code is missing, this will add to the readability of one or more methods.

For example the System.Math class is static and therefore filled with static functions. Calling code for these functions would be less readable if it had to instanciate an instance of the math class prior to execution.

染年凉城似染瑾 2024-12-22 04:04:11

我通常会避免使用静态方法,因为它们很难与单元测试和依赖项注入一起使用。 (我知道打字机)。
只有当该方法被紧密包含时,我才会考虑使用静态方法。

I usually stay a way from using static methods, because they can be difficult to use with unit testing and dependency injection. (I know about typemock).
Only if the method is tightly contained would I consider using a static method.

丢了幸福的猪 2024-12-22 04:04:11

就我个人而言,我对这个问题有两种看法。

一方面,有人可能会认为该方法应该设为静态,因为它不访问任何实例成员。虽然这有一定道理,但我并不总是急于去做这件事,有几个原因:

  1. 命名空间污染。如果我们养成这种习惯,它就会开始带有“美好时光”的味道,那时所有东西都集中到全局模块中,你可以随意调用函数。我一想到这里就不寒而栗。诚然,静态方法被组织成类和命名空间,但它们仍然不是特定于实例的,对我来说它们只是全局函数的味道。它们让我的脚趾卷曲,就像指甲划在黑板上的声音。

  2. 仅仅因为方法今天不访问实例变量并不意味着明天也不会。为此进行重构是一个重大更改。将方法设为静态并不是一个应该轻易做出的决定。您这样做是因为您知道该方法旨在以这种方式使用,而不是因为工具注意到今天您没有使用实例变量。

话虽如此,我确实相信静态方法具有非常有用的用途,并且如果使用得当,可以出色地解决无数问题。当我打算使用它们时,我会为了非常具体的目的而使用它们,而这些目的通常与 Resharper 是否认为我应该这样做无关。

我还没有看到任何内容表明如果将方法标记为静态,应用程序将更有效地执行。我可能倾向于认为静态类会降低性能,因为它们在应用程序的生命周期中存在,并且它们分配的任何资源在应用程序超出范围之前不会被释放。 (另一方面,根据实施情况,这可能会导致性能提升。)

最终,性能问题归结于实施。

Personally, I'm of two minds on the subject.

On the one hand, one could argue that the method should be made static because it doesn't access any instance members. And while there's some truth to that, there are several reasons I'm not always in a big hurry to run out and do that:

  1. Namespace pollution. If we get into this habit, it starts to smack of the "good ol' days" when everything was lumped into global modules and you could just invoke functions willy nilly. I shudder at the thought. While static methods are, admittedly, organized into classes and namespaces, they are still not instance specific, and they just smack of global functions to me. They make my toes curl like the sound of fingernails on a chalk board.

  2. Just because a method doesn't access instance variables today doesn't mean it won't tomorrow. And refactoring it to do so is a breaking change. Making a method static isn't a decision that should be made lightly. You do it because you KNOW the method is intended to be used that way, not because a tool noticed that today you aren't using instance variables.

Now, having said that, I do believe that static methods serve a very useful purpose and, when used properly, solve myriads of problems splendidly. I'll use them when I intend to and for very specific purposes that frequently have nothing whatsoever to do with whether or not Resharper thinks I should do it.

I haven't seen anything that states that the application will perform more efficiently if a method is marked static. I might be inclined to believe that static classes degrade performance, since they exist for the lifetime of the application, and any resources they allocate won't be released until the application goes out of scope. (On the other hand, depending on implementation, this might result in a performance boost.)

Ultimately, the performance issue comes down to implementation.

马蹄踏│碎落叶 2024-12-22 04:04:11

我个人会让它们静态。感觉更正确的是,如果某些东西在实例上不起作用,它不应该成为实例上的成员(而是类上的成员)。

例如,一个接受两个参数并将它们相加并返回结果的 add 方法不需要更多的东西来处理自身,因此可以是静态的。如果您将其保留为实例方法,则可能意味着它会产生影响,而您运行它显然不会产生影响!

我不认为在效率方面有任何优势(尽管我没有仔细研究它),只是在代码的可读性方面。

I'd personally make them static. It feels more right that if something doesn't work on the instance it shouldn't be a member on the instance (but on the class).

For example an add method that takes two parameters and adds them and returns the result will need nothing more to process itself so can be static. If you left it as an instance method it might imply that it will make a difference which isntance you run it on which it clearly won't!

I don't imagine there is any advantage in terms of efficiency (though I haven't looked into it closely), just in readability of code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文