Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

eclipse

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?

like image 825
sdasdadas Avatar asked Jan 16 '13 22:01

sdasdadas


People also ask

What is a Java side effect?

A side effect method is a method which modifies some state variable value/arguments passed having a consequence beyond its scope, that is to say it has an observable effect besides returning a value (the main effect) to the invoker of the operation.

What is a side effect in a function?

A side effect is when a function relies on, or modifies, something outside its parameters to do something. For example, a function which reads or writes from a variable outside its own arguments, a database, a file, or the console can be described as having side effects.

What expression is side effect?

those expressions that, besides calculating a value, correspond to an operation on memory, such as an assignment (simple or combined) or an increment. We call these expressions-with-side-effect. Recall that we use the term side-effect to indicate a modification of the state (i.e., the memory) of the program.

When does a method produce a so called side effect?

In computer science, a function or expression is said to have a side effect if, in addition to producing a value, it also modifies some state or has an observable interaction with calling functions or the outside world.


2 Answers

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.

like image 144
beny23 Avatar answered Oct 11 '22 17:10

beny23


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.

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.

like image 37
user949300 Avatar answered Oct 11 '22 17:10

user949300