This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

opinion poll: readability

 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think? Which of the following is more readable:

or this?


I'm particularly interested in hearing from you experienced programmers out there. Of course, since this is related to an assignment I'm working on, there's really only one opinion that counts in the end
Thanks,
Pauline
[This message has been edited by Pauline McNamara (edited October 28, 2001).]
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it's the assignment I think it is, the point is moot because there's a simpler way to write the method in question.
That said, although I'm a fan of the ?: operator in certain circustances, I would tend not to nest or chain it like you do in the first version. I try to keep the expression as simple as possible.
Michael "am I experienced?" Matola
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two other things to think about:
(1) If you're going the route of returning from multiple places in the method and this is the whole of the logic, then you can simplify a little by getting rid of the elses:

(2) However, JavaRanch style doesn't allow you to return from anywhere but the end of a method (grrr...), so here's another way of coding that logic.

Both versions have a "default" value returning if the condition(s) prove(s) to be false. It's worth thinking about whether any of the possible outcomes is more default-worthy than the others (or if any of conditionals are any tidier than the others, etc.).
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can just pull a
return b - a ;
if it's only the sign of the numbers and not their values that's relevant. (Now why would I think that...)

[This message has been edited by Michael Matola (edited October 28, 2001).]
 
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally I'd choose Micheals (2). Nice and neat , no change of misunderstanding it. I'm not a big fan of the ? : operator
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Matola:
If it's the assignment I think it is, the point is moot because there's a simpler way to write the method in question.


What a tease!
How could I resist such a cryptic hint?! Testing continues...
Thanks,
Pauline
[This message has been edited by Pauline McNamara (edited October 29, 2001).]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Matola:
(2) However, JavaRanch style doesn't allow you to return from anywhere but the end of a method (grrr...)...


There is a reason for that rule, you know.
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do tell!
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am old-fashioned and loved (to some degree) APL-one-liners and I was practicing 'stenographie' (meaning I understand criptic stuff).
Pauline, your first example is not so complicated to understand the construction. Maybe a second look is needed, but a second 'look' is needed in Michael's answer (2) too. (e.g. to realize the default).
And if you are used to this construction, no problem at all. If you have to take into account others (beginners?), they should
learn this basic construct, shouldn't they?
If there is something as a style-guide saying don't ...
So I prefer in this case your first version.
If not allowed, Michael's (2) and suggestions are OK.
Michael's second note
b-a may 'overflow', I would not use.
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but a second 'look' is needed in Michael's answer (2) too. (e.g. to realize the default).
Why ?? the 1st line intializes the default value.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Matola:
Or you can just pull a
return b - a ;
if it's only the sign of the numbers and not their values that's relevant. (Now why would I think that...)


I'm so glad you asked - made me take a closer look at the method in question (and what it returns ). I'd been sidetracked by an example of that method that returned those values...
return b - a ; looks like a pretty clever solution.

[This message has been edited by Pauline McNamara (edited October 30, 2001).]
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
method in question
Which method. Is the - a method in this case and if it is of which class is it a method of ??
Man I'm missing some basics here.

[This message has been edited by Johannes de Jong (edited October 30, 2001).]
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the other hand...

Originally posted by Peter Gragert:
Michael's second note
b-a may 'overflow', I would not use.


What do you mean by this Peter?
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You beat me to the draw Pauline. Overflow as far as I'm concerned can't happen when you subtract !!!
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Johannes de Jong:
method in question
Which method. Is the - a method in this case and if it is of which class is it a method ?? Man I'm missing some basics here.


Good Morning Johannes.
I'm not sure if I should give the name of the method, but I'm working on OOP-3 right now, if that helps stir up some fond memories.
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Johannes de Jong:
Overflow as far as I'm concerned can't happen when you subtract !!!


Oh?

Gotta watch those negative subtrahends.
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Peter Gragert:


If you have to take into account others (beginners?), they should
learn this basic construct, shouldn't they?


I was not suggesting not to use the ternary operator, just not to nest it.


Michael's second note
b-a may 'overflow', I would not use.


If Pauline's code is generating the values of a and b the way I think it is, then there isn't any risk of overflow. (In fact the code generating the values of a and b uses the subtraction technique too.)
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Pauline McNamara:


I'm so glad you asked - made me take a closer look at the method in question (and what it returns ). I'd been sidetracked by an example of that method that returned those values...
return b - a ; looks like a pretty clever solution.


The "return b - a" is a slight distractor here, I believe. True that if you've calculated both b and a, subtracting is the easiest way to return zero or a negative or positive integer.
But there's a way of writing your method in which you just piggyback on the Java API method you've found, and that case you don't really care about the specific values of a and b. (And if you examine the source of that method, you can see that you don't risk overflow by subtracting the values.)
Now fess up, Pauline. You've never read all the code for the assignment log prototype! This technique is used there several times.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uh oh. Caught. 10 lashes with a wet noodle.
But imagine I'd read it all - we wouldn't be having this discussion, and I wouldn't have had to jump through the same mental hoops as I have been for the last 48 hours...
... which, by the way, had led me to what seems like a nifty solution. Maybe it's even the piggybacking you refer to. (Is piggybacking a pattern? )In my current version of the "method in question", the return statement calls another class's method with almost the same name. Sound close?

Thanks for holding those hoops up just high enough.
Pauline
p.s. Once I'm on servlets, I plan to go through the assignment log code. Just wait 'til you get bombarded with questions then!
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pauline McNamara:
In my current version of the "method in question", the return statement calls another class's method with almost the same name. Sound close?


