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


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

Re: About java program.

From Arved Sandstrom <asandstrom2@eastlink.ca>
Newsgroups comp.lang.java.programmer
Subject Re: About java program.
References (5 earlier) <ILKdnZ-vg53OjiLMnZ2dnUVZ7tmdnZ2d@bt.com> <19wnriq53qxel.u9rt8wy6z52g.dlg@40tude.net> <kpnleo$7os$1@dont-email.me> <PlLvt.57919$hw2.43756@newsfe04.iad> <kpo2ch$h8j$1@dont-email.me>
Message-ID <7u3wt.22047$N36.9643@newsfe27.iad> (permalink)
Organization Public Usenet Newsgroup Access
Date 2013-06-18 17:33 -0300

Show all headers | View raw


On 06/17/2013 07:30 PM, Eric Sosman wrote:
> On 6/17/2013 5:38 PM, Arved Sandstrom wrote:
>> On 06/17/2013 03:49 PM, Eric Sosman wrote:
>>> On 6/17/2013 2:32 PM, Joerg Meier wrote:
>>>> [...]
>>>> Personally, I found your example considerably harder to read than the
>>>> one
>>>> liner, as well as harder to read than what would have been my solution:
>>>>
>>>> private boolean askYesNoquestion (String str){
>>>>      if(str.equals("yes"))
>>>>          return true;
>>>>
>>>>      return false;
>>>> }
>>>
>>>      Or, for even greater clarity:
>>>
>>>      private boolean askYesNoquestion(String str) {
>>>          boolean yes;
>>>          boolean no;
>>>          if (str != null && str.equals("yes")) {
>>>              yes = true;
>>>              no = false;
>>>          } else if (str != null && str.equals("no")) {
>>>              no = true;
>>>              yes = false;
>>>          } else {
>>>              yes = no = false;
>>>          }
>>>          return (no != true && yes != false) == true;
>>>      }
>>>
>>> (If we put our minds to it, I bet we can turn the one-liner
>>> into an entire class hierarchy with runtime-selected plug-in
>>> provider implementations!)
>>>
>> We can joke about it, but the one-liner has problems, especially if not
>> wrapped in a method like Robert did. I've seen exactly that one-liner
>> cause dozens of defects over the past 15 years of me doing Java. Not all
>> programmers out there are superstars like all of us here in CLJP, in
>> fact 99 percent aren't. If they - not graced with our wisdom and keen
>> skills and perspicacity - decide to use that short form - they will
>> almost *never* wrap it in a method that constrains the type being
>> compared to, or does a null check if that's also unacceptable. In which
>> case sooner or later - sooner usually - lack of controls elsewhere in
>> the code, which is par for the course, will make that elegant little
>> snippet fail.
>
>      "As simple as possible, no simpler."  The observation that
> a simple construct can be misused -- a true observation -- does
> not mean that the cure is to add complexity.  For one thing, the
> added bulk offers more opportunities for misuse.
>
>      static int sum(int a, int b) {
>          return a + b;
>      }
>
>      static int sum(int a, int b) {
>          int total = a;
>          total += b;
>          return total;
>      }
>
>      static int sum(int a, int b) {
>          assert Integer.MIN_VALUE <= a && a <= Integer.MAX_VALUE;
>          assert Integer.MAX_VALUE >= b && b >= Integer.MAX_VALUE;
>          Random rand = new Random();
>          int total;
>          do {
>              total = rand.nextInt();
>          } while (total - a != b);
>          return total;
>      }
>
>      Seriously, there are very few reasons to add complexity, and
> many to reduce it.  Complexity can (sometimes) make a needed
> performance improvement; complexity can (occasionally) provide a
> self-check or a debugging hook; complexity can (almost never)
> add clarity.
>
>      (Truthfully, now: Did you spot the error above on first glance?)
>
I didn't spot the error at first glance until you said there might be 
one and I went into code review mode. :-) Having said that, I'd shoot a 
coder who also perpetrated that while condition. :-)

