0

I have a method, that returns a List of strings. For some reason I do not allow the method to return a List with more than one string in it. Is it better to name such a method GetEntry or GetEntries?

Facundo Casco
  • 10,065
  • 8
  • 42
  • 63
AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120

3 Answers3

2

Well, it should be getEntry, since it only returns a string; anyway this sounds like more of a personal problem, and not very much of a code problem.

James Litewski
  • 451
  • 2
  • 6
  • 16
1

It's best to make the method return a string rather than a list, but if that is not possible then you should name it so that its name indicates what it does, so GetEntry would be better or even GetSingleEntry to make it more explicit.

Edit As it seems you are not going to necessarily return a list I would just got for GetEntry, i would only use GetSingleEntry if, for whatever reason, you were returning a list which only had a single entry to make that clear to the callers. this would not be necessary if the method only returns a string, so in that case GetEntry would be sufficient.

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • The reason for this is I draw data from a database. Normally, I should only get one row back but I want to collect all data returned. But because receiving more than one row is not "allowed", I want to throw an exception in the method. Do you think this is a good way to handle it? – AGuyCalledGerald Apr 07 '11 at 10:17
  • if getting more than 1 row is an error then yes it might be appropriate to throw an exception, depending on if there is anything you can do about this. I think it would be better to return a string and throw if you can't do that because you got an error in you program/data. – Sam Holder Apr 07 '11 at 10:26
0

I would return an IEnumerable, and then use FirstOrDefault() at the call-site. If you want to return a single item, call it GetSingleStringFromXXX() with the following signature:

string GetSingleStringFromXXX(params[] args)
GregC
  • 7,737
  • 2
  • 53
  • 67