Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > The greeting code in Java

Reply
Thread Tools

The greeting code in Java

 
 
blmblm@myrealbox.com
Guest
Posts: n/a
 
      06-20-2011
In article <(E-Mail Removed)>,
Saeed Amrollahi <(E-Mail Removed)> wrote:
>
> On Jun 19, 7:01 pm, Jeff Higgins <(E-Mail Removed)> wrote:
> > On 06/19/2011 10:17 AM, Saeed Amrollahi wrote:


[ snip ]

> Actually, I am going to prepare a seminar under the title: C++ vs.
> Java
> I try to compare C++ and Java from point of programming view.


All the more reason to try for examples that make idiomatic use
of language features, then. Your C++ program doesn't read its input
a character at a time; why should the Java program?

--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
 
Reply With Quote
 
 
 
 
Jeff Higgins
Guest
Posts: n/a
 
      06-20-2011
On 06/20/2011 01:00 PM, Jeff Higgins wrote:

> Java: Accounts of My Explorations.
> 1. Hello


.... The maddening rattle-clank of the trucks on rails subsided as our
train approached Java Station. Outside the dusty, smokey panes of the
stifling car, amid the lush forest of Java, suddenly loomed a great line
of platforms. On and on they seemed to stretch, each different and I
hadn't been informed where to disembark. I reached for my preprocessor;
but the brown-eyed girl, sensing my anxiety, smiled and touched my hand.
"They are all the same" she whispered, "write once, run anywhere, we say
here". Pulling the signalling cord, she said "Come along, we'll get off
here and I'll show you." "We shall say hello and they will all
understand us, and smile and welcome us."
....

 
Reply With Quote
 
 
 
 
Tobias Blass
Guest
Posts: n/a
 
      06-21-2011
On 2011-06-20, blmblm myrealbox.com <(E-Mail Removed)> wrote:
> In article <itlgsn$vr8$(E-Mail Removed)>,
> Jeff Higgins <(E-Mail Removed)> wrote:
>>
>> On 06/19/2011 02:36 PM, Saeed Amrollahi wrote:
>> > On Jun 19, 6:31 pm, Jeff Higgins<(E-Mail Removed)> wrote:
>> >> On 06/19/2011 09:06 AM, Saeed Amrollahi wrote:
>> >>
>> >>> What are the problems of my code and how can I write
>> >>> a better one. Please throw some light.
>> >>
>> >> What means better?
>> >
>> > For example using less abstractions and less involve with Java
>> > stream class hierarchy.

>>
>> For a beginner, learning to write C in Java is unhelpful.

>
> Even in C one would not be likely to input a string by reading one
> charaacter at a time -- in a beginner program at least.
>


Hmm at least in K&R (the inofficial standard C learning book) you often find
while((c=getchar())!=EOF)

