5

I have a beginners question. I searched a lot for the answer but can't seem to find the exact answer so maybe somebody of more experienced developers can help me with this one.

So, let's say you have a following situation in code (this is simplified version of the situation):

SomeObject a1 = new SomeObject();
a1 = someMethod(a1);

public SomeObject someMethod(SomeObject a1) {
 a1.changeVariable();
return a1;
}

now I heard from several people saying that passing a reference to an object to a method and catching the return value is bad coding practice. Unfortunately nobody can exactly explain to me why it is bad coding practice, and my search for the reason came up with nothing. Can some explain why it is bad practice to do this? I can not think of a situation where something for this can go wrong.

Thanks in advance, you nice people have already helped me with answers to other people countless times, this is a first time I actually needed to post a question :)

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Newbie
  • 53
  • 4
  • 1
    I do not see a "bad practice" in this, especially if you prefer immutable objects. – Tom Dec 12 '14 at 09:05
  • What's the need in returning value.. when we are actually in not need of that ??? – Srikanth Dec 12 '14 at 09:06
  • @Srikanth For example: [Method Chaining](http://en.wikipedia.org/wiki/Method_chaining#Java). – Tom Dec 12 '14 at 09:08
  • 1
    What do you mean by "catching" the return value? It would be useful if you'd explain why you *are* returning the original argument though. Note that your example code won't compile, as you're trying to pass an uninitialized variable (`a1`) into the method. – Jon Skeet Dec 12 '14 at 09:10
  • @Tom thaks for the answer, I read about that. From what I understood when immutable objects are passed, such as String, a copy of the object is created before a start of the method and it is no longer a reference to the original String object that was passed.But what if a muttable object is passed? Is there anything that can go wrong with that scenario? – Newbie Dec 12 '14 at 09:11
  • @Newbie I dont see anything that go wrong – Srikanth Dec 12 '14 at 09:13
  • @Jon Skeet yes sorry I forgot to initialize it, I edited the code. The reason why I return the original is that I need it for later use in the code. And by catching I mean assigning the return value to the already initialized variable a1. And thanks for the prompt reply :) – Newbie Dec 12 '14 at 09:13
  • @Tom Yes you are right. – Srikanth Dec 12 '14 at 09:14
  • 1
    @Newbie Just to make that clear, you have to create the new instance manually, in case of a immutable object. The JVM won't do that manually. Well, I do not see any scenario there this could lead into a problem, if you return the same instance that was provided as the argument (in case of a mutable object). Maybe someone else experienced a problem in this scenario. – Tom Dec 12 '14 at 09:14
  • 1
    May be you could give a real example? One show above is just useless. Whats the point of this returning and assignment? What changes if you switch method to void? – Mikhail Dec 12 '14 at 09:16

1 Answers1

5

In the scenario you've shown, there isn't much obvious benefit. However, as a pattern it can be useful in my experience - particularly if the method performs validation.

Guava's Preconditions.checkNotNull method is a great example of this. It means I can write code like this:

public class Player {
    private final String name;

    public Player(String name) {
        this.name = Preconditions.checkNotNull(name);
    }
}

... instead of having to split the assignment and the validation. It also allows validation to exist as part of another call, e.g. to another constructor:

super(Preconditions.checkNotNull(name));
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Ok so you are saying that if used correctly, outside the immutable situation, there is nothing wrong with writing code like this. Also, like you mentioned above as a pattern it can have certain benefits in simplification (I am guessing the checkNotNull returns what was passed to it, even though in this case it is an immutable object and we will get a reference to a new object instead of the one we passed). Sorry for these questions which may sound stupid but I am just starting with Java and want to get a solid knowledge of the basics from the start. – Newbie Dec 12 '14 at 09:28
  • @Newbie: In your example you *don't* get a reference to a new object. You're returning the same reference passed into it. It also sounds like you have a few misunderstandings around how references and objects work in Java. Nothing automatically creates copies of objects when a reference is passed into a method... – Jon Skeet Dec 12 '14 at 09:48
  • yes, sorry I was talking about your example but confused it with something I read in another article [link](http://stackoverflow.com/questions/1270760/passing-a-string-by-reference-in-java) – Newbie Dec 12 '14 at 10:02
  • @Newbie: As many of the answers there say, the *reference* is copied - not the object. – Jon Skeet Dec 12 '14 at 10:03
  • understood only in that question this was done: zText += "foo"; so a new String was created and the change was not visible outside that method, but this has no impact on my question. Sorry for going of track here. – Newbie Dec 12 '14 at 10:07