aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A matter of coding style: Assignment statement in loop expressions. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A matter of coding style: Assignment statement in loop expressions." Watch "A matter of coding style: Assignment statement in loop expressions." New topic
Author

A matter of coding style: Assignment statement in loop expressions.

Marcus Jastrebowski
Ranch Hand

Joined: Nov 15, 2007
Posts: 55
I have been slowly digging my way through the Chapter 6, in Sierra/Bates. And, in a couple of examples, I stumbled upon these type of constructs:

I'm wondering why don't we write this fragment simply like that:

It seems to me that the second fragment is stylistically more readable (and compact). Also, the variable 'b' is not used anywhere, so what is the assignment for? But then of course I've been wondering if my idea of code readability (and proper programming style) is rather shaky in this case? Or perhaps the intention of these examples in the book is to show how SCJP obfuscates the code readability?

Best Wishes,
Marcus
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Variable b is used to evaluate the while loop. which could have been avoided by the second example. Second example is better as it saves one addtional boolean variable creation and assignment fom hasNext to boolean.


SCJP, SCWCD, SCBCD
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Marcus Jastrebowski:
Or perhaps the intention of these examples in the book is to show how SCJP obfuscates the code readability?


Hi Marcus,

I'm pretty sure you hit the nail on the head there. The SCJP exam designers seem to love trying to trip people up by using = instead of == in conditions, so K&B puts a bunch of these constructs here and there to give you practice in looking out for this trick. As you noted, it's rarely (if ever) useful to actually use such expressions if you're not an evil exam writer.


SCJP 5.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A matter of coding style: Assignment statement in loop expressions.