Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Side effects in Java methods

Tags:

java

This might be a trivial question, but I need some clarification... There is a book called Clean Code that says that our methods should be small, preferably up to 5-10 lines long. In order to achieve that we need to split our methods into smaller ones. For instance, we may have someMethod() shown below. Let's say, modification of 'Example' takes 5 lines and I decide to move it into a separate method, modify 'Example' there and return it back to someMethod(). By doing this, someMethod() becomes smaller and easier to read. That's good, but there is a thing called "side effects" which says that we shouldn't pass an object to another method and modify it there. At least, I was told that it's a bad idea ) But I haven't seen anything prohibiting me from doing so in Clean Code.

public Example someMethod() {

    // ... different lines here

    Example example = new Example();
    example = doSomethingHere(example, param1, param2, ...);    

    // ... different lines here

    return example;
}

private Example doSomethingHere(Example example, 'some additional params here') {

    // ... modify example's fields here ...

    return example;
}

So, am I allowed to split the methods this way or such a side effect is prohibited and instead I should deal with a rather long-line method that definitely breaks Clean Code's rules talking about short methods?


UPDATED (more specific name for the sub-method)

public Example someMethod() {

    // ... different lines here

    Example example = new Example();
    example = setExampleFields(example, param1, param2, ...);    

    // ... different lines here

    return example;
}

private Example setExampleFields(Example example, 'some additional params here') {

    // ... modify example's fields here ...

    return example;
}
like image 670
sva605 Avatar asked Jul 10 '17 17:07

sva605


People also ask

Does Java have side effects?

Side effects can be unavoidable: Get(x); changes the state of the input. Put(x); changes the state of the output.

What are side effects in OOP?

In OO programming, side effects are when a function does something it doesn't communicate, something that might not be anticipated. Those are side effects. Good functions don't have side effects, whether you're doing OO or functional.

Are side effects bad in programming?

The reason why side effects are bad is because, if you had them, a function can be unpredictable depending on the state of the system; when a function has no side effects we can execute it anytime, it will always return the same result, given the same input.

What is a side effect in Python?

Function is said to have a side effect if it changes anything outside of its function definition like changing arguments passed to the function or changing a global variable.


2 Answers

As JB Nizet commented, it's not actually a side effect if it's the only effect, so any blanket statement that "all side effects are bad" doesn't apply here.

Still, the main question stands: Is this (side) effect okay?

Talking about the principles first, side effects are, in general, dangerous for two reasons:

  • they make concurrency more difficult
  • they obscure/hide information

In your example, there is some information that is hidden. You could call this a potential side effect, and it can be exposed with a question: "Does this doSomethingHere method create a new object or modify the one I pass in?" The answer is important, and even more so if it's a public method. The answer should be trivial to find by reading the doSomethingHere method, especially if you're keeping your methods 'clean', but the information is nonetheless hidden/obscured.

In this specific case, I would make doSomethingHere return void. That way there's no potential for people to think that you've created a new object. This is just a personal approach - I'm sure that plenty of developers say you should return the object you modify. Alternatively, you can pick a 'good' method name. "modifyExampleInPlace" or "changeSomeFieldsInPlace" are pretty safe names for your specific example, imo.

like image 193
Jeutnarg Avatar answered Oct 23 '22 20:10

Jeutnarg


we shouldn't pass an object to another method and modify it there.

Who says that? That is actually a good practice in order to split your function in a way that forms a "recipe" and have specific functions that know exactly how to populate your object properly.
What is not recommended (and probably the source where you got your recommendation misunderstood this rule) is defining a public API and modify the arguments. Users appreciate not having their arguments modified as it leads to less surprises. An example of that is passing arrays as arguments to methods.

like image 35
Jim Avatar answered Oct 23 '22 21:10

Jim