Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: Overloading methods question Date: Wed, 24 Oct 2018 18:56:51 -0400 Organization: Aioe.org NNTP Server Lines: 57 Message-ID: References: NNTP-Posting-Host: PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 X-Notice: Filtered by postfilter v. 0.8.3 Content-Language: en-US Xref: csiph.com comp.lang.java.programmer:38647 On 10/24/2018 5:04 PM, Rhino wrote: > Is it better to overload a single method name several times or to create > uniquely-named methods to accomplish similar work? > It seems reasonable to me that I might at some point need to know the > name of the current month: > - in either short or long version - i.e. "Oct" or "October" > - in the local timezone or a different one (after all, while it may be > late in the day on Oct 31 in my timezone, it may already be early Nov 1 > a few timezones away) > > APPROACH 1: > I could have several overloaded versions of a single method. For instance: > - getCurrentMonthName(boolean fullMonthName) and assume local time zone > - getCurrentMonthName(ZoneId zoneId) and assume full month name > - getCurrentMonthName(boolean fullMonthName, ZoneId zoneId) and assume > nothing > - getCurrentMonthName() and assume local time zone and full month name > > APPROACH 2: > Or I could do this: > - getCurrentMonthNameLocal(boolean fullMonthName) and assume local time > zone > - getCurrentMonthNameRemote(boolean fullMonthName, ZoneId zoneId) >  and assume nothing > > APPROACH 3: > I could even do this: > - getCurrentMonthNameFullLocal() and assume local time zone > - getCurrentMonthNameShortLocal() and assume local time zone > - getCurrentMonthNameFullRemote(ZoneId zoneId) and use specified time zone > - getCurrentMonthNameShortRemote(ZoneId zoneId) and use specified time zone > > APPROACH 4: > Or I could reduce the number of methods I need by having only this: > - getCurrentMonthName(boolean fullMonthName, ZoneId) which would force > users to supply a value for the boolean and the zoneId every time even > if they wanted the local time zone > > Which is the best design and, most importantly, WHY? I expect there is a > basic design precept somewhere that helps a developer decide which is > the best way to go in circumstances like these. It would be helpful to > learn (or rediscover) this principle. I would go for approach #5: getCurrentMonth(boolean fullMonthName) getCurrentMonth(boolean fullMonthName, ZoneId zoneId) My logic: * I expect the method name in a get to explain what it is getting and any arguments to qualify how it should be getting it and that points to #2 and * and ZoneId is sufficient rare to warrant a default value and since Java does not have defaults that means an overload Arne