You're looking at Less Wrong's discussion board. This includes all posts, including those that haven't been promoted to the front page yet. For more information, see About Less Wrong.

cata comments on Open thread, Jan. 12 - Jan. 18, 2015 - Less Wrong Discussion

6 Post author: Gondolinian 12 January 2015 12:39AM

You are viewing a comment permalink. View the original post to see all comments and the full post content.

Comments (155)

You are viewing a single comment's thread. Show more comments above.

Comment author: cata 15 January 2015 07:37:58PM *  0 points [-]

I might try Groovy for the REPL stuff -- I was trying Clojure before, but I ran into problems getting it to get the dependencies and stuff all into the REPL (I work on a big project that uses Gradle as a build system, and Clojure doesn't usually use Gradle.)

carefully encapsulate state -- that's what the "private" and "public" keywords are for. I don't quite understand what could be the problem here (other than bad programmers not using these keywords; or the verbosity).

One pattern I have in mind here: if I have some algorithm I have to perform that has some intermediate state, I will break it down into some functions, pass the state around from function to function as necessary, and wind up composing five or six functions to get from the start to the end. Java programmers seem to often instead do something like make a class with the five or six functions as methods, and then set all the state as fields on the class, which the methods then initialize, mutate, and consume, and call the methods in the right order to get from the start to the end. That seems a lot harder for me to read because unless there is also great documentation I have to read very closely to understand the contract of each individual method.

(I'm also incidentally confused about when in Java people like to use a dependency injection tool like Guice and when people like to pass dependencies explicitly. I don't think I understand the motivation for the tool yet.)

Comment author: Viliam_Bur 16 January 2015 11:50:08AM *  0 points [-]

Java programmers are usually familiar with procedural programming, not functional. The older ones are probably former C/C++ programmers, so they mostly write C/C++ code using Java syntax. That probably includes most textbook authors.

Nothing in Java prevents you from having intermediate states, and composing the functions. You just have to specify the data type for each intermediate state, which may require creating a new class (which is a lot of typing), or typing something like Pair<Map<Integer, Integer>, List<Integer>>, so yeah, there are inconveniences.

As a crazy creative solution, I could imagine wrapping the "class with the five or six functions" into multiple interfaces. Something like this:

Old version:

class Something {
void step1(T1 param1) { ... }
void step2(T2 param2) { ... }
void step3(T3 param3) { ... }
R getResult() { return ...; }
}

New version:

interface AfterStep2 {
R step3(T3 param3);
}
interface AfterStep1 {
AfterStep2 step2(T2 param2);
}
class Something implements AfterStep1, AfterStep2 {
AfterStep1 step1(T1 param1) { ...; return this; }
AfterStep2 step2(T2 param2) { ...; return this; }
R step3(T3 param3) { ...; return ...; }
}

This would force users to write things like:

R result = something.step1(x1).step2(x2).step3(x3);

I also admit my colleagues would kill me after doing this, and the jury would probably free them.