Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What is a good way to name a method that checks if X needs to be done, and does X it if necessary?

For example, how to name a method that updates a user list if new users have logged in? UpdateListIfNeeded seems too long, while simple UpdateList implies a possibly expensive and unnecessary operation is done every time. EnsureListUpdated is a variant as well.

C# has a bool TryXXX(args, out result) pattern (e.g. int.TryParse(str, out num)) to check if X is possible and do it, but that is subtly different.

share|improve this question
Just a point of recommendation, methods like this, while well intentioned, should be created with caution. Along the way they tend to get peppered into unrelated code under the guise of "just in case" or "adding a call to it in X() fixed a bug, but I'm not sure why". If you have the discipline to only call RefreshUserList() during LoginUser() and LogoutUser(), and not during, say GetUser() (where the list should be up-to-date already), then by all means the advice below applies. – Kevin McCormick Aug 21 '12 at 18:44
up vote 15 down vote accepted

I tend to use Ensure. It carries the meaning of making sure that something is taken care of, however that needs to be done. If it's already fine, just check it and we're done. Otherwise, do it. Either way, just ensure that it gets done.

share|improve this answer

My preference is UpdateList. If nothing needs to be done, so be it. The expense should always be minimized anyway, so if UpdateList is doing more than necessary something is incorrect in its implementation.

Basically it's the verb that tells you what the method should do. CalculateX should always recalculate X. GetY should always return Y, but only do the work of retrieving it if necessary. Likewise, UpdateZ requests an update of Z assuming it's done the most efficient way possible.

share|improve this answer

Call me "Maybe"

Co-opted from Haskell snippets I've seen, try

share|improve this answer
I suppose if I understood Haskell better, this might make more sense. Corollary: If this is used in a language other than Haskell, it will only be understood by those who know Haskell. – Robert Harvey Aug 21 '12 at 17:59

Your Answer


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.