is it a bad practice modifing an object passed as an argument inside a method?

java modify passed parameter
a function parameter should not be modified
output arguments should be avoided
when an argument is passed to a method
what is method argument in java
java change parameter value in method
java modifying list in method
java reassign object in method

is it a bad practice modifing an object passed as an argument inside a method?

what is the difference between doSomething1 and doSomething2? the both does the same but in a different way. which are appropriate and why is that?

class Foo {
  constructor () {
    this.bar = { aaa: 123, bbb: 456 }
    this.doSomething1(this.bar)
    this.doSomething2()
  }

  doSomething1(arg) { arg.aaa = 10 },
  doSomething2() { 
    this.bar.bbb = 0
  }

}

the code itself is not practical but just a sample code.

That's the difference between a function and a method. A method, which is a function defined on an object, knows all the properties of the object and is allowed to modified them (also known as mutate or change the state). doSomething2 is a method of Foo class. Hence it should have the right or privilege to mutate any of the properties of an instance of Foo.

doSomething1 on the other hand is a function masquerading as a method. It has no right to modify the argument it receives and so shouldn't. If it really must, then create a new copy with the updated property instead.

Java Method Arguments, Can a Java method change the contents of any variable passed as an argument? T/F In the method header, the method modifier public means that the method belongs to the class, not a specific object. False T/F An argument passed by value to a method could be a constant, a variable, or an expression.

The difference is that doSomething1 can be used to modify any object, while doSomething2() is specifically designed to modify only the bar property of the object Foo object that it's called on.

So doSomething1() is a more general function. Since it doesn't reference this, it doesn't really even need to be in the class, except that the class limits how you can call it.

Also, to call class methods you need to call it on an object, this.methodname(...).

var abc = {};

class Foo {
  constructor () {
    this.bar = { aaa: 123, bbb: 456 }
    this.doSomething1(this.bar)
    this.doSomething2()
    this.doSomething1(abc)
  }

  doSomething1(arg) { arg.aaa = 10 },
  doSomething2() { 
    this.bar.bbb = 0
  }
}

Is "pass by reference" bad design?, design. There are certain scenarios it's valid for, like array position swapping for high performance sorting operations. There are very few reasons you should need this functionality. Thus, when we pass this reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument. This effectively means that objects act as if they are passed to methods by use of call-by-reference. Changes to the object inside the method do reflect in the object used as an argument.

Modern JavaScript is taking a lot of notes from the functional programming world these days. In functional programming you never modify anything. The question you are asking is highly subjective and many people will have different opinions but my opinion is that this will fail a tech test for you instantly. If I saw you do that in a tech interview that would be the instant I made my mind up and you would definitely not get the position based solely on seeing you modify a function parameter no matter how good the rest of the interview went.

Return a new instance of an object.

return { ...arg, prop: 'New value' };

Output arguments, is specified as one of the following object types or values: A single data object of a type. A series of data objects of the same type. hi, i am created by a function passed as an argument. Wrapper function. Wrapper function or decorator allows us to wrap another function in order to extend the behavior of the wrapped function, without permanently modifying it. In Decorators, functions are taken as the argument into another function and then called inside the wrapper function.

Passing String objects as arguments Java, ALWAYS passed by VALUE, it never did anything else. Modifying the attributes of an object argument inside a method is reflected outside of it. Modifying collections and maps inside the method is reflected outside of them. Like This Article?

So it's not 'one method modifying it's arguments', it is 'the method sends a In object-oriented programming, is the keyword "this" or "self" more accurate in The question covers two different situations: pass by reference and pass by value​. To allow a method to modify a argument, you must pass in an object. Objects in Java are also passed by value, however, the value of an object is a reference. So, the effect is that the object is passed in by reference. When passing an argument by reference, the method gets a reference to the object.

Be Careful When Passing Ruby Objects as Method Arguments. Published Sep 18 What happens if we modify an object inside of a method? For a method to modify an argument, it must be of a reference type such as an object or array. Objects and arrays are also passed by value, but the value of an object is a reference. So the effect is that arguments of reference types are passed in by reference.

Comments
  • Not really the correct site to post this, but the answer is, depends.
  • did I post this in a wrong SO community? Im using a mobile app of SO and not sure where my question is posted...
  • Yes, you've used StackOverflow, it's really meant to help with finding bugs with code, and not advice on coding practices. CodeReview is more for this type of question -> codereview.stackexchange.com
  • @Keith This doesn't seem appropriate for CodeReview. That's for real code with a specific goal. Software Engineering takes questions about general design principles, although this seems to minor for them as well.
  • @Barmar Crikey, how many sub-sites does this company have.. :),..
  • Yes. That's what I was referring to.
  • I think doSomething1 should return a new object.
  • Why do you think that?
  • @shu Also doSomething2() won't work, as it should be -. this.doSomething2()
  • I agree. I never modify an object passed as an argument. but, why is this bad practice?
  • Because you never know where else that object is being used. If you create a new instance of the object for the code you are writing the function for you can be 100% certain that you haven't affected another part of the app you were not anticipating. I suggest reading up on functional programming and data immutability.
  • @shu It's not!!, like everything in any programming language, use the right tools for the job.
  • I honestly would have agreed with that statement a year or two ago, but after learning how much more productive and how many fewer defects you get I haven't looked back.
  • but what is the difference between the two methods? both mutate an object.