store whole InputStream in a String

A

Andrew Thompson

On Mon, 13 Sep 2004 09:19:22 -0600, Chris Smith wrote:

(long URL's break)
..That's why
you'll often see me use a quote character before a long URL.

How does that look? Does it preserve the
URL or simply inform the reader?

I had a quick look back through your
posts but did not see any links to
longer URL's.
 
A

Andrew Thompson

Andrew Thompson wrote: ...
..Here's an example:

That's brill., I'll have to suggest it to
the Gravity user I know who gives lots
of long links. I may even reconsider
using Gravity, myself.

Tah.
 
C

Chris Smith

Paul said:
Yes, I agree. My point was that one could create new entry points through a
liberal scattering of labels, as in my example.

I don't see what you're saying at all. You certainly cannot create new
entry points to a block of code. A block of code can only be entered at
the beginning, and its statements will be run sequentially until it
finishes (whether due to break, continue, return, throw, or simply
reaching the end of the statements in the block).

So out of curiosity, how do you feel about multiple returns in a method?
How about exceptions? These two are probably are responsible for far
more multiple exit points from a block of code than break or continue.
Yes, it cannot be written in Java, because one cannot have forward
references to a label in Java.

That isn't the issue. It's equally not possible to do:

someLabel:
{
...
}

break someLabel;

even though that's not a forward reference. The point is not the
lexical structure, but that break and continue are statements for
exiting a block, and they can only be used from within the block they
are exiting. Yes, your explanation makes them sound more like goto, to
your advantage, but it's also not accurate, and doesn't explain the
behavior and set of legal situations where these statements can be used.
My sole point was that this ability to scatter labels about, if exercised by
a student or someone in a hurry, would quickly devolve into a jumble of
difficult-to-interpret code.

Here's where we will differ. The random strewing about of any control
structure will result in difficult to interpret code. If you can
demonstrate that people using break/continue to solve real problems are
likely to write difficult code, that would be a different matter.
However, labeling an if statement doesn't qualify as a realistic thing
for anyone to do, unless someone is trying to write nonsense code.

If you want to see nonsense code that's hard to follow but uses only
single exit points from a block of code, here you go:

int a = 1, b;
for (b = 40; a < 17; b += a)
{
while (b % a != 3)
{
if (b & 0xA == 2) b--;
else a++;
}
}

To teach people to write readable code, you teach them to think about
what their code is doing, and choose ways of expressing it that make
sense. Absolute technical rules, like "one entry, one exit", only make
code more readable when they match the logic that the programmer is
writing. If the problem domain expresses something with multiple exits,
then I'd encourage a programmer to use a control structure with multiple
exits -- even if that means starting with "while (true)".
I think structured programming purists would argue that it may take longer
to design an algorithm without break/continue, but the resulting code will
be more comprehensible, not less.

You're ignoring what I said. I didn't say that the algorithm without
break/continue would take longer to "design" (to the extent that a
choice between control structures within a method can be called design),
but that the resulting code will be more complex. Introducing temporary
local variables and extra levels of nesting just to avoid a break
statement is not taking the hard road to doing it right; it's spending
more effort to come up with an inferior result.

I'm mostly ignoring the rhetoric about use of break/continue not really
being structured programming. However, for the sake of those who may
assume Paul's terminology is authoritative, I'll point out that it's
not. When discussing structured programming with others, it would be
helpful to point out if you mean something unusual, like excluding the
use of break and continue statements, instead of the usual structured
programming practices of top-down design, etc.
One often sees college coursework in
which the use of break/continue is simply forbidden,

I've never seen this. Have you seen this?
Why would Sun
go out of their way to design and implement a new extension to a
language feature, when they've only included it as a concession to non-
structured programmers?

Why indeed? [...]

That's not an answer. Clearly, if Sun's engineers thought break and
continue are useless statements that are to be discouraged, they could
have at least not elaborated on them with new innovative features.
Sorry, but you can't claim the language designers at Sun as part of a
movement to abolish these control structures.
Also there is the issue of how they are used. They can easily be misused, as
in my example, so one could argue instead that the circumstances of their
use, not their presence in the language, is the real issue.

I would argue that misuse is the issue, except that I don't see frequent
examples of their misuse. Being able to write confusing nonsense code
is not proof that the constructs in your code are misused, either easily
or frequently. You've given something like a random combination of
English words that obeys all the rules of grammar. Writing real code --
or English -- requires both intelligence, and having something to say.
THEN the question of whether the code says it well can be debated.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

ak

I suppose you think the program
shouldn't be configurable at all in case you break it?
no, I just think it should be possible to configure it back to working state
without actions like
"delete user directory"
The fact that you readily try to blame Paul for messing up the program menus
that *you* reconfigured badly speaks volumes.
no, I try to blame Paul for his art to communicate.

I posted worked easy understandable code and he was really hard to me.
So I just try to show that he is not perfect man.
May be I make it wrong way...

Back to his editor - tell me why if I put file list at top of menu
and then click on "clear file list" then is File menu completely cleared?
Is it not a bug?
If you use, and try to configure, a free program, then you do so at your own
risk.
ANY programm is used at own risk Please read license agreement.
No satisfaction is guaranteed at all. If you think something is wrong
with it, you could try telling the author - politely - or you could just stop
using the program.
That's it, but the same can be made with commercial programm too.
So tell me, who are the writers of free software thinking of, if not their
users?
they may just like to write programms.
I would argue that people selling commerical software are primarily thinking of
themselves - i.e. their profits - and are *less* likely to think of the
end user.
Don't be naive.. They MUST think about users, no users - no profit...
Unsatisfacted user means less profit in the future.
 
R

Rogan Dawes

Paul Lutus wrote:

This doesn't solve the problem that the original do-loop doesn't have a
provision to terminate.

Neither does your for-loop example, unless there is no input on the
first read.

It seems to me that the buffer is read only once at loop start, but len
is never subsequently updated.

I would also have expected AK's version to be correct:

for (int len=is.read(buffer); len>0; len=is.read(buffer)) {
// do something with buffer
}

because it updates len at the end of each loop, which yours does not.

I might actually start using this in preference to the while construct.
I quite like it, stylistically.
All apart from an error I have also been ignoring:

len = inputStream.read[buffer]; -> len = inputStream.read(buffer);

Well, I was ignoring it too, until I saw you sniping about an error that
YOU originally made yourself!!

---snip-----
??? Please consider the following, and avoid constructions like
"while(true)" and "break" when possible:

int len;

while((len = inputStream.read[buffer]) > 0) {
bout.write(buffer, 0, len);
}
---snip------

and

-------------
I could instead have said:

for (int len = inputStream.read[buffer]); len > 0;) {
}

