AGuyCalledGerald
AGuyCalledGerald

Reputation: 8150

How to name method that can only return one entry. (C#, ASP.NET)

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?

Upvotes: 0

Views: 406

Answers (3)

Sam Holder
Sam Holder

Reputation: 32964

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.

Upvotes: 1

James Litewski
James Litewski

Reputation: 449

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.

Upvotes: 2

GregC
GregC

Reputation: 8007

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)

Upvotes: 0

Related Questions