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


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

Re: Overloading methods question

From Rhino <no_offline_contact@example.com>
Newsgroups comp.lang.java.programmer
Subject Re: Overloading methods question
Date 2018-10-25 16:07 -0400
Organization A noiseless patient Spider
Message-ID <pqt7qs$j8k$1@dont-email.me> (permalink)
References <pqqmoq$jeo$1@dont-email.me> <pqsak0$es4$1@dont-email.me>

Show all headers | View raw


On 2018-10-25 7:49 AM, Eric Sosman wrote:
> 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?
>> [...]
>> APPROACH 1:
>> I could have several overloaded versions of a single method. For 
>> instance:
>> - getCurrentMonthName(boolean fullMonthName) and assume local time zone
>> [...]
> 
>      In addition to what Arne and Joerg have said, I'd suggest that
> the association `true' -> "full name" and `false' -> "short name"
> is not very self-documenting.  Perhaps you should use an `enum'
> instead, either TextStyle or something of your own, since
> 
>      String name = getCurrentMonthName(TextStyle.SHORT);
> 
> is more easily read and understood than
> 
>      String name = getCurrentMonthName(false);
> 
>      Also, if you someday need to support extraterrestrial locales
> and find out that Klingon has *three* common forms for month names,
> an `enum' can always grow a third instance while a boolean can't ...
> 
Yes, I'd already thought about the lack of flexibility in the boolean 
approach before I saw your and Joerg's replies. I created an Enum called 
MonthNamePattern and gave it all 8 values possible for a Month, 
acccording to the documentation in DateTimeFormatter: MMMM, MMM, MM, M, 
LLLL, LLL, LL, and L.

I could just pass that Enum to the method to have it return the exact 
format the calling program wants or, better yet (if I understood Joerg 
correctly) just let the convenience method return the month value in a 
java.time.Month and then let the *calling* program decide if it wants 
the TextStyle.FULL or TextStyle.SHORT or the numeric equivalent of the 
month. I could even use my Enum with a DateTimeFormatter to make the 
calling program display the month using the single M formatting code if 
that became necessary.

If I return the java.time.Month, then I really only need two variations 
of getCurrentMonth:
- getCurrentMonth() which defaults to the system default zone ID
- getCurrentMonth(zoneId) which uses the specified zone ID

I *like* this approach! No unwieldy names like 
getCurrentMonthFullRemote() and only two simple methods to write. In 
fact, the convenience methods are so simple they are very close to 
trivial and may not even be worth writing....

(Thanks to Joerg's remarks, I just now discovered the existence of the 
separate classes for Month, Day, etc. and the various techniques they 
offer to present the information however you want them. I had really 
just started looking at java.time.* this week after a few years away 
from Java and hadn't explored all the classes yet.)


-- 
Rhino

Back to comp.lang.java.programmer | Previous | Next — Previous 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