--------------

;-)

Note that you also claimed to have compiled and tested that, which I
somewhat doubt ;-)

---snip-------

Quite false. It is entirely appropriate and a common coding practice.



I compiled it, then posted it. You could do the same.

---snip---------

Rogan
 
P

Paul Lutus

Rogan said:
Paul Lutus wrote:



Neither does your for-loop example, unless there is no input on the
first read.

Which for-loop example are you speaking of? The current one, posted five
times? Now six times:

// using while

int len;

while((len = inputStream.read(buffer)) > 0) {
        bout.write(buffer, 0, len);
}

// using for

for(int len;(len = inputStream.read(buffer)) > 0;) {
        bout.write(buffer, 0, len);
}
It seems to me that the buffer is read only once at loop start, but len
is never subsequently updated.

I would also have expected AK's version to be correct:

for (int len=is.read(buffer); len>0; len=is.read(buffer)) {
// do something with buffer
}

because it updates len at the end of each loop, which yours does not.

I might actually start using this in preference to the while construct.
I quite like it, stylistically.

Again, it has a duplicate line of code where this is not necessary. It is
bad coding practice.
All apart from an error I have also been ignoring:

len = inputStream.read[buffer]; -> len = inputStream.read(buffer);

Well, I was ignoring it too, until I saw you sniping about an error that
YOU originally made yourself!!

I certainly did. See above.
 
R

Rogan Dawes

Paul said:
Rogan Dawes wrote:




Which for-loop example are you speaking of? The current one, posted five
times? Now six times:

Sorry about that. I was reading the thread top down, and had not
encountered your corrections yet.
Again, it has a duplicate line of code where this is not necessary. It is
bad coding practice.

Is it really so bad? I feel that it is being explicit about how the loop
is initialised, as well as being explicit about how it is updated at the
end of each loop. Isn't that appropriate for a for-loop?

I understand that you don't like using duplicate lines of code in
different sections of the algorithm, possibly obscured by intervening
code doing other things. But when it is on the same line, even in the
same construct, surely it is reasonable to expect a maintenance
programmer to understand what is happening?

Your version using the for-loop simply makes use of the for contructs
ability to enclose the variable definition, without actually using the
rest of the for-loop's features.

I realise that you don't really think much of your version using the
for-loop, and so don't support it much, but doesn't it make sense to use
what the construct offers?

I have also used the equivalent of your while-construct in my code, and
understand it perfectly. I just think that the for-construct could be
slightly easier to understand.

Rogan
 
P

Paul Lutus

Rogan Dawes wrote:

/ ...
Is it really so bad?

