views:

90

answers:

7

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?

+3  A: 

Your accept rate is horrible, so start going through your list and accept good answers, people take time (just like this time) to help.

To answer your question, 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.

JonH
At least you're consistent in misspelling consistent. ;-)
Wim Hollebrandse
Very consistant with consistant...:)
JonH
I have actually been on vacation on and off the last view weeks and have not had time to really read through the responses to my questions. That being said, your comment about my accept rate is correct and justified. I will make it a point to fix that. (Did not even know about the accept rate until you mentioned it...)
Jason
Sorry didn't mean it to offend you, was just making a note of it. It helps others know that you too are helping the community so you will get better responses. It means you care :).
JonH
Not offended at all...thank you for pointing it out.
Jason
+11  A: 
  1. getBooks() is when you are getting all the books associated with an object, it implies the set is already defined.
  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() 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.
fuzzy lollipop
Nice breakdown.
Mr-sk
I'd add that I'd use fetch/retrieve if the data were to be retrieved from a data base
Liz Albin
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
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
fuzzy lollipop
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
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.
fuzzy lollipop
@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
loadBooks() __access__ would be nested inside getBooks()
fuzzy lollipop
A: 

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
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
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
@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
@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
A: 

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.

I second the vote for accepting answers, please.

Ninefingers
A: 

Consistency with a good memory (I have a bad memory so consistency is effected) you can create a document for your reference.

Vishal
A: 

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.

NickLarsen
A: 

As long as you're consistent across your API, that's the main thing.

I quite like this approach

Author.GetBooks(); // Instance method
Book.Search( ... ); // Static method, where ... represents the criteria for searching a DB
Book.Load( Stream ); // Load a book from a file
Rowland Shaw
It would be better to have Library.search(criteria) or Author.search() or Publisher.search(criteria) than on Book because Book.search(criteria) implies than Book has a collections of Books associated with it.
fuzzy lollipop
That's why it would be a static [factory] method.I'd expect Publisher.GetBooks( ... ) or Publisher.GetBooks( ... ), rather than Search() - I'd say it depends on where you're getting your things from.
Rowland Shaw