0

Extending Method names for getting data in a different perspective.

I have a series of GetData methods, which returns data in various forms. I am wondering what best way to name these methods, precise and effective.

  1. public DataTable GetData() - returns the data from a given table
  2. public List GetData() - returns the data in the form of List of objects
  3. public DataSet GetData() - returns the data when there are multiple data tables associated with queries
  4. public objectx GetData(objecty) - returns selected object
  5. public DataTable GetData(objecty) - returns selected object as single row in table
  6. public DataSet GetData(objecty) - returns selected object (ex: in case of master-child)
Community
  • 1
  • 1
Krishna Sarma
  • 1,852
  • 2
  • 29
  • 52

2 Answers2

2

In C#, you can't have method overloads that differ only by the return type. As such, you'll need unique names.

I would recommend using naming based on what is returned. This will make the API more clear when used via intellisense, as well. I'd also use a more specific name than "Data" - for example, if the method is retrieving data representing customers, I'd use:

public DataTable GetCustomerTable() {
public List<Customer> GetCustomers() {
public DataSet GetCustomerDataSet() {

public Customer GetCustomer(int id) {
// I'd question whether these really belong at all...
public DataTable GetCustomerAsTable(int id) { 
public DataSet GetCustomerAsDataSet(int id) { 
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

Naming is pretty subjective, but in my opinion there's 2 things to follow to have "good names".

  1. Be precise.
  2. Follow the principle of least astonishment.

These two principles are important for maintenance; if you have a bunch of methods named GetData, people won't know what to expect and will waste time reading the doc to find the appropriate overload.

You also want to have names representing the level of abstraction you're at. So let's say you want to get some entities named Page. At the DAO level it could easily be public DataTable GetPagesTable();, because you're dealing with tables. At a higher level of abstraction, with the List, you could change it to public List GetPages(); (Removed Table from the name). You're far from the database, so the names should reflect that.

If you're passing an object or ID, say it clearly in the method name to avoid any surprises, like public DataTable GetPagesFromId(int id);

From your examples, I would probably end up with something like that :

public DataTable Get<TableName>Table() - returns the data from a given table
public List Get<ObjectName>s() - returns the data in the form of List of objects
public DataSet Get<ComposedObjectName>() - returns the data when there are multiple data tables associated with queries
public objectx Get<ObjectName>(objecty) - returns selected object
public DataSet Get<ObjectName>AsMasterChild(objecty) - returns selected object (ex: in case of master-child)
Pierre-Luc Pineault
  • 8,993
  • 6
  • 40
  • 55