142

Warning: This is a not very serious question/discussion that I am posting... but I am willing to bet that most developers have pondered this "issue"...

Always wanted to get other opinions regarding naming conventions for methods that went and got data from somewhere and returned it...

Most method names are somewhat simple and obvious... SaveEmployee(), DeleteOrder(), UploadDocument(). Of course, with classes, you would most likely use the short form...Save(), Delete(), Upload() respectively.

However, I have always struggled with initial action...how to get the data. It seems that for every project I end up jumping between different naming conventions because I am never quite happy with the last one I used. As far as I can tell these are the possibilities -->

  • GetBooks()
  • FetchBooks()
  • RetrieveBooks()
  • FindBooks()
  • LoadBooks()

What is your thought?

Jason
  • 2,806
  • 2
  • 28
  • 38

5 Answers5

163

It is all about consistent semantics;

In your question title you use getting data. This is extremely general in a sense that you need to define what getting means semantically significantly unambiguous way. I offer the follow examples to hopefully put you on the right track when thinking about naming things.

  1. getBooks() is when you are getting all the books associated with an object, it implies the criteria for the set is already defined and where they are coming from is a hidden detail.
  2. findBooks(criteria) is when are trying to find a sub-set of the books based on parameters to the method call, this will usually be overloaded with different search criteria
  3. loadBooks(source) is when you are loading from an external source, like a file or db.
  4. I would not use fetch/retrieve because they are too vague and get conflated with get and there is no unambiguous semantic associated with the terms.

Example: fetch implies that some entity needs to go and get something that is remote and bring it back. Dogs fetch a stick, and retrieve is a synonym for fetch with the added semantic that you may have had possession of the thing prior as well. get is a synonym for obtain as well which implies that you have sole possession of something and no one else can acquire it simultaneously.

Semantics are extremely important:

the branch of linguistics and logic concerned with meaning

The comments are proof that generic terms like get and fetch have no specific semantic and are interpreted differently by different people. Pick a semantic for a term, document what it is intended to imply if the semantic is not clear and be consistent with its use.

words with vague or ambigious meanings are given different semantics by different people because of their predjudices and preconceptions based on their personal opinions and that will never end well.

Community
  • 1
  • 1
  • 8
    I'd add that I'd use fetch/retrieve if the data were to be retrieved from a data base – Liz Albin Jan 26 '10 at 19:10
  • 1
    Hmmm. Would you distinguish between wildcard criteria and specific criteria. For example, would you use FindBooks(publisher) when looking for books from a specific publisher? Or maybe GetBooksFromPublisher(publisher)? – Jason Jan 26 '10 at 19:12
  • 6
    publisher.getBooks() would be the prefered way of doing it, where publisher was a specific instance of a Publisher. Library.getBooks(publisher) where publisher implemented a BookSearchCriteria interface. Same with Library.getBooks(author) where author implemented a BookSearchCriteria interface. You would also logically have Library.getPublishers() as a factory method –  Jan 26 '10 at 19:33
  • 3
    Thank you for this answer. Definitely adds some clarity. In essence, you are saying that methods for retrieval by a particular filter should stay with the object. In what way would the interface be used? For example, the BookSearchCriteria interface. Can you provide some sample code? – Jason Jan 27 '10 at 20:44
  • here is a hint for design. you should strive for Loose Coupling and Tight Cohesion, google those theories but here are the cliff notes, Objects should rely on as few other objects as possible and should do one thing and only one thing really well. Context is everything. –  Jan 28 '10 at 13:38
  • @fuzzy lollipop: Nice answer. One further question: According to your answer, `getBooks()` and `loadBooks()` don't seem mutually exclusive. Which name would you choose when both criteria apply (the set of books is clearly defined, however they must be fetched from a DB)? – stakx - no longer contributing Jul 31 '10 at 15:52
  • loadBooks() __access__ would be nested inside getBooks() –  Jul 31 '10 at 19:11
  • Would you say `listBooks()` is equivalent to `getBooks()`, or is there a difference there? – Svish Jan 30 '14 at 14:05
  • @Jarrod Roberson How would you name a method that sets object properties? In my project I use get for methods that return a data, but there are also some methods that works pretty much the same as get, but instead of returning data, they populate object properties with that data, and the prefix is load. What are your thoughts on this? – Kamil Latosinski Aug 02 '16 at 08:38
  • @KamilLatosinski - that is terrible side effect programming based on C idioms from the 70s. [Fluent Builder Pattern](http://www.vertigrated.com/blog/2015/08/fluent-builder-pattern-revisited/) is your friend. –  Aug 02 '16 at 16:20
  • 2
    usually naming is like `fetch` when data **access time** is **low**, i.e. on same device, from local database, from memory.`load`, or `download` if access time is **higher**, from Internet, external DB, from file – János Feb 25 '18 at 09:45
  • I wrote this 8 years ago and it still holds up well. –  Feb 25 '18 at 16:18
  • When you want to load a book with a certain criteria from your external source, would you use `FindBook(criteria, source)` or `LoadBook(source, criteria)`? And, what would be a good name for a function that `creates` an object in your external source (inserting a row in your database). Would `create_and_save` be appropriate? Edit: Any chance we could extend the list of "good semantics"? – BARJ Jun 18 '19 at 03:30
  • I would interpret 'loadBooks' as 'tell the object to load its books for quicker future access'. A 'fetchBooks' function however, I would interpret to do the same as the 'loadBooks' function but with the difference that the books would also be returned from it. – Lukas Kalinski Mar 19 '20 at 21:06
  • Another good point is the expected outcome of the operation, as pointed out on [stackexchange](https://softwareengineering.stackexchange.com/a/182120/258760) as well: “I would say that `find` may fail but `get` shouldn't.” – pixelbrackets Aug 12 '20 at 09:39
16

Honestly you should just decide with your team which naming convention to use. But for fun, lets see what your train of thought would be to decide on any of these:

  • GetBooks()

This method belongs to a data source, and we don't care how it is obtaining them, we just want to Get them from the data source.

  • FetchBooks()

You treat your data source like a bloodhound, and it is his job to fetch your books. I guess you should decide on your own how many he can fit in his mouth at once.

  • FindBooks()

Your data source is a librarian and will use the Dewey Decimal system to find your books.

  • LoadBooks()

These books belong in some sort of "electronic book bag" and must be loaded into it. Be sure to call ZipClosed() after loading to prevent losing them.

  • RetrieveBooks()

I have nothing.

Nick Larsen
  • 18,631
  • 6
  • 67
  • 96
  • 4
    The accepted answer didn't distinguish well between "get" and "fetch" as well as this answer. Fetch implies it has to be done by someone else because it will take some time or requires some expertise to fulfill, whereas get implies it's instantaneous and available verbatim or "off the shelf". – Sridhar Sarnobat Jul 10 '18 at 03:14
  • I too am having trouble with the accepted answer today. For my project (React/Redux-based) I believe it is important to distinguish between data being pulled from the Redux store vs the app's database vs a third party API. Getting this right will definitely help with readability in the future. The original developer used `add` for both writes to a database and writes to the Store. Now I'm trying to separate those out, and it is a pain. – tim.rohrer May 03 '19 at 13:38
11

The answer is just stick to what you are comfortable with and be consistant.

If you have a barnes and nobles website and you use GetBooks(), then if you have another item like a Movie entity use GetMovies(). So whatever you and your team likes and be consistant.

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
JonH
  • 32,732
  • 12
  • 87
  • 145
4

It is not clear by what you mean for "getting the data". From the database? A file? Memory?

My view about method naming is that its role is to eliminate any ambiguities and ideally a need to look up documentation. I believe that this should be done even at the cost of longer method names. According to studies, most intermediate+ developers are able to read multiple words in camel case. With IDE and auto completions, writing long method names is also not a problem.

Thus, when I see "fetchBooks", unless the context is very clear (e.g., a class named BookFetcherFromDatabase), it is ambiguous. Fetch it from where? What is the difference between fetch and find? You're also risking the problem that some developers will associate semantics with certain keywords. For example, fetch for database (or memory) vs. load (from file) or download (from web).

I would rather see something like "fetchBooksFromDatabase", "loadBookFromFile", "findBooksInCollection", etc. It is less sightly, but once you get over the length, it is clear. Everyone reading this would right away get what it is that you are trying to do.

Uri
  • 88,451
  • 51
  • 221
  • 321
  • 4
    The problem with fetchBooksFromDatabase is when you want to factor / generalize and fetchBooks then could be some pulling of data from say XML. I like specifics too but you then find yourself seperating the same functionality across different function *names*. And that sir is not cool! – JonH Jan 26 '10 at 19:07
  • I think the difference between Get versus Find tends to be obvious but I wonder about the question regarding Get versus Fetch. Hell..is there a difference? – Jason Jan 26 '10 at 19:23
  • @JonH: I agree with you on that. However, if you knew in advance that you would have different types of fetch, you would have encoded that in the class naming(so you could interpret the meaning from the context, e.g., DatabaseConnector vs. XmlConnector). – Uri Jan 26 '10 at 19:40
  • 9
    @Jason: Because "getters" are so ubiquitous (and are part of frameworks like JavaBeans), many programmers tend to think of them as almost-transparent means of accessing a data field. "Fetch", on the other hand, indicates to some programmers a more lengthy access that involves transfer of data from place to place (a-la CPU fetching) or database fetching. For instance, Hibernate's query language has a Fetch construct. Developers often use methods based on their expectations of the name rather than by reading documentation so avoiding sending the wrong signal is crucial. – Uri Jan 26 '10 at 19:42
3

In OO (C++/Java) I tend to use getSomething and setSomething because very often if not always I am either getting a private attribute from the class representing that data object or setting it - the getter/setter pair. As a plus, Eclipse generates them for you.

I tend to use Load only when I mean files - as in "load into memory" and that usually implies loading into primitives, structs (C) or objects. I use send/receive for web.

As said above, consistency is everything and that includes cross-developers.

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246