In this NotePadProvider sample code, I noticed that the ContentValues parameter is duplicated even if it’s not null:
ContentValues values;
if (initialValues != null) {
values = new ContentValues(initialValues);
} else {
values = new ContentValues();
}
On the face of it, this looks redundant. It looks like it would be more efficient to just write:
ContentValues values;
if (initialValues != null) {
values = initialValues;
} else {
values = new ContentValues();
}
But that’s not what the author of the sample chose to do and my question is Why?
They are not equivalent. The original code creates a copy of ‘initialValues’, the method then proceeds to modify the new instance in the variable
value. This way theContentValuesinstance supplied to thatinsertmethod is not modified by this method.Your version does modify the original object, which might have unwanted side-effects in other areas of the program.