It's less performant than reading whole blocks but in many cases much simpler
(And that's what matters in a learning book)
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      06-21-2011
Tobias Blass <(E-Mail Removed)> writes:
>Hmm at least in K&R (the inofficial standard C learning book) you often find
>while((c=getchar())!=EOF)
>It's less performant than reading whole blocks but in many cases much simpler
>(And that's what matters in a learning book)


Usually, »getchar« already is buffered internally.

(Also, thinking about optimising even before any
requirement specification is given really seems
to be premature.)

More computing sins are committed in the name of
efficiency (without necessarily achieving it) than for
any other single reason -- including blind stupidity.

William A. Wulf

We should forget about small efficiencies, say about 97%
of the time: premature optimization is the root of all
evil.

Donald E. Knuth

We follow two rules in the matter of optimization:

Rule 1. Don't do it.

Rule 2 (for experts only). Don't do it yet -- that is,
not until you have a perfectly clear and unoptimized
solution.

M. A. Jackson

 
Reply With Quote
 
Tobias Blass
Guest
Posts: n/a
 
      06-21-2011
On 2011-06-21, Stefan Ram <(E-Mail Removed)-berlin.de> wrote:
> Tobias Blass <(E-Mail Removed)> writes:
>>Hmm at least in K&R (the inofficial standard C learning book) you often find
>>while((c=getchar())!=EOF)
>>It's less performant than reading whole blocks but in many cases much simpler
>>(And that's what matters in a learning book)

>
> Usually, »getchar« already is buffered internally.
>
> (Also, thinking about optimising even before any
> requirement specification is given really seems
> to be premature.)
>
> [snip some quotes]
>
> M. A. Jackson
>


I know these quotes (every programmer should).
I didn't want to warn of using getchar. I must admit the performance thing was a
guess (and I think I read it somewhere), but even if it is the second part of
the sentence applies. Premature Optimization is mainly evil _because_ you
sacrifice simplicity (there are other reasons but this is the most important
one).
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-21-2011
On Tue, 21 Jun 2011 09:34:34 +0000 (UTC), Tobias Blass
<(E-Mail Removed)> wrote:

[snip]

>I know these quotes (every programmer should).
>I didn't want to warn of using getchar. I must admit the performance thing was a
>guess (and I think I read it somewhere), but even if it is the second part of
>the sentence applies. Premature Optimization is mainly evil _because_ you
>sacrifice simplicity (there are other reasons but this is the most important
>one).


This point was covered neatly in "Code Complete". At least, it
was in the first edition. I have not read the latest.

McConnell was writing encryption software to run on an original
IBM pc. He optimised and optimised to speed up his C code. Finally,
he hucked it and rewrote in Assembler. His observation was that as
more and more optimisations were added, the code became much less
readable.

I like to optimise for code readability. If code is useful, it
generally will have a long lifetime. That gives more opportunity for
changes being required. Changes are a real bother when the code is
not easily readable.

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
blmblm@myrealbox.com
Guest
Posts: n/a
 
      06-21-2011
In article <itpi56$9du$(E-Mail Removed)>,
Tobias Blass <(E-Mail Removed)> wrote:
> On 2011-06-20, blmblm myrealbox.com <(E-Mail Removed)> wrote:
> > In article <itlgsn$vr8$(E-Mail Removed)>,
> > Jeff Higgins <(E-Mail Removed)> wrote:
> >>
> >> On 06/19/2011 02:36 PM, Saeed Amrollahi wrote:
> >> > On Jun 19, 6:31 pm, Jeff Higgins<(E-Mail Removed)> wrote:
> >> >> On 06/19/2011 09:06 AM, Saeed Amrollahi wrote:
> >> >>
> >> >>> What are the problems of my code and how can I write
> >> >>> a better one. Please throw some light.
> >> >>
> >> >> What means better?
> >> >
> >> > For example using less abstractions and less involve with Java
> >> > stream class hierarchy.
> >>
> >> For a beginner, learning to write C in Java is unhelpful.

> >
> > Even in C one would not be likely to input a string by reading one
> > charaacter at a time -- in a beginner program at least.


s/charaacter/character/, of course (why do these details only become
apparently *after* posting?)

> Hmm at least in K&R (the inofficial standard C learning book) you often find
> while((c=getchar())!=EOF)
>
> It's less performant than reading whole blocks but in many cases much simpler
> (And that's what matters in a learning book)


Well, if you say so (my copy of K&R is not easily accessed right
now). Certainly that style makes sense in examples in which
the goal is to process a character at a time (to classify/count
characters, for example, or to copy one file to another).

But is this style used in examples in which the objective is to
collect one whitespace-delimited "string" and print it? Huh.
Maybe I need to reread K&R ....

--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
 
Reply With Quote
 
Tobias Blass
Guest
Posts: n/a
 
      06-22-2011
On 2011-06-21, blmblm myrealbox.com <(E-Mail Removed)> wrote:
> In article <itpi56$9du$(E-Mail Removed)>,
> Tobias Blass <(E-Mail Removed)> wrote:
>> On 2011-06-20, blmblm myrealbox.com <(E-Mail Removed)> wrote:
>> > In article <itlgsn$vr8$(E-Mail Removed)>,
>> > Jeff Higgins <(E-Mail Removed)> wrote:
>> >>
>> >> On 06/19/2011 02:36 PM, Saeed Amrollahi wrote:
>> >> > On Jun 19, 6:31 pm, Jeff Higgins<(E-Mail Removed)> wrote:
>> >> >> On 06/19/2011 09:06 AM, Saeed Amrollahi wrote:
>> >> >>
>> >> >>> What are the problems of my code and how can I write
>> >> >>> a better one. Please throw some light.
>> >> >>
>> >> >> What means better?
>> >> >
>> >> > For example using less abstractions and less involve with Java
>> >> > stream class hierarchy.
>> >>
>> >> For a beginner, learning to write C in Java is unhelpful.
>> >
>> > Even in C one would not be likely to input a string by reading one
>> > charaacter at a time -- in a beginner program at least.

>
> s/charaacter/character/, of course (why do these details only become
> apparently *after* posting?)
>
>> Hmm at least in K&R (the inofficial standard C learning book) you often find
>> while((c=getchar())!=EOF)
>>
>> It's less performant than reading whole blocks but in many cases much simpler
>> (And that's what matters in a learning book)

>
> Well, if you say so (my copy of K&R is not easily accessed right
> now). Certainly that style makes sense in examples in which
> the goal is to process a character at a time (to classify/count
> characters, for example, or to copy one file to another).
>
> But is this style used in examples in which the objective is to
> collect one whitespace-delimited "string" and print it? Huh.
> Maybe I need to reread K&R ....
>


Obviously it is not the right choice for any problem you might encounter (the
K&R examples are copy input to output, count characters, count lines etc.) but
there are situations where getchar() is useful and one should be aware that
it exists. I don't want to say that one should use it everywhere.
 
Reply With Quote
 
blmblm@myrealbox.com
Guest
Posts: n/a
 
      06-22-2011
In article <its9fk$5jt$(E-Mail Removed)>,
Tobias Blass <(E-Mail Removed)> wrote:
> On 2011-06-21, blmblm myrealbox.com <(E-Mail Removed)> wrote:
> > In article <itpi56$9du$(E-Mail Removed)>,
> > Tobias Blass <(E-Mail Removed)> wrote:
> >> On 2011-06-20, blmblm myrealbox.com <(E-Mail Removed)> wrote:
> >> > In article <itlgsn$vr8$(E-Mail Removed)>,
> >> > Jeff Higgins <(E-Mail Removed)> wrote:
> >> >>
> >> >> On 06/19/2011 02:36 PM, Saeed Amrollahi wrote:
> >> >> > On Jun 19, 6:31 pm, Jeff Higgins<(E-Mail Removed)> wrote:
> >> >> >> On 06/19/2011 09:06 AM, Saeed Amrollahi wrote:
> >> >> >>
> >> >> >>> What are the problems of my code and how can I write
> >> >> >>> a better one. Please throw some light.
> >> >> >>
> >> >> >> What means better?
> >> >> >
> >> >> > For example using less abstractions and less involve with Java
> >> >> > stream class hierarchy.
> >> >>
> >> >> For a beginner, learning to write C in Java is unhelpful.
> >> >
> >> > Even in C one would not be likely to input a string by reading one
> >> > charaacter at a time -- in a beginner program at least.


[ snip ]

> >> Hmm at least in K&R (the inofficial standard C learning book) you often find
> >> while((c=getchar())!=EOF)
> >>
> >> It's less performant than reading whole blocks but in many cases much simpler
> >> (And that's what matters in a learning book)

> >
> > Well, if you say so (my copy of K&R is not easily accessed right
> > now). Certainly that style makes sense in examples in which
> > the goal is to process a character at a time (to classify/count
> > characters, for example, or to copy one file to another).
> >
> > But is this style used in examples in which the objective is to
> > collect one whitespace-delimited "string" and print it? Huh.
> > Maybe I need to reread K&R ....
> >

>
> Obviously it is not the right choice for any problem you might encounter (the
> K&R examples are copy input to output, count characters, count lines etc.) but
> there are situations where getchar() is useful and one should be aware that
> it exists. I don't want to say that one should use it everywhere.


Well, we agree that there are situations in which reading input
a character at a time makes perfect sense, and others in which it
doesn't. I claim that the OP's greeting program is in the second
category. You might disagree!

--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The greeting code in Java Saeed Amrollahi Java 15 07-24-2011 02:15 AM
Greeting cards application in java - what is the best approach? tak Java 0 02-27-2007 11:29 PM
Thanks to all - Eastern greeting - No Question Matthias S. ASP .Net 1 03-27-2005 02:21 PM
Url / Page Greeting? Leon ASP .Net 2 12-03-2004 02:47 PM
HTML problem with Nova Greeting Card Factory Sharon HTML 6 01-03-2004 03:38 AM



Advertisments