Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38646
| From | Rhino <no_offline_contact@example.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Overloading methods question |
| Date | 2018-10-24 17:04 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <pqqmoq$jeo$1@dont-email.me> (permalink) |
Is it better to overload a single method name several times or to create uniquely-named methods to accomplish similar work? I am in the midst of rewriting some old convenience methods that work with dates and times but use the old Calendar and java.util.Time classes. I know that it is far better to use the java.time.* packages now. In the course of revising my methods to use the Java 8 classes, it has occurred to me that I should have several variants of some of the methods. For instance, one of my methods determines the name of the current month. 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. Can anyone help me out? In the absence of such a principle, it seems to me that I need to weigh the costs of each approach against the benefits and see which is the best balance. For instance, Approach 4 has the benefit of requiring me to code only one method to get the current month but means the method is more complex and probably has more conditions to test in unit test. It may also be more work for the person trying to use these classes as they have to understand the meaning of the parameters and figure out how to set the desired values. (I'm just learning the Java 8 time classes and didn't know what a zoneId was 24 hours ago, let alone how to set it and what values I could use. A developer using these classes might be in the same boat.) -- Rhino
Back to comp.lang.java.programmer | Previous | Next — 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