3

I have a class which implements an interface and have responsibility to create item and "maintain" collection of created items.

Class have method GetOrCreate(int id) which return instance of created item.

public class PoolOfItems : IPoolOfItems
{
    public Item GetOrCreate(int id) { }
}

Requirements for GetOrCreate method that in case when item, for given id, is already created, we need provide already created instance.

What is the most "understandable" name for method GetOrCreate can be used?

I afraid that name GetOrCreate describes implementation of the interface. And Get and Create names can be misleading when used with implementation.

  • Naming can be considered as "opinion-based" only when you don't care about used names in your code base. If you invest some effort in naming(hoping it pays you back when you read the code) - you will end up with different rules and best practices in same way as some design pattern question can be "opinion-based" because you can use "Factory" or "Builder" pattern – Fabio Mar 6 '17 at 15:47
  • 1
    "What is the name of this thing" questions are allowed here, even though most of them don't produce a viable answer (because programmers think everything has a name). However, "What should I name this thing" questions are specifically off-topic here. See Are “name that thing” questions on-topic, and On the troubles of naming and terminology – Robert Harvey Mar 6 '17 at 16:31
  • If you are concerned about exposing details of the implementation, just call the method "GetInstance". However, the fact that the same instance may be reused, could have severe consequences for the user of the pool, if that instance contains any mutable data. – Frank Hileman Mar 6 '17 at 16:57
  • For what it's worth, I think "GetOrCreate" is already pretty clear. I don't really understand what the last paragraph in your question means. – Robert Harvey Mar 6 '17 at 17:06
  • Last paragraph is about concern that another implementation of IPoolOfItems can create new instance of Item on every call. In this case name GetOrCreate is misleading – Fabio Mar 6 '17 at 18:06
2

GetOrCreate communicates that the method can create an item. If that behavior is part of the method's contract then it should be communicated to users of the interface. If the method were just called Get, callers would be left wondering what happens when there is no item with the specified ID. Does it return null? Throw an exception? GetOrCreate avoids that ambiguity.

On the other hand, if creating an item isn't part of the method's contract but is a possible implementation of its contract, GetOrCreate is problematic because it implies a certain behavior that may not match the real implementation. In this case the more ambiguous Get would be more appropriate, but the method should be documented accordingly.

  • +1 Personally, I'd call it get and include a doc comment specifying the required behaviour of the method and circumstances that the behaviour is implementation specific (such as if no object exists). – Maybe_Factor Mar 9 '17 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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