Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Is there are any standards or conventions on naming methods which add something if it not exist into another something.

For example: Store.instance().addItemIfNotExist(item)

IfNotExist is really ugly part. What workaround may be?

share|improve this question
6  
Why do you think 'addItemIfNotExist' is bad? Would you rather put the 'IfNotExist' part in the documentation and force your users to read the documentation along with the code? They're always going to have to read the code, so the more information you can state in your code (and nowhere else) the better. If you still don't like this, consider instead making your class a "Set", which already has this behavior defined intrinsically. –  Nate W. Feb 1 '13 at 22:32
    
In memory, you would use a set and just call add(). A set can only contain one copy of each value. –  kevin cline Feb 2 '13 at 18:20

2 Answers 2

up vote 3 down vote accepted

Some databases use the term Upsert to mean "Update or Insert as needed", but you may not want the Update aspect.

In general, an addItem() function has an implied, expected failure if the item already exists within the database, so you could use that. Your error handling code after the addItem() call would make it clear that failure because of already existing was okay.

Finally, you could consider addItemExclusive() to better indicate that the routine will check for existence prior to adding the item.

share|improve this answer

an easy one that comes to mind is addIfAbsent()

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.