This is an issue for software maintainers and managers, it is not an issue
of "get it working and ship it." Software reliability is improved when
there is only one line of code for each distinct operation. If any part of
the duplicated line changes, the maintainer has to locate and change two
lines, not one, and make the change in exactly the same way. This has been
the source of any number of persistent, difficult-to-trace bugs.
I feel that it is being explicit about how the loop
is initialised, as well as being explicit about how it is updated at the
end of each loop. Isn't that appropriate for a for-loop?

Not if it can be done in a simpler way, one that has a single line of code
for each distinct operation.
I understand that you don't like using duplicate lines of code in
different sections of the algorithm, possibly obscured by intervening
code doing other things. But when it is on the same line, even in the
same construct, surely it is reasonable to expect a maintenance
programmer to understand what is happening?

Not if his workload is doubled in a flash. :)
Your version using the for-loop simply makes use of the for contructs
ability to enclose the variable definition, without actually using the
rest of the for-loop's features.

Yes, that's right. But it is valid code, and it is just a variant on the
while-loop version, which I prefer.
I realise that you don't really think much of your version using the
for-loop, and so don't support it much, but doesn't it make sense to use
what the construct offers?

The three clauses are optional in valid code. Even the following is valid
(although it is indistinguishable from "while(true)", and has all the same
objections):

for (;;) {
}
I have also used the equivalent of your while-construct in my code, and
understand it perfectly. I just think that the for-construct could be
slightly easier to understand.

I have posted a for-loop version of my original while-loop construction (I
don't mean original with me, not by any means). It causes the "len"
variable to be local to the controlled block, but it is otherwise identical
in its operation. It meets the above criterion -- no duplicate lines.

I want to emphasize this is entirely about the maintainiability of software
that lives for years or decades. It is not about whether something works in
an algorithmic sense.
 
A

Alex Hunsley

ak said:
no, I just think it should be possible to configure it back to working state
without actions like
"delete user directory"

I think deleting the user directory to make something work again is a
small inconvenience compared to the benefit of getting free software. Do
you not think so?
no, I try to blame Paul for his art to communicate.

I posted worked easy understandable code and he was really hard to me.
So I just try to show that he is not perfect man.
May be I make it wrong way...

I don't think he was being really 'hard' on you. He was saying he didn't
like something about what you wrote, I don't think it was personal in
any way.
Back to his editor - tell me why if I put file list at top of menu
and then click on "clear file list" then is File menu completely cleared?
Is it not a bug?

Perhaps it is, I got the impression from what Paul said that something
had gone wrong as a result of something silly you'd done. Maybe it is a
bug! I'm not sure at this point. If it *is* a bug, then please disregard
my comment about you breaking it...
ANY programm is used at own risk Please read license agreement.

Ok, maybe I said that wrong. I mean that with free software, when you
haven't paid any money for it, you've got less right to expect support,
and instant fixes to any problems... of course it's probably in the
interest of the author of freeware to try and fix problems.
That's it, but the same can be made with commercial programm too.

Not really, if you buy a commercial program and you have problems with
it, you could expect support, or perhaps some sort of refund.
users?
they may just like to write programms.



thinking of


end user.
Don't be naive.. They MUST think about users, no users - no profit...
Unsatisfacted user means less profit in the future.

Yes, it is in their interests to make it usable and useful to a certain
level, but they also have profits in mind. With freeware, the author is
primarily concerned with the usefulness of the software - there is no
profit consideration.

alex
 
A

ak

No satisfaction is guaranteed at all. If you think something is wrong
Not really, if you buy a commercial program and you have problems with
it, you could expect support, or perhaps some sort of refund.

At this part commercial programm seems to be better.
I don't think he was being really 'hard' on you. He was saying he didn't
like something about what you wrote, I don't think it was personal in
any way.

Ok, he was hard to my code, this is nearly the same ;-)
Yes, it is in their interests to make it usable and useful to a certain
level, but they also have profits in mind. With freeware, the author is
primarily concerned with the usefulness of the software - there is no
profit consideration.

Profit can have many forms. Money is only one of them.
 
M

Mike Schilling

Paul Lutus said:

Duelling academic papers!

http://stwww.weizmann.ac.il/g-cs/benari/articles/loop.pdf

This one claims that loops which exit in the middle can be easier to verify.
It says, among other things:

WHERE TO EXIT A LOOP
Where should we place the exit point of a loop? Actu-
ally, it doesn't matter. Any loop will have an invari-
ant and an exit condition. There is no a-priori reason
to suppose that an exit at the start of the loop is bet-
ter than an exit from the middle.

There's also a famous paper by Eric Roberts of Stanford called "Loop exits
and structured programming: reopening the debate". It can be found at

http://portal.acm.org/citation.cfm?...nical Symposium on Computer Science Education

though you'll need an ACM web account to read it. The abstract says:

Internal exits from loops represent a critically important control
structure that should be taught in the introductory CS1 curriculum.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top