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


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

Re: Overloading methods question

Newsgroups comp.lang.java.programmer
Date 2018-10-26 06:26 -0700
References <pqqmoq$jeo$1@dont-email.me> <1b2bqd7oqzez6$.sfpve8lwmic2.dlg@40tude.net> <pqt6v0$e65$1@dont-email.me> <75ad8b55-3def-4ec1-b46b-65ecd971d0fe@googlegroups.com> <pqtdpa$nl4$1@dont-email.me>
Message-ID <fef929a1-e6f6-450a-8f39-5d05fe1bcab7@googlegroups.com> (permalink)
Subject Re: Overloading methods question
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


On Thursday, October 25, 2018 at 5:49:37 PM UTC-4, Eric Sosman wrote:
> 
>      Also, be careful to distinguish between "there are exactly two
> possibilities" and "I have thought of only two possibilities."  It
> is perhaps instructive that the example given would not work for the
> printer that sits on my desk at this moment, which has *five* modes
> for duplexing:
> 
>      One-sided
> 
>      Two-sided (automatic, flip on long axis)
> 
>      Two-sided (automatic, flip on short axis)
> 
>      Two-sided (manual, flip on long axis)
> 
>      Two-sided (manual, flip on short axis)
> 
>      (In the "automatic" modes the printer marks one side but doesn't
> spit the page out, then sucks it back in and does the second side.
> In the "manual" modes it prints and ejects all the back sides, then
> you re-insert the stack in the paper supply and it prints the formerly-
> blank fronts.  "Automatic" is more convenient, "Manual" can be faster
> for long documents.)
> 
>      Duplex-or-not is, as suggested, a Yes/No question -- but if the
> answer is Yes the answer is incomplete, lacking essential detail.
> A good API offers (relatively) straightforward ways to supply all
> the necessary information.

Well you're just full of booleans, they could be overloads.
setDuplex(boolean isDuplex)
setDuplex(boolean isDuplex, boolean flipShortSide)
setDuplex(boolean isDuplex, boolean flipShortSide, boolean auto)

Though a separate method for the not-so-obvious-boolean would be more readable, setFlipOnShortSide(boolean), with flip on long side being the default either way.

It is probably common that boolean options are linked ie calling setFlipOnShortSide() does nothing unless you call setDuplex(true).
I actually can't think of many good examples of true independent boolean parameter examples.  It seems more often boolean is used in the return value.

Then we have old school parameter, java.awt.font.TextAttribute is full of static final numeric values, and new school parameter ie javax.print.attribute.standard.Sides.  While the documentation for EnumSyntax does say an enum is just an integer, it does auto-validate at compile time, and you don't have to maintain a condition in every method:
if (passInValue < 1 or passInValue > 5) { throw new InvalidParameterException("parameter must be in range of 1 to 5"); }
or deal with stack trace errors.

Most parameters could actually be boolean but is it more or less readable?  Is it more or less work?
public class Person{
  private int gender = 0;
  public void setIsMale(boolean val) {
    gender = val == true ? 0 : 1;
  }
  public void setIsFemale(boolean val) {
    gender = val == true ? 1 : 0;
  }
  public void setIsBiGender(boolean val) {
    gender = val == true ? 2 : gender;
  }
}
Now if the parameters are self documenting and it is both possible to be both or neither you could use one method
public void setGender(boolean isMale, boolean isFemale)

Some APIs like to name everything arg1 with no javadoc...

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