• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Curious as to what looks cleaner

 
Ranch Hand
Posts: 71
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just setting some initial values

 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I personally prefer the first but over time opinions seem to favor the second.

The first requires the reader to be familiar with order of operator precedence and associativity whereas the second does not.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd use the eyeball Scan test.  How many eyeball passes over the code is needed to understand what it does?
I'd say the fewer the better.
For me the first code takes 2 or 3 passes to see what it is doing.
The second only takes 1.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Second form, definitely.
Let's see whether there is anything in the old Sun style guide. Not obviously, no
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't care too much about the form. For me, what deserves more attention is the intent. Why are the variables being initialized referring to the idea of "element" but the value assigned talks about a line that was read. If the concern is really about clean code, I'd rather discuss what name is a better representation of what "lineRead" really means with respect to previousElement and largestElement. Maybe firstElement?
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

G Atwal wrote:just setting some initial values


When you gave those two versions, the more correct of second version in respect of first one would be:
This is essentially what the first version does behind the hoods.

In some weird way, in some cases (and might even this) I could sometimes find it clearer a first version, but only if the variables names are well chosen, and if those 3 variables are sort of connected in a logical chain. Don't know how to explain exactly what I mean...

But regardless, I personally would recommend to use a second version, and the main reason being - it is more conventional, and less likely to cause some noisy discussions between developers. Why did you do that way? Why not the other way?

 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I'd rather discuss what name is a better representation of what "lineRead" really means with respect to previousElement and largestElement. Maybe firstElement?


And in general it is a bit unintuitive that lineRead is communicated as an element. Elements more conventionally referring to elements within an array for example or list. But that's purely in the context of data structures.

@OP
What is that previous/largest element? What is that? Largest apple? Largest number? Anything else?
 
G Atwal
Ranch Hand
Posts: 71
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I wouldn't care too much about the form. For me, what deserves more attention is the intent. Why are the variables being initialized referring to the idea of "element" but the value assigned talks about a line that was read. If the concern is really about clean code, I'd rather discuss what name is a better representation of what "lineRead" really means with respect to previousElement and largestElement. Maybe firstElement?



Well in this case lineRead refers to the value pulled from a text/terminal input before anything else is done.For the moment, it is just a line the program has read. If it is the first value being pulled, it is set as the largestElement and previousElement. I used the term element because every valid value (definition of valid in this case I wont discuss here) is then added to a linkedList.
 
G Atwal
Ranch Hand
Posts: 71
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Junilu Lacar wrote:I'd rather discuss what name is a better representation of what "lineRead" really means with respect to previousElement and largestElement. Maybe firstElement?


And in general it is a bit unintuitive that lineRead is communicated as an element. Elements more conventionally referring to elements within an array for example or list. But that's purely in the context of data structures.

@OP
What is that previous/largest element? What is that? Largest apple? Largest number? Anything else?




In this case, the information is being processed as a string, but the text files could contain words or numbers. It is not limited to one type of information. and the term element was used because whatever values largestElement and previousElement hold, it WILL and MUST exist in the linkedList.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

G Atwal wrote:In this case, the information is being processed as a string, but the textfiles could contain words or numbers. It is not limited to one type of information


Ok. And if it is a word entered there as an element, what would mean largestElement then?
 
G Atwal
Ranch Hand
Posts: 71
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:
Ok. And if it is a word entered there as an element, what would mean largestElement then?



Using the compareTo() method, they will be checked lexicographically
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, understood, in which case probably element makes sense now that you explained.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the real semantics of the original example are:


I don't mind the multi-assignment per se, as it is more compact and — as long as you don't do tricky operations in the middle — easy to comprehend.

However, it's generally discouraged, in part because requirements may change over the life of the app, making it necessary to break the statement apart, which can itself be confusing.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Actually, the real semantics of the original example are:


No. It is like I mentioned in my earlier post, because the associativity of "=" operator is from right to left.

earlier, I wrote:When you gave those two versions, the more correct of second version in respect of first one would be:


 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Liu is right.

The value of an assignment expression is the value being assigned so you might also argue that it's the same as

The bytecode might give you a more definitive answer but largestElement is definitely assigned the value of lineRead before previousElement is assigned the same value.
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And that's what I get from copying the expanded lines instead of re-factoring the original line

Yup.

It would be horrible if the logic were reversed, as you'd have no clue what the original value of largestElement when it assigned to previousElement.

As far as code generation goes, the optimiser shouldn't care which of the 2 forms you use. They'd both result in the same bytecode. IIRC, the bytecode interpreter is a stack machine, so it would be:

1. Load value of lineRead onto stack.
2. Duplicate top of stack
3. pop and save top of stack in largestElement
4. pop and save top of stack in previouselement

Or swap 3 and 4, since they are independent.

On an register-based machine, it gets even terser in native code, since writing from a register doesn't destroy the value and you therefore don't have to duplicate it.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . The bytecode might give you a more definitive answer . . .

Who needs bytecode? The assignment operators all associate to the right, as you will see from the grammar shown in the JLS (=Java® Language Specification), and that is what Liutauras explained.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My opinion, for what it's worth now, is that if you have to have a discussion about operator precedence to explain or clarify the first option, then the second option is preferable.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Let's see whether there is anything in the old Sun style guide. Not obviously, no


Well, there's section 10.4:

Do not use embedded assignments in an attempt to improve run-time performance. This is the job of the compiler. Example:

should be written as


In our case, it's not something being done specifically for performance.  But I think it's close enough that the rule would apply.

Regardless, I agree with Paul's last comment.  While I do think people should know the orders of precedence, for their own benefit, I don't want to rely on that in my code.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I did see that section and thought it didn't apply here. I agree about not needing to rely on the precedences (and associativities) however.
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:
Regardless, I agree with Paul's last comment.  While I do think people should know the orders of precedence, for their own benefit, I don't want to rely on that in my code.



I "know" the orders of precedence. But I use lots of parentheses. Because it visibly enforces them for the benefit of the reader. And because not every language I work in has exactly the same priorities.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Junilu Lacar wrote:. . . The bytecode might give you a more definitive answer . . .

Who needs bytecode? The assignment operators all associate to the right


It's more from a pedantic perspective, whether the actual operations are essentially

or

The effect is the same and it's just a matter of saying for sure if it's six or a half dozen.

This code:

decompiles as

0: aload_0
1: iconst_0
2: aaload
3: dup
4: astore_1
5: astore_2
6: return


If I'm not mistaken, this means that what's actually happening under the hood is essentially:

and not
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . six or a half dozen. . . .

Not six and two threes?
I got the same from javap; that is consistent with what the JLS section I quoted says because astore_2 precedes astore_3.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:IIRC, the bytecode interpreter is a stack machine, so it would be:

1. Load value of lineRead onto stack.
2. Duplicate top of stack
3. pop and save top of stack in largestElement
4. pop and save top of stack in previouselement


That looks spot on from what I see in my experiments.

Tim Holloway wrote:
As far as code generation goes, the optimiser shouldn't care which of the 2 forms you use. They'd both result in the same bytecode.


Different incantations produce slightly different bytecode:

produces this:

0: aload_0
1: iconst_0
2: aaload
3: astore_1
4: aload_1
5: astore_2
6: return


Whereas this:

produces this:

0: aload_0
1: iconst_0
2: aaload
3: astore_1
4: aload_0
5: iconst_0
6: aaload
7: astore_2
8: return

 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
G Atwal, cowgratulations, your topic has been published in our CodeRanch monthly journal: https://coderanch.com/wiki/754594/CodeRanch-Journal-September
 
reply
    Bookmark Topic Watch Topic
  • New Topic