• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

small program discussion

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I was wondering if we could discuss a program and try to understand why the author may have chosen to write the program in that way.
The code and an explanation is at:
http://java.oreilly.com/bite-size/java_0299.html
A couple of things first, I'm wondering why it was written like this:
while (true) addClient(serverSocket.accept());
as opposed to:
while (true) {
Socket connection = serverSocket.accept();
addClient(connection);
}
another thing is, I couldn't quite understand why this was written the way it was, is it normal or is there a neater way?
public abstract class Listener {
public abstract void processLine(String line);
private BufferedReader mIn;
public Listener(InputStream in) {
mIn = new BufferedReader(new InputStreamReader(in));
Thread t = new Thread() {
public void run() {
try {
String line;
while ((line = mIn.readLine()) != null) processLine(line);
}
catch (IOException ioe) {}
}
};
t.start();
}
}
When an instance of Listener is made in the addClient() it is anonymous why?
new Listener(clientSocket.getInputStream()) {
public void processLine(String line) {
if (line.length() == 0) mClients.remove(out);
else sendToClients(line);
}
};
Thanks for your time.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It all depends what style guide, if any, the author follows. Also the level of experience expected from a reader of the code. I would write it:

That would be the style expected on this site's CattleDrive. (But I stand to be corrected.)
Regarding the anonymous classes. That is an idiom used when a class is going to be used in one place only and extends another class (or implements an interface) in a very simple way. The author wishes to keep the definition close (in the code) to the actual use of the class as possible. This can be confusing to a beginner but after a time starts to become acceptable for trivial class extensions. If it's more code than these examples then it could perhaps better be done with toplevel classes.
Notice that the author said "in about 150 lines", that number is obviously influenced by the style of coding followed.
Question: should we write all code as if for a beginner's eyes?
-Barry
[ December 22, 2002: Message edited by: Barry Gaunt ]
 
Wan Zifty
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your comments. I think that all programs should be written so they can be understood by beginners especially if it's gonna be on O'Reilly's website!
Possibly if you're writing a small program for another company as a one off or specifically don't want anyone else to understand it, then maybe you wouldn't have to make it easy to understand.
Something else too. The Listener class, I don't understand why it 'uses' a Thread instead 'being a' Thread by extending Thread or implementing Runnable.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic