Method names for getting data

Jason picture Jason · Jan 26, 2010 · Viewed 33.8k times · Source

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?

Answer

user177800 picture user177800 · Jan 26, 2010

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.