Naming Conventions - What's a Good Name for a Method That Gets or Creates an Object?fleeting
- Référence externe : https://stackoverflow.com/questions/3184197/whats-a-good-name-for-a-method-that-gets-or-creates-an-object
In most cases a simple GetFoo suffices as the caller doesn’t need to know that you are creating and caching it. That’s what encapsulation is all about.
However, in some circumstances, creating is an expensive operation, so it’s useful to know that you may be creating something on demand and in some cases it will be slow. In this case, a different naming convention makes it clearer to the caller. GetOrCreate() or Get(Options.CreateIfMissing) is a good hint to the caller.
(The behaviour should of course be noted in the documentation, but it’s good to use a method name that reminds people about side effects while they are reading the code, without them having to bring up and read the documentation for every method that is called)
I use Get when I know the retrieval time will be very short (as in a lookup from a hash table or btree).
Find implies a search process or computational algorithm that requires a “longer” period of time to execute (for some arbitrary value of longer).
In my opinion, it’s a generally a bad practice to create something in a procedure that is not documented as having the powers of creation. Either name the procedure getOrCreate… or have a separate create… procedure and then if you really want, have a getOrCreate… that first attempts get…, and if that fails, calls create… and then calls get….
The user of the library will probably not expect the get… procedure to create if the get operation fails. If they suddenly find out that their test calls to get… created a whole tonne of data, they will probably be rather surprised. And how do they clean it up? What if they write code thinking that they will get an error if get… fails and they want to handle that their way?
Django get_or_create Django get_or_create accomplishes a couple of things:
- It’s a convenient way to avoid boilerplate get and create code
- More importantly, it allows you to avoid duplicates when you have multiple processes trying to create objects Here’s how you would usually deal with get/create:
try: author = Author.objects.get(name=‘Leo Tolstoy’) except DoesNotExist: author = Author(name=‘Leo Tolstoy’) author.save()
You try to grab the author from the database, if you can’t find him, you create him. If you have multiple requests executing this code, there can be a race condition. It’s possible that both threads will find that the author doesn’t exist and both will create that author, creating duplicates.
Django get_or_create on the other hand is atomic (assuming everything is configured correctly, more on that below), meaning that it’s a single operation and so can be used concurrently. Here’s how to use it:
author, created = Author.objects.get_or_create(name=‘Leo Tolstoy’)