4 ms·
But it's easier to program multi-threaded applications when the code doesn't produce side-effects...
by dablya 18y ago
But it's easier to program multi-threaded applications when the code doesn't produce side-effects...
- gaius 18y agoIt ought to be possible to structure an OO program so side effects can't escape their logical boundaries. But the only people who can do that are a) functional programmers and b) given the choice aren't using Java...
- dablya 18y agoIt's most likely possible. However, I am not aware of any OO compiler/runtime that can enforce it. So, while it's possible, it's not easy to implement. With functional programming it's there by design.
- nostrademons 18y agoIt's actually pretty easy in Java. Create objects that initialize all their internal state in the constructor, and never mutate it in public methods. If a method must mutate state, have it return a new object. Several well-known Java gurus have advocated this, eg. Joshua Bloch. I worked with Ken Arnold for a bit, and a lot of the code he wrote was in this style. The problem is that you're usually using Java because you want access to all those Java libraries, and the vast majority of Java libraries do not use this style. So you get state leaking into your program even if your own code doesn't do it. (This is also why I'm less thrilled by Clojure than many other people are, even though I think it's a very well-designed language. The reason people are into Clojure is because it can use Java libraries and yet provides a mostly-functional language on top. But the problem isn't in Java-the-language, it's in Java libraries themselves. Unless you go rewrite the offending libraries - and this includes most of Swing, JSF, JFreeChart, the JavaBeans spec, and Calendar - you'll still run into problems. The only major libraries I've seen that use a relatively stateless style are Date and Java Collections Frameworks (both done by Josh Bloch, not surprisingly), JavaSpaces (done by Ken), and the basic String and Number classes.
- dablya 18y agoDefensive programming? You still don't have any guarantees that your code is in fact free of side effects. There isn't a way to easily test for it either. So, even if you don't use any libraries and write all your code in this style, you could have bugs that will be very hard to track down. Functional languages ensure, by design, that this doesn't happen.
- nostrademons 18y agoIt's not that hard to check for. Simply do a regexp search on your code for "variable =", where "variable" is one of the instance variables in the class, and ensure that all occurrences are in the constructor. Also need to avoid calling mutating methods of built-in classes - this is easy with Strings and Dates, but you need to be very careful to make copies of all your collections. I could write a simple script or Eclipse plugin that does it all for me. This, again, assumes you can trust your libraries. A single method that doesn't follow this convention will pollute anything that calls it. I've done things like this, in Python and to a lesser extent in Java. It works. It is pretty easy to slip up - I've had some bugs introduced because I forgot to copy a list - but it's at least a tractable problem. Gets easier if you use things like list comprehensions and slicing, which copy by default. Though Java's lack of support for closures can make this difficult.
- dablya 18y agoSure, it's possible. But this now sounds similar to arguing that you can do OO programming in C. Doing inheritance with function pointers and defining all "object" methods to take the data structure as the first parameter. I just think if you're going to code in a specific style, it's better to choose a language that better supports the style.
- nostrademons 18y agoYeah, it's a lot like doing OO programming in C. Not everyone gets to choose their language. When I do, it's usually something saner than Java.