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.

This is a question about function naming. I am writing some code that updates the structure of a database according to a specification. Somewhere in this code I have something like this:

void createIndices(Table tbl){
    for(Index idx : table.getIndices()){
        if(notExists(idx)){
            createIndex(idx);
        }
    }
}

void createIndex(Index idx){
    ...
}

As you can see, the function createIndices is not well named because it doesn't quite create the indices. It creates only those indices that do not already exist. I can come up with a variety of names for the function:

  • createIndicesIfNotExist
  • createIndicesThatDoNotExist
  • etc.

However, because "if not exists" is such a recurring idiom in programming, I was wondering if there is some kind of standard for its plural form. Just like createIndexIfNotExists could be considered the standard singular form, even if it's not grammatically correct.

share|improve this question

closed as primarily opinion-based by gnat, MichaelT, GlenH7, Bart van Ingen Schenau, Dan Pichelman Jun 30 at 0:55

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

12  
I'd call it createMissingIndices. –  Lee Jun 29 at 12:17
2  
@Lee createMissingIndices sounds good to me –  abl Jun 29 at 12:21
    
    
@gnat I can see this question is likely to generate opinionated discussion. However, I have not asked for an opinion, or "what would you do", but rather whether there is some de facto standard that I am not aware of for naming functions with similar semantics. –  abl Jun 30 at 14:12

2 Answers 2

In this specific context and for most cases, 'missing' is probably the best.

For other cases 'notIn' or 'nonExistent may be more appropriate.

share|improve this answer
2  
“Nonexistent” is one word, so it wouldn’t be nonExistent but nonexistent. –  Jon Purdy Jun 29 at 21:32
    
Kind of. Non-existent is usually spelt with a hyphen and my inclination is to capitalise hyphenated words. –  nevster Jun 30 at 2:16

I tend to name functions for their purpose rather than their implementation, so would use "EnsureIndicesExist"

share|improve this answer

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