Performance/good practice question – should I check if object exists or just delete it?
So:
obj = getObj(someid);
if(obj != null) {
deleteObj(someId);
}
Or just:
deleteObj(someId);
?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
From a performance standpoint you will likely be better off just making an attempt at deleting a record based on id versus trying to fetch the record and then going back to the database to delete it. It’s always good practice to limit the number of transactions on your database.
You will know if you deleted any records based on the row count that is returned from a query similar to the one above.