How do I determine the side effects of a Java function?



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?

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

Answer

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

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.

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.



Source: stackoverflow