Those asserts are actually an example of poorly maintainable code, and 
given the calibre of the average coder I'd rebuke the clever programmer 
who felt he had to write that. His follow-on maintainers may not get it 
completely.

Point being though, compactness is not always less complexity. It works 
in some languages, not in others. In APL-like languages like J it's an 
art form; in functional languages with powerful constructs it's often 
natural too. In languages like Java, it may or may not be appropriate, IMO.

I often decide against method chaining in Java, for example, so that I 
can break out the intermediate results and give them descriptive names. 
This is if the methods are my own or part of the app under development, 
I'm more willing to chain if the methods are part of a well-known and 
established API.

I also don't much do complicated logic tests. Not that yours are overly 
complex in your contrived example, but I wouldn't normally write that 
way. The fact that it takes some careful study to spot the error is a 
sign that maybe it's a maintenance problem waiting to happen.

That limits test is something that I might make a method actually. I do 
something similar if I need to do a zero-closeness test for floating 
points; I'll make that a carefully tested private utility method.

To recap, I don't think verbosity is always bad. I don't think that 
unrolling stuff and avoiding cleverness is bad. I am in vehement 
agreement with you that complexity almost never adds clarity; I dispute 
in a friendly fashion that careful extra code checks are the kind of 
complexity that is of concern. From a metrics standpoint, sure, there is 
some addition of complexity, but not of the type that concerns me.

AHS

-- 
When a true genius appears, you can know him by this sign:
that all the dunces are in a confederacy against him.
-- Jonathan Swift

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


Thread

