Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38647

Re: Overloading methods question

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Overloading methods question
Date 2018-10-24 18:56 -0400
Organization Aioe.org NNTP Server
Message-ID <pqqtbi$lr$1@gioia.aioe.org> (permalink)
References <pqqmoq$jeo$1@dont-email.me>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Overloading methods question Rhino <no_offline_contact@example.com> - 2018-10-24 17:04 -0400
  Re: Overloading methods question Arne Vajhøj <arne@vajhoej.dk> - 2018-10-24 18:56 -0400
  Re: Overloading methods question Joerg Meier <joergmmeier@arcor.de> - 2018-10-25 10:13 +0200
    Re: Overloading methods question Rhino <no_offline_contact@example.com> - 2018-10-25 15:53 -0400
      Re: Overloading methods question Eric Douglas <e.d.programmer@gmail.com> - 2018-10-25 13:58 -0700
        Re: Overloading methods question Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-10-25 17:49 -0400
          Re: Overloading methods question Martin Gregorie <martin@mydomain.invalid> - 2018-10-25 22:29 +0000
            Re: Overloading methods question Arne Vajhøj <arne@vajhoej.dk> - 2018-10-25 20:08 -0400
              Re: Overloading methods question Martin Gregorie <martin@mydomain.invalid> - 2018-10-26 00:37 +0000
                Re: Overloading methods question Arne Vajhøj <arne@vajhoej.dk> - 2018-10-25 21:42 -0400
                Re: Overloading methods question Joerg Meier <joergmmeier@arcor.de> - 2018-10-26 12:46 +0200
                Re: Overloading methods question Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-10-26 07:50 -0400
                Re: Overloading methods question Martin Gregorie <martin@mydomain.invalid> - 2018-10-26 19:33 +0000
                Re: Overloading methods question Eric Douglas <e.d.programmer@gmail.com> - 2018-10-26 13:13 -0700
                Re: Overloading methods question Arne Vajhøj <arne@vajhoej.dk> - 2018-10-26 20:58 -0400
                Re: Overloading methods question Martin Gregorie <martin@mydomain.invalid> - 2018-10-27 22:36 +0000
                Re: Overloading methods question Arne Vajhøj <arne@vajhoej.dk> - 2018-10-27 19:38 -0400
                Re: Overloading methods question Martin Gregorie <martin@mydomain.invalid> - 2018-10-28 10:39 +0000
                Re: Overloading methods question Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-10-28 07:54 -0400
          Re: Overloading methods question Eric Douglas <e.d.programmer@gmail.com> - 2018-10-26 06:26 -0700
      Re: Overloading methods question Joerg Meier <joergmmeier@arcor.de> - 2018-10-26 12:52 +0200
        Re: Overloading methods question Rhino <no_offline_contact@example.com> - 2018-10-26 22:10 -0400
          Re: Overloading methods question Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-10-27 07:43 -0400
            Re: Overloading methods question Rhino <no_offline_contact@example.com> - 2018-10-29 14:40 -0400
              Re: Overloading methods question Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-10-29 16:39 -0400
          Re: Overloading methods question Eric Douglas <e.d.programmer@gmail.com> - 2018-10-29 05:07 -0700
            Re: Overloading methods question Rhino <no_offline_contact@example.com> - 2018-10-29 14:41 -0400
  Re: Overloading methods question Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-10-25 07:49 -0400
    Re: Overloading methods question Rhino <no_offline_contact@example.com> - 2018-10-25 16:07 -0400

csiph-web