11 ms·
What is up with the dodgy numbering? I hope he didn't get a Scala program to do the list!!! I am always deeply suspicious of any appeal for using a language t
by Stormbringer 16y ago
What is up with the dodgy numbering? I hope he didn't get a Scala program to do the list!!!
I am always deeply suspicious of any appeal for using a language that uses as it's initial premise that the language is shorter or saves keystrokes.
Personally, I'd take one of his examples and make it 'worserer' by enbiggening it.
new RunningReporter();
public class RunningReporter extends Thread {
public void run() {
System.out.println("Running");
}
}
And then I'd redesign it to something useful, e.g.
public class RunningReporter extends Thread {
public void run() {
System.out.println("Running");
doSomething();
}
public void doSomething() {}
}
So then subclasses of running reporter would spit out Running to System.out.println and then go and do their funky thing.
But even that isn't as useful as it could be, it should really be something like:
public class RunningReporter extends Thread {
String message = "";
public RunningReporter(String s) {
super();
message = s;
}
public void run() {
System.out.println(message);
doSomething();
}
public void doSomething() {}
}
Okay, so now you can customise the message, so that you can tell these threads apart if you're doing stuff:
e.g.
new BluetoothRunningReporter("Spp fix");
So we've passed through shorter, and gotten bigger, but the line that would be used in the program conveys enormously much more information.
I find that upon examination, in a properly and well designed Java system almost all of the anonymity disappears... which makes me uncomfortable with that example of how Scala is better than Java, because in the real world, if you were doing it properly, you wouldn't do it that way in Java anyway.
- hamletdrc2 16y agoThe numbering is based on the "Tip" Numbers from the Effective Java book. Each tip is numbered and indexed, and it is common to use the tips during code reviews.
- msmith 16y agoI'm not sure if I'm following your point. Are you suggesting that anonymous inner classes aren't useful in a well-designed Java program? If so, I'd have to disagree. As an example, the Java Collections API (one of the most well-designed Java APIs, imho) uses Comparators extensively. It's pretty common to define the Comparator as an anonymous inner class when you need to do a one-off sort() or max() call. Scala just makes it easier to use this pattern. There's no need to go through the effort of wrapping the function in class if you're only going to use it once.