Win a copy of liveProject: Build an ML Recommender System this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

How do you name your methods

Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most of methods are related to some kind of data retrieval. Naturally, what is the point to ask someone to do something and do not get result. Some people name lengthy data retrieval operations like:
List findSomething() or
List searchSomething() or ... there are quite few versions.

What I do is I just name them getSomething(). It does not matter if it is 1 or 15 lines of code. It does not matter if it works 0.1 sec or 5 mins. So my code consists of 80% of getters. Which are not normal getters that return reference to of class field. They are logic, they are code behind the application.

If programmer follows the first model, it's easier to spot for the caller which method will run faster and which may be slow. I.e. find should take less time than get. Also by properly naming method you may imply implementation details.
In my case, the caller does not care how long it takes. Because the method name does explain the implementation details. I can change the method implementation anytime and I will not have to change find to get etc.

I am more seeing first model , than a second one. I am sometimes scared myself how many "getters" I have! But it works... And it works nice with JSP and Struts too.
So how do you name your methods? And why?

(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getSomething() is fine for getters. In fact it's a standard for Java Beans and a lot of IDEs can generate the code for you.

Frameworks are full of these beans because standardized getters and setters are easy to automate. The beans we use for Data Transfer Object pattern and other Java Bean patterns are very common but they are not the whole world.

The problem is that when we can we like objects to hide their data. For "rich object models" with "good" domain objects, we focus on behavior. One guideline is "tell, don't ask" meaning tell an object what to do rather than getting the data and doing the work yourself. Knight's Principles lay this idea out in easy and entertaining terms.

Does the difference between data transfer objects in frameworks and rich domain objects make sense to you?
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic