Suggest how to name the function "get / create and receive"

I am working on a function that returns an object from a container (e.g. Array) by value (e.g. by name). If the object does not already exist, a copy of the default object is created and returned (default constructor).

Thus, this ALWAYS function returns an object, regardless of whether the object existed before the function was called.

Now my question is: what should I call this function? GetOrCreateThenGet sounds silly. Any ideas?

Edit: I feel that something like this will be closer to the essence of the function: GetForSure ...

+6
source share
3 answers

Why do you need nothing but getXXX() . Any method that has and / or means that it does a lot and mixes problems and seriously violates the Single Responsibility Principle .

If the caller really cares whether the object is created on demand, if all they need from the contract is to get the object?

getOrCreate implies that the get or object will be created but not returned.

Again, why should I worry about the create part?

If I really have to take care, would getOrCreateAndGet be semantically correct, but extremely unorthodox and prone to discussions with peers about methods that do many things, and mixing problems and breaking the Single Responsiblity Principle ? Just because other people call things ambitious and insensitive, it is not a good excuse to do it wrong.

+5
source

The best name for the function would be “Provide,” for example, if you provide goods that you can send from a warehouse or make / buy a new one and then deliver.

+2
source

GetGuaranteed seems to cover everything a subscriber needs to know ...

-1
source

Source: https://habr.com/ru/post/943570/


All Articles