By George I think she got it
Very nifty eh? By the way, enjoying the forum?
Jason

[This message has been edited by jason adam (edited October 30, 2001).]
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jason adam:
By George I think she got it


"By Michael"?
Yes, very nifty.


By the way, enjoying the forum?


I'm actually tending 2 of them, but they're both veeeery quiet.
How about yours?
Pauline
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gotta watch those negative subtrahends.
Actually I dont agree with you Micheal.
This is not an overflow problem. "- (-2147483647)" is simply a logical error ie. the programmer forgot (or did not know) the max signed value's. It's not an overflow error. I see an overflow error as an runtime exception.
[This message has been edited by Johannes de Jong (edited October 30, 2001).]
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jason adam:
By George I think she got it


Sounds that way to me to!
(Has Johannes figured out yet what method we're all talking about?)
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Johannes de Jong:


This is not an overflow problem. "- (-2147483647)" is simply a logical error ie. the programmer forgot (or did not know) the max signed value's. It's not an overflow error.


Suppose instead the expression was
System.out.println( getB() - getA() ) ;
And the value that getB() returns is 2147483647, and the value that getA() returns is -2147483647.


I see an overflow error as an runtime exception.


Java doesn't.
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And besides, there's no such thing as subtraction. It's just a special case of addition (where at least one operand is negative).
Michael "wondering if that deserves a smiley" Matola
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pauline McNamara:
What do you think? Which of the following is more readable:
<pre>
return (a < b ? -1 : ( a == b ? 0 : 1 ) )
</pre>
or this?
<pre>
if ( a < b )
{
return -1 ;
}
else if ( a == b )
{
return 0 ;
}
else
{
return 1 ;
}
</pre>
I'm particularly interested in hearing from you experienced programmers out there. Of course, since this is related to an assignment I'm working on, there's really only one opinion that counts in the end


Neither one. As has already been stated, ?: is ok, but nested ?:'s are to be avoided. Also, returning in the middle of a method is a no-no.

I'm glad you have found another alternative, Pauline.

[This message has been edited by Marilyn deQueiroz (edited October 30, 2001).]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Matola:
And besides, there's no such thing as subtraction. It's just a special case of addition (where at least one operand is negative).

Well there is the '-' binary operator and there is the '-' unary operator. What else would you call the '-' binary operator if not subtraction?
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Johannes de Jong:
This is not an overflow problem. "- (-2147483647)" is simply a logical error ie. the programmer forgot (or did not know) the max signed value's. It's not an overflow error. I see an overflow error as an runtime exception.


If you add two very large integers, and the result overflows, you will not get any notification of the overflow, neither compile-time nor run-time.

 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it just me, or does it seem like when Marilyn gets going on her posts, it all should just end with "Case closed"
Jason "Definitely thinks that deserved a smily" Adam
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn deQuieroz writes:

Well there is the '-' binary operator and there is the '-' unary operator. What else would you call the '-' binary operator if not subtraction?


I'm not debating what to call it. I'm saying there's really no need for it in the first place. Or, in other words, subtraction is not an operation on par with addition, it's just a special case of it.
Once you admit the possibility of "negative numbers" (or, what amounts to, in your/Java terminology, the existence of "the '-' unary operator"), then any subtraction expression (um, I think there's some qualification here about real numbers, but I'm fuzzy on that stuff) is just an addition expression with the unary operator applied to one of the operands (not arbitrarily, of course).
Or, more directly, for any
a - b
you could express that as
a + (-b)
And the need for the "'-' binary operator" goes away.
I have no idea how Java implements the binary operators '+' and '-', but just for grins take a look at BigInteger.subtract(). (If the magnitude of the numbers is the same, it subtracts by adding.)
My whole point in this subtraction silliness was that I just didn't buy Johannes's argument there's something special about subtraction in terms of overflow.
[This message has been edited by Michael Matola (edited October 30, 2001).]
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok ok you guys win
The following is quoted from the Overflow entry in the Java glossary gotchas.
Overflow
Java is cavalier about overflow. There are no compile-time warnings or run-time exceptions to let you know when your calculations have become too big to store back in an int or long. There is no warning for float or double overflow either. One place you often get nailed is when some calculations are done as int, high order parts are truncated, then promoted to long.

But tommorow morning I'm posting a PL/1 overflow dump here !!
and no I still dont know which method you guys are talking about , I sat the whole damm afternoon in meetings
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JdJ, what's the Java glossary gotchas?
And be careful of what you dump around here, it's a java site, after all.
[This message has been edited by Pauline McNamara (edited October 30, 2001).]
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pauline see : Java Gotchas
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply had to post this
<size=+0>

</size>
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, JdJ, what's that mention of subtraction doing in your post?
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What an excellent site JdJ, thanks for sharing that with us! Definitely will have to pass that along to the other students in my Java class
Jason
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That list of gotchas does look like a great source. I found it here at the ranch, too, among Maha Anna's resources: Java Gotchas
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, JdJ, what's that mention of subtraction doing in your post?
Oops forgot to edit it out
Its just that I've never managed to produce an overflow dump on the mainframe using subtraction , but then I dont use all the possiblities. The guys that wrote the debugger know more than I do , so once again I'm kindly being humiliated
 
Peter Gragert
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I added the 'overflow' issue.
I thougt wrongly, because Java gives no error.
But the concrete examples:
System.out.println( 2147483647 - (-2147483647) ) ;
System.out.println( 2000000000 + 2000000000 );
(Thanks guys !)
show what I meant (is THE problem) not to use "a-b".
 
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I'll have a drink now....................
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic