如何确定Java函数的副作用?

时间:2021-12-28 15:37:23

I'm working on an object, specifically its one function, which looks like this:

我正在处理一个对象,特别是它的一个函数,它看起来像这样:

public class Dog {
    private ArrayList<Paw> paws;
    private double age;
    private Tongue tongue;

    public Dog(ArrayList<Paw> paws, double age, Tongue tongue) {
        this.paws = paws;
        this.age = age;
        this.tongue = tongue;
    }

    public void bark() {
        // ... about 100 lines of side effects operating
        // on the object's global members ...
    }
}

I really want to fix these side effects and refactor the object into functions that do only one thing.

我真的想要修复这些副作用并将对象重构为只做一件事的函数。

Is there an automated process in Eclipse to mark possible side effects?

Eclipse中是否有自动化过程来标记可能的副作用?

If not, is there a manual algorithm I can follow so I don't get lost down the rabbit hole?

如果没有,是否有我可以遵循的手动算法,所以我不会迷失在兔子洞里?

3 个解决方案

#1


8  

I would create a unit test which tests your old Dog class including all "side effects" (not quite sure what you mean here).

我会创建一个单元测试来测试你的旧Dog类,包括所有“副作用”(不太清楚你在这里的意思)。

Assuming your unit test passes, you can then start the refactoring (using Eclipse, selecting appropriate lines and using right-click, Refactor, Extract Method could be your friend here) and keep using your unit test to check whether the refactoring has broken anything.

假设您的单元测试通过,您可以开始重构(使用Eclipse,选择适当的行并使用右键单击,Refactor,Extract Method可能是您的朋友)并继续使用单元测试来检查重构是否已经破坏了任何内容。

EDIT:

To find out what else is changing the attributes of your Paw and Tongue classes, you could set modification watchpoints (i.e. set a breakpoint on the attributes, right click on the breakpoint, Breakpoint properties, untick "access") on any attributes used by bark(), then when you are running your app in the debugger, every time anything changes a property, the debugger will stop.

要找出改变Paw和Tongue类属性的其他内容,您可以在树皮使用的任何属性上设置修改观察点(即在属性上设置断点,右键单击断点,断点属性,取消“访问”)。 (),然后当您在调试器中运行应用程序时,每次更改属性时,调试器都将停止。

#2


1  

I don't know of an automated process.

我不知道自动化过程。

As for a manual algorithm, if you make all the class fields final, you will get an error most places they are modified as a side effect of bark(). (An alternative is to rename them but that also flags reads from them) So you can find the side effects very easily. Slowly refactor into smaller functions until bark() has no errors.

至于手动算法,如果你使所有的类字段都是最终的,那么你将修改大多数地方作为bark()的副作用。 (另一种方法是重命名它们,但也标记从它们读取)所以你可以很容易地找到副作用。慢慢重构为较小的函数,直到bark()没有错误。

The ArrayList paws will not be covered by the final trick, cause you can still add(), remove() etc. from it. So that is probably one to rename.

最终技巧不会覆盖ArrayList爪子,因为你仍然可以添加(),删除()等。所以这可能是一个重命名。

#3


1  

Is there an automated process in Eclipse to mark possible side effects?

Eclipse中是否有自动化过程来标记可能的副作用?

No, there isn't.

不,没有。

If not, is there a manual algorithm I can follow so I don't get lost down the rabbit hole?

如果没有,是否有我可以遵循的手动算法,所以我不会迷失在兔子洞里?

You can start by commenting (ctrl + /) all the object's global members. So in your function you will have words underlined on red, which you have to repair with argument passing or local variables. Later uncomment members. In Eclipse you can also use a shortcut Ctrl + Shift + M for method extracting. For refectoring there is also a very useful shortcut Ctrl + Shift + R for changing variable names.

您可以从注释(ctrl + /)所有对象的全局成员开始。因此,在您的函数中,您将在红色下划线,您必须使用参数传递或局部变量进行修复。后来取消注释成员。在Eclipse中,您还可以使用快捷键Ctrl + Shift + M进行方法提取。对于重构,还有一个非常有用的快捷键Ctrl + Shift + R用于更改变量名称。

#1


8  

I would create a unit test which tests your old Dog class including all "side effects" (not quite sure what you mean here).

我会创建一个单元测试来测试你的旧Dog类,包括所有“副作用”(不太清楚你在这里的意思)。

Assuming your unit test passes, you can then start the refactoring (using Eclipse, selecting appropriate lines and using right-click, Refactor, Extract Method could be your friend here) and keep using your unit test to check whether the refactoring has broken anything.

假设您的单元测试通过,您可以开始重构(使用Eclipse,选择适当的行并使用右键单击,Refactor,Extract Method可能是您的朋友)并继续使用单元测试来检查重构是否已经破坏了任何内容。

EDIT:

To find out what else is changing the attributes of your Paw and Tongue classes, you could set modification watchpoints (i.e. set a breakpoint on the attributes, right click on the breakpoint, Breakpoint properties, untick "access") on any attributes used by bark(), then when you are running your app in the debugger, every time anything changes a property, the debugger will stop.

要找出改变Paw和Tongue类属性的其他内容,您可以在树皮使用的任何属性上设置修改观察点(即在属性上设置断点,右键单击断点,断点属性,取消“访问”)。 (),然后当您在调试器中运行应用程序时,每次更改属性时,调试器都将停止。

#2


1  

I don't know of an automated process.

我不知道自动化过程。

As for a manual algorithm, if you make all the class fields final, you will get an error most places they are modified as a side effect of bark(). (An alternative is to rename them but that also flags reads from them) So you can find the side effects very easily. Slowly refactor into smaller functions until bark() has no errors.

至于手动算法,如果你使所有的类字段都是最终的,那么你将修改大多数地方作为bark()的副作用。 (另一种方法是重命名它们,但也标记从它们读取)所以你可以很容易地找到副作用。慢慢重构为较小的函数,直到bark()没有错误。

The ArrayList paws will not be covered by the final trick, cause you can still add(), remove() etc. from it. So that is probably one to rename.

最终技巧不会覆盖ArrayList爪子,因为你仍然可以添加(),删除()等。所以这可能是一个重命名。

#3


1  

Is there an automated process in Eclipse to mark possible side effects?

Eclipse中是否有自动化过程来标记可能的副作用?

No, there isn't.

不,没有。

If not, is there a manual algorithm I can follow so I don't get lost down the rabbit hole?

如果没有,是否有我可以遵循的手动算法,所以我不会迷失在兔子洞里?

You can start by commenting (ctrl + /) all the object's global members. So in your function you will have words underlined on red, which you have to repair with argument passing or local variables. Later uncomment members. In Eclipse you can also use a shortcut Ctrl + Shift + M for method extracting. For refectoring there is also a very useful shortcut Ctrl + Shift + R for changing variable names.

您可以从注释(ctrl + /)所有对象的全局成员开始。因此,在您的函数中,您将在红色下划线,您必须使用参数传递或局部变量进行修复。后来取消注释成员。在Eclipse中,您还可以使用快捷键Ctrl + Shift + M进行方法提取。对于重构,还有一个非常有用的快捷键Ctrl + Shift + R用于更改变量名称。