开发者

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

开发者 https://www.devze.com 2023-02-22 13:25 出处:网络
I have a method, that returns a List of strings. For s开发者_JAVA技巧ome 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

I have a method, that returns a List of strings. For s开发者_JAVA技巧ome 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?


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号