Comment on accessor coding

?

-

Please comment on the following and provide a professional alternative
if possible:

public Object getObject(Object object) {
Object obj = null;

// if (object != null) { <---- Qn 1: Should I check for null?
for (Object o : objects) {
if (object.equals(o) { <---- Qn 2: Or if (o.equals(object)) ?
obj = o; If i do this, then I don't
break; need to check for null.
}
}
//}

return obj;
}
 
F

Fred L. Kleinschmidt

- said:
Please comment on the following and provide a professional alternative
if possible:

public Object getObject(Object object) {
Object obj = null;

// if (object != null) { <---- Qn 1: Should I check for null?
for (Object o : objects) {
if (object.equals(o) { <---- Qn 2: Or if (o.equals(object)) ?
obj = o; If i do this, then I don't
break; need to check for null.
}
}
//}

return obj;
}

This method seems to be pretty useless. Why do you need to get an object
you already have?

Unless you are checking to see whether the input object is in your
current array of objects. In that case, getObject is a poor choice for
the nameof this method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top