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


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

Re: null checks

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: null checks
Date 2017-07-26 16:40 -0400
Organization A noiseless patient Spider
Message-ID <olaug3$l2j$1@dont-email.me> (permalink)
References (1 earlier) <ojoaip$pab$1@gioia.aioe.org> <70f5af57-8dc0-416c-b2aa-0bbbb47fc7b5@googlegroups.com> <ojoe2s$6rm$1@dont-email.me> <c7fe6140-5e85-4cbb-b1b1-cb72fbfbaec7@googlegroups.com> <c660da66-8808-45b1-aa2a-8024e2bff635@googlegroups.com>

Show all headers | View raw


On 7/26/2017 2:40 PM, Eric Douglas wrote:
> [...]
> Would this be a valid use of assert?
> [A method asserts that it is running on the Event Dispatch Thread]

     "Bless you, it all depends!" -- Pitti-Sing

     Specifically, it depends on your attitude towards calling the method
on an unsuitable thread: Would that be a "bug" or a "mistake?"  If the
call occurs on the wrong thread, is it because something has gone wrong
in the program's logic ("bug"), or because the programmer was ignorant
or perhaps careless ("mistake")?  In the "bug" case an `assert' seems
entirely appropriate, whereas for a "mistake" it would probably be
better to throw something like an IllegalStateException.

     Bear in mind that `assert' can be disabled without recompiling,
and is in fact disabled unless explicitly enabled at run-time.  Thus
it is more suited to a debugging situation than to a defensive one;
you'd enable `assert' during testing and fix the bugs it highlights,
then leave it "latent" in the code in case mysteries arise after
deployment and you'd like to turn it back on again.  But you would
not use `assert' to check that a user's input is valid, or that a
particular file is accessible, or anything else that is not already
under the program's control.

     The code you link to lies in a gray area: You can debug your own
use of it until you're satisfied all is well, but that's not the end
of the tale.  The code is a `protected' method of a `public' class,
meaning that other programmers can extend the class and call the
method -- and perhaps call it inappropriately.  Your tests never saw
their faulty code, and the `assert' is turned off until somebody
thinks to turn it back on again, and ...  In this case, a test that
will *always* be there and *always* fire may be appropriate.  This
may be a case where IllegalStateException would be preferable.

     ... but then again, maybe not.  "It all depends!"

-- 
esosman@comcast-dot-net.invalid
Twelve hundred seventy-four days to go.

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


Thread

null checks Eric Douglas <e.d.programmer@gmail.com> - 2017-07-07 08:03 -0700
  Re: null checks Arne Vajhøj <arne@vajhoej.dk> - 2017-07-07 11:49 -0400
    Re: null checks Eric Douglas <e.d.programmer@gmail.com> - 2017-07-07 08:54 -0700
      Re: null checks Arne Vajhøj <arne@vajhoej.dk> - 2017-07-07 12:12 -0400
        Re: null checks Eric Douglas <e.d.programmer@gmail.com> - 2017-07-07 10:16 -0700
        Re: null checks Joerg Meier <joergmmeier@arcor.de> - 2017-07-08 22:33 +0200
          Re: null checks Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-08 21:14 +0000
            Re: null checks Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-08 17:51 -0400
              Re: null checks Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-08 22:26 +0000
            Re: null checks Arne Vajhøj <arne@vajhoej.dk> - 2017-07-08 18:28 -0400
            Re: null checks Joerg Meier <joergmmeier@arcor.de> - 2017-07-25 07:26 +0200
              Re: null checks Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-25 12:12 +0000
          Re: null checks Arne Vajhøj <arne@vajhoej.dk> - 2017-07-08 18:20 -0400
            Re: null checks Eric Douglas <e.d.programmer@gmail.com> - 2017-07-10 05:35 -0700
        Re: null checks Robert Klemme <shortcutter@googlemail.com> - 2017-07-09 13:10 +0200
      Re: null checks Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-07 12:53 -0400
        Re: null checks lewbloch@gmail.com - 2017-07-26 11:16 -0700
          Re: null checks Eric Douglas <e.d.programmer@gmail.com> - 2017-07-26 11:40 -0700
            Re: null checks Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-26 16:40 -0400
            Re: null checks lewbloch@gmail.com - 2017-07-28 18:47 -0700
  Re: null checks Wayne <wayne@nospam.invalid> - 2017-07-07 16:24 -0400
  Re: null checks Joerg Meier <joergmmeier@arcor.de> - 2017-07-08 22:32 +0200
  Re: null checks Lothar Kimmeringer <news201705@kimmeringer.de> - 2017-07-09 15:59 +0200
    Re: null checks Arne Vajhøj <arne@vajhoej.dk> - 2017-07-11 20:30 -0400

csiph-web