About java program. rajbhaumik@gmail.com - 2013-06-15 13:38 -0700
  Re: About java program. Arne Vajhøj <arne@vajhoej.dk> - 2013-06-15 16:50 -0400
  Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-06-16 01:12 +0200
    Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-16 14:21 +0100
      Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-06-16 17:28 +0200
        Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-16 19:52 +0100
          Re: About java program. Arne Vajhøj <arne@vajhoej.dk> - 2013-06-16 15:50 -0400
        Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-17 09:36 +0100
        Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-17 09:49 -0300
          Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-17 14:46 +0100
            Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-17 13:32 -0300
            Re: About java program. Joerg Meier <joergmmeier@arcor.de> - 2013-06-17 20:32 +0200
              Re: About java program. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-06-17 14:49 -0400
                Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-17 18:38 -0300
                Re: About java program. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-06-17 18:30 -0400
                Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-18 17:33 -0300
                Re: About java program. Arne Vajhøj <arne@vajhoej.dk> - 2013-07-04 22:56 -0400
                Re: About java program. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-06-18 00:13 +0200
                Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-18 17:50 -0300
                Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-19 08:50 +0100
                Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-19 15:16 -0300
                Re: About java program. Lew <lewbloch@gmail.com> - 2013-06-19 16:20 -0700
                Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-07 13:31 -0400
                Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-07 18:16 -0400
                Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-07 19:21 -0400
                Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-07 20:07 -0400
                Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-07 20:21 -0400
                Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-07 20:40 -0400
                Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-07 22:20 -0400
                Re: About java program. Lew <lewbloch@gmail.com> - 2013-07-07 21:47 -0700
                Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-08 06:58 -0400
                Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-08 08:07 -0400
                Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-08 08:58 -0400
                Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-08 09:18 -0400
                Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-08 09:47 -0400
                Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-07-08 10:07 -0400
                Re: About java program. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-06-19 16:42 +0200
                Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-20 23:33 -0300
                Re: About java program. Martin Gregorie <martin@address-in-sig.invalid> - 2013-06-21 04:16 +0000
                Re: About java program. Joerg Meier <joergmmeier@arcor.de> - 2013-06-21 12:27 +0200
                Re: About java program. Martin Gregorie <martin@address-in-sig.invalid> - 2013-06-21 21:14 +0000
                Re: About java program. Joerg Meier <joergmmeier@arcor.de> - 2013-06-21 23:37 +0200
                Re: About java program. Martin Gregorie <martin@address-in-sig.invalid> - 2013-06-21 23:42 +0000
                Re: About java program. Gene Wirchenko <genew@telus.net> - 2013-06-24 13:00 -0700
                Re: About java program. Martin Gregorie <martin@address-in-sig.invalid> - 2013-06-24 21:29 +0000
                Re: About java program. Martin Gregorie <martin@address-in-sig.invalid> - 2013-06-24 21:42 +0000
                Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-21 23:59 -0300
                Re: About java program. Martin Gregorie <martin@address-in-sig.invalid> - 2013-06-22 13:30 +0000
                Re: About java program. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-06-21 16:24 +0200
                Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-21 22:39 -0300
                Re: About java program. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-06-22 19:25 +0200
                Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-22 19:41 +0100
                Re: About java program. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-06-23 03:53 +0200
                Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-23 09:21 +0100
                Re: About java program. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-06-24 18:57 +0200
                Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-24 18:14 +0100
                Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-22 09:03 +0100
                Re: About java program. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-06-22 19:26 +0200
                Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-22 18:51 +0100
                Re: About java program. Arne Vajhøj <arne@vajhoej.dk> - 2013-07-04 22:58 -0400
                Re: About java program. Joerg Meier <joergmmeier@arcor.de> - 2013-06-19 00:38 +0200
                Re: About java program. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-06-18 18:56 -0400
                Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-18 21:26 -0300
                Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-06-19 21:20 +0200
                Re: About java program. Sven Köhler <remove-sven.koehler@gmail.com> - 2013-06-20 11:23 +0300
                Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-06-20 19:18 +0200
                Re: About java program. blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2013-07-15 20:20 +0000
                Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-19 09:14 +0100
                Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-19 09:11 +0100
                Re: About java program. Arne Vajhøj <arne@vajhoej.dk> - 2013-07-04 23:11 -0400
                Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-07-05 09:13 +0100
                Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-07-05 23:15 +0200
                Re: About java program. Arne Vajhøj <arne@vajhoej.dk> - 2013-07-05 22:03 -0400
                Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-07-06 13:01 +0200
                Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-07-06 13:43 +0100
                Re: About java program. Martin Gregorie <martin@address-in-sig.invalid> - 2013-07-06 14:17 +0000
                Re: About java program. Arne Vajhøj <arne@vajhoej.dk> - 2013-07-06 16:18 -0400
                Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-07-06 21:42 +0100
                Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-07-06 09:16 +0100
                Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-07-06 13:14 +0200
                Re: About java program. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-07-06 08:22 -0400
                Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-07-07 20:43 +0200
                Re: About java program. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-07-07 18:07 -0400
                Re: About java program. Arne Vajhøj <arne@vajhoej.dk> - 2013-07-07 19:35 -0400
                Re: About java program. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-07-07 20:42 -0400
                Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-07-08 20:50 +0200
                Re: About java program. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-07-08 15:01 -0400
                Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-07-08 23:29 +0200
                Re: About java program. Arne Vajhøj <arne@vajhoej.dk> - 2013-07-06 16:27 -0400
                Re: About java program. Lew <lewbloch@gmail.com> - 2013-07-06 13:41 -0700
                Re: About java program. Arne Vajhøj <arne@vajhoej.dk> - 2013-07-05 21:59 -0400
              Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-17 21:06 +0100
            Re: About java program. Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2013-06-17 17:36 -0500
              Re: About java program. Robert Klemme <shortcutter@googlemail.com> - 2013-06-18 08:24 +0200
              Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-18 09:03 +0100
                Re: About java program. Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2013-06-18 13:07 -0500
                Re: About java program. Lew <lewbloch@gmail.com> - 2013-06-18 14:48 -0700
                Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-19 08:36 +0100
              Re: About java program. Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-06-18 23:33 -0300
                Re: About java program. lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-06-19 08:34 +0100
            Re: About java program. Arne Vajhøj <arne@vajhoej.dk> - 2013-07-04 22:53 -0400
  Re: About java program. Roedy Green <see_website@mindprod.com.invalid> - 2013-06-16 07:04 -0700
  Re: About java program. Jeff Higgins <jeff@invalid.invalid> - 2013-06-18 04:06 -0400

csiph-web