Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38650
| From | Rhino <no_offline_contact@example.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Overloading methods question |
| Date | 2018-10-25 15:53 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <pqt6v0$e65$1@dont-email.me> (permalink) |
| References | <pqqmoq$jeo$1@dont-email.me> <1b2bqd7oqzez6$.sfpve8lwmic2.dlg@40tude.net> |
On 2018-10-25 4:13 AM, Joerg Meier wrote:
> On Wed, 24 Oct 2018 17:04:27 -0400, Rhino wrote:
>
>> Is it better to overload a single method name several times or to create
>> uniquely-named methods to accomplish similar work?
>
> I would argue that you should overload methods that return the same thing,
> but have separate methods for returning different things. In that spirit, I
> would overload the methods with an without a zone, but have separate
> methods for gettting the full name, as I suspect from your posting that
> those do in fact return something different.
>
> That being said, while this was probably just an example, I should point
> out that this is generally the completely wrong approach and kind of
> violates sensible object oriented design: return the month instead, and let
> the caller ask the month what it's called. That's what java.time.Month is
> for.
>
Actually, it was a "real life example". That is one of the methods I am
rewriting
in my class of convenience methods.
I was intrigued by your suggestion that I should just return the month
and let the caller ask the month what it is called. I wasn't sure what
you mean by that. but I wrote a bit of code and came up with this:
static java.time.Month getCurrentMonth() {
LocalDateTime ldt = LocalDateTime.now();
ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
return zdt.getMonth();
}
Then, in the code which calls the convenience method, something like this:
String fullMonthName =
CurrentDateTimeUtils.getCurrentMonth().getDisplayName(TextStyle.FULL,
new Locale("de", "DE"))); //gets full month name in German e.g. Oktober
String shortMonthName =
CurrentDateTimeUtils.getCurrentMonth().getDisplayName(TextStyle.SHORT,
new Locale("de", "DE"))); //gets short month name in German e.g. Okt.
int numericMonth = CurrentDateTimeUtils.getCurrentMonth().getValue();
//gets numeric value of month e.g. 10 for October
Or did you have something different in mind?
I rather like the approach you're suggesting. If I give the
responsibility of deciding which version of the month name people want
back to the calling program, that simplifies my convenience class so
that I need fewer methods while leaving it entirely possible for the
calling program to get the month information in any format it likes
since all the possible formats can be derived from what the convenience
method returns.
Either way, the program using my convenience method has to decide what
it wants - and will get what it wants - so it's very reasonable to
design the convenience class the way you suggest.
--
Rhino
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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