File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes do... whiles are good... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "do... whiles are good..." Watch "do... whiles are good..." New topic
Author

do... whiles are good...

Richard Whitely
Greenhorn

Joined: Feb 08, 2002
Posts: 2
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

Joined: Oct 13, 2000
Posts: 193
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

Joined: Jul 09, 2001
Posts: 1879

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

Joined: Jul 22, 2000
Posts: 9047
    
  10

Another choice


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
"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


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: do... whiles are good...