Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

do... whiles are good...

 
Richard Whitely
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Programming Style Guide area of JavaRanch it states:

"Never use a do...while"
I belive that there are certain situations where a do...while is good e.g. examining a resultset (which isn't scrollable).

Example with do...while
-----------------------

Example without do...while
--------------------------

Any opinions are welcome... especially any equaly attractive sollutions which dont ude do...while.
 
Graeme Brown
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am inclined to agree. I don't think this should be a hard and fast rule. Sometimes logic dictates that this is the correct construct.
However the reasoning for the rule in JavaRanch is valid.
The way I get round this is by placing the code for the do while in a method, therefore it is still possible to see from the start of the loop what the termination condition is.

Very occasionally I will stretch this to two or three method calls, but never any more than that. I know this makes it hard to define a "rule", maybe it should be a guideline.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tend to agree with the Ranch on this one...just for consistency. This is how I work the code so that it works for me:

since I usually need the row count as well, there is no real performance cost of incrementing the counter. also to note, that incrementing a counter is hardly a performance cost especially when you start making hits to the database!
Jamie
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Another choice
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"JavaGibbon"
Your name doesn't agree with the javaranch guidelines. Please take a moment and edit your profile after reviewing the guidelines at http://www.javaranch.com/name.jsp
Please change your Publicly Displayed name to follow the guidelines.
thanks for your cooperation.
- satya
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic