如何命名只能返回一项的方法。 (C#、ASP.NET)
我有一个方法,它返回一个字符串列表。由于某种原因,我不允许该方法返回其中包含多个字符串的列表。将这样的方法命名为 GetEntry 还是 GetEntries 更好?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,应该是 getEntry,因为它只返回一个字符串;无论如何,这听起来更像是个人问题,而不是代码问题。
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.
最好让该方法返回一个字符串而不是一个列表,但如果这是不可能的,那么您应该命名它,以便它的名称表明它的作用,所以
GetEntry
会更好,甚至GetSingleEntry
使其更加明确。编辑因为看来你不一定会返回我刚刚为
GetEntry
获得的列表,所以我只会使用GetSingleEntry
,如果,无论出于什么目的原因是,您返回的列表只有一个条目,以便让调用者清楚地了解这一点。如果该方法仅返回一个字符串,则不需要这样做,因此在这种情况下,GetEntry
就足够了。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 evenGetSingleEntry
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 useGetSingleEntry
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 caseGetEntry
would be sufficient.我将返回 IEnumerable,然后在调用站点使用 FirstOrDefault()。如果要返回单个项目,请使用以下签名调用 GetSingleStringFromXXX():
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: