2 ms·
Is it bad to completely ignore this behavior? For instance, something like: Dog d = new Dog("fido"); d = alterDog(d); public Dog alterDog(Dog d) { d.name="a
by tunesmith 9y ago
Is it bad to completely ignore this behavior? For instance, something like:
Dog d = new Dog("fido");
d = alterDog(d);
public Dog alterDog(Dog d) {
d.name="arfybark";
return d;
}
It's not quite a pure function since it changes it in place, but at least the parameters and return types reflect the method. I ran across a term for this practice in the past but don't remember what it's called.
- ohf 9y agoI would actively avoid that, but only because it confuses me. That syntax loosely implies that removing the second d assignment would allow me to create new-but-altered dogs with alterDog(Dog), which isn't the case. If I wanted to use assignments like that, I would try to go fully immutable with factory methods that help me make slightly different, new dogs on the fly, and return them.