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?

- 10,065
- 8
- 42
- 63

- 7,882
- 17
- 73
- 120
-
1It's probably best to make it just return a String. Is that not an option? – Jeff Foster Apr 07 '11 at 09:57
-
sorry, forget about the return type, that´s just an example, I am only interested in the naming. – AGuyCalledGerald Apr 07 '11 at 10:00
3 Answers
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.

- 451
- 2
- 6
- 16
-
-
No need to curse, all I'm saying is, I don't understand why your asking others what you should name a method. It is more of a personal issue, because what you name your own method is your personal preference. – James Litewski Apr 07 '11 at 10:07
-
Why is it not allowed to ask others about how to name methods? I would like to know their opinion. – AGuyCalledGerald Apr 07 '11 at 10:09
-
Lol, I'm not trying to get on your case. I just found it a little odd. But whatever, sorry if I offended you or anyone else. – James Litewski Apr 07 '11 at 10:10
-
@James, naming is very important and I think its appropriate to ask questions about that, especially if the methods are going to be used by others. – Sam Holder Apr 07 '11 at 10:26
-
Okay, sorry; I guess I just made a newbie mistake. Won't happen again, haha. Still learning. – James Litewski Apr 07 '11 at 10:28
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.

- 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
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)

- 7,737
- 2
- 53
- 67