开发者

Naming convention for getting collection size

I have the following class

class X {
    private List<Calendar> calendars;

//    public int getCalendarCount()
//    public int getCalendarSize()
//    public int getSize()
//    public int getCount()
//    public int size()
//    public int count()
}

I was wondering, what is the most common used naming convention, to retrieve the calendas' size.

In List, they are using size.

However, in som开发者_开发百科e 3rd parties library like XStream, they are using getXXXCount.


If X is only about holding a list of calendars, then size would make sense — but then that would beg the question of why you're creating a class as opposed to just using List<Calendar>.

If X is going to hold much of anything else, then presumably it's going to provide a means of accessing the List of Calendars, so it doesn't really need a method to give that list's size (since List already has it). That would be the usual thing.

But if you're not directly exposing that List for some reason (and remember, you can make it an unmodifiable List), then I'd probably go with getCalendarCount.


Depends on the actual name of X.

I would opt for getCalendarCount() unless your class is named something like CalendarList in which case something like getSize may be appropriate.

There's no real convention as far as I know.


There are no hard-and-fast conventions on whether to use size versus count versus length, and whether or not to use getXxx versus xxx.

The best advice I can offer is:

  • to try to be consistent, and
  • use the getXxx convention if there is a possibility that your class may be used as a "bean".
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