Method Names for Retrieving Data

Warning: This is not a very serious issue / discussion that I am publishing ... but I bet that most developers have thought about this "problem" ...

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

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

However, I always struggled with the initial action ... how to get the data. It seems that for each project I end up jumping between different naming conventions because I am never 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?

+44
methods naming-conventions
Jan 26 '10 at 18:57
source share
5 answers

It's all about consistent semantics.

In the title of the question, you are using data acquisition. This is very common in the sense that you need to define what it means in a semantically significantly unambiguous way. I offer the following examples to hopefully put you on the right track when thinking of naming things.

  • getBooks() is when you get all the books associated with an object, this means that the set is already defined.
  • findBooks(criteria) when you try to find a subset of books based on the parameters of a method call, it will usually be overloaded with another search criteria
  • loadBooks(source) is when you load from an external source, like a file or db.
  • I would not use fetch / retrieve, because they are too vague and combine with get and there is no unambiguous semantics related to terms.

Comments are proof that general terms such as get and fetch are not specific semantics and are interpreted differently by different people. Choose a semantic term, write down what it is intended to mean that the semantics are not clear and consistent with its use.

+60
Jan 26 '10 at 19:02
source share

The answer is simply to stick to what you are comfortable with and be consistent.

If you have a barnes and nobles website and you use GetBooks (), then if you have another element, such as a Movie object, use GetMovies (). So you and your team like and be consistent.

+7
Jan 26 '10 at 19:01
source share

Honestly, you should just decide with your team which naming convention to use. But for fun, let's see what your train of thought will be to solve any of these problems:

  • GetBooks ()

This method belongs to the data source, and we don't care how it gets them, we just want to get them from the data source.

  • Fetchbooks ()

You treat your data source like a bloodhound, and its job is to get your books. I think you have to decide how much he can dig into his mouth right away.

  • FindBooks ()

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

  • LoadBooks ()

These books belong to some kind of "electronic book bag" and should be loaded into it. Be sure to call ZipClosed () after loading so as not to lose them.

  • RetrieveBooks ()

I have nothing.

+7
Jan 26 '10 at 19:07
source share

It is not clear what you mean by "data acquisition." From the database? File? Memory?

My opinion on the name of the methods is that its role is to eliminate any ambiguities and, ideally, the need to look for documentation. I believe that this should be done even at the cost of longer method names. According to research, most intermediate + developers can read a few words in the case of a camel. With the IDE and the automatic completion of writing long method names is also not a problem.

Thus, when I see "fetchBooks", if the context is not very clear (for example, a class called BookFetcherFromDatabase), it is ambiguous. Where to get it from? What is the difference between fetching and searching? You also risk that some developers associate semantics with specific keywords. For example, the selection for the database (or memory) depending on the download (from a file) or download (from the Internet).

I would prefer something like "fetchBooksFromDatabase", "loadBookFromFile", "findBooksInCollection", etc. This is less obvious, but as soon as you go along the length, it is understandable. Everyone who reads this will immediately understand what you are trying to do.

+1
Jan 26 '10 at 19:03
source share

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

I usually use Load only when I mean files - as in "loading into memory", and this usually means loading into primitives, structures (C) or objects. I use send / receive for the web.

As mentioned above, consistency is all that includes cross-developers.

0
Jan 26 '10 at 19:04
source share



All Articles