absolutely!
works out at ~3 less bytecodes =)
absolutely!
works out at ~3 less bytecodes =)
I know what it’s good for, but the syntax is just horrible.
All other blocks have their flow control defined at the top. When reading code with a do {…} while block, you have to look down (or even worse, SCROLL down) to know what the block is about.
do
{
banana();
}
while (!isDone());
vs
boolean done = false;
while (!done)
{
banana();
done = isDone();
}
Sure, it’s slightly more verbose, and it requires an extra variable, but at least it reads right.
It makes sense though… the flow control is checked only after executing the body of the block, so the code is “where it should be” in that sense.
You have to scroll down to see what it is going to do AFTER running the body, not so you can see “what the block is about”.
Maybe we should just get rid of while, do, and for, and use if and goto exclusively
loop:
do stuff();
if (!done)
goto loop;
Is that better?
Well, that’s kinda my point.
I think
do
{
stuff();
}
while (!done)
is just as evil as
loop:
stuff();
if (!done)
goto loop;
Don’t get me started on break and continue.
(By the way, just to make this clear: I use do {…} while, break and continue in my code when it’s needed. I just don’t think they’re “pure”. This is all mostly tongue in cheek.)
One of my favourite additions that Java has that C/C++ is missing is the labelled break and labelled continue. I find they make the code much cleaner than introducing extra variables and logic in inner loops.
So I guess I prefer do…while to introducing that extra variable, and that “special case” init code outside the loop to “set things up so it works right for the first pass”… to me do…while is the cleaner solution. Though I do get where you are coming from with respect to the consitency of having all of the loop logic at the beginning loop statement. It’s all about personal preference really… there is nothing scientifically right or wrong about either approach.
[quote]This is all mostly tongue in cheek.
[/quote]
ditto.
whileButAtLeastOnce(!done)
{
stuff();
}
!!!
do{…}while just sounds plain wrong…:
“do your thing while the dog is out”
does that imply that I should do my thing at least once… nope.
The correct construct should have been lifted from BASIC’s “repeat…until” which makes far, far more sense.
Too late now. The milk puddle evaporated 10 years ago…
Cas
I think we have all heard lots of warnings about these things. instanceof, break, continue, multiple return statements, and so on. But these all have (few) legitimate uses, where they simplify matters a lot. The one syntactical feature of java which I cannot say this about, is the following:
int[] a; //An int-array called a
int b[]; //An int called b, or, well, make that, like, a whole array of them now that we're at it, because now the caret is all the way over to the right of the identifier and I can't be bothered to use my left arrow key to go back and change it.
I prefer the Java way:
int[] a;
But I assume this was left in so the transition for C programmers would be easier
int b[];
I agree, it is lame.
I think you hit it on the head. To most of us, and to the Java designers, int[] makes eminently mreo sense but if you tink back to the beginnign of Java one of the reasons Java succeeded where any other simialr new languages failed is that transiton from C or C++ was realtively easy. Im sure this was to head of resistance to the chnage of syntax on arrays,
Then again, you access it as b[i], and instantiate it (usually) as new b[10].
nope, I instantiate it like this:
int[] b = new int[10];
but actually if we want to be real strict, it should be:
int[] b = new int[](10);
and
b.get(i) instead of b[i]
Oops, it’s new int[10] of course.
* Markus_Persson blames having potato chips for breakfast