views:

117

answers:

5

so that you can make your program concurrent easily in the future.

+2  A: 

I focus on making items Immutable. Immutable objects allow you to reason about multi-threaded code a lot easier than "thread safe" objects. The object has one visible state that can be passed between threads without any synchronization. It takes the thought out of multi-threaded programming.

If you're interested, I've published a lot of my work with immutable objects, in particular immutable collections on code gallery. The name of the project is RantPack. In the collection area I have

  • ImmutableCollection<T>
  • ImmutableMap<TKey,TValue>
  • ImmutableAvlTree<T>
  • ImmutableLinkedList<T>
  • ImmutableArray<T>
  • ImmutableStack<T>
  • ImmutableQueue<T>

There is an additional shim layer which (CollectionUtility) which will produce wrapper objects that implement BCL interfaces such as IList<T> and ICollection<T>. They can't fully implement the interfaces since they are immutable but all possible methods are implemented.

The source code (C#) including the unit testing is also available on the site.

JaredPar
A: 

Don't use member variables or global variables. Use the local stack of functions/methods. When a method uses only internally scoped variables and call parameters and returns all information using out/inout/reference parameters or return values, it is functional.

MZywitza
You didn't cover it all: […] don't use I/O functions or functions that returns random value etc. Basically, don't use functions with side effects.
Claymore
A: 

I program mainly in Java. I'm waiting patiently for the day where closures will be added to the language. But as I am still stuck on Java 1.4.2, even if they get added, that's not going to be for me for a long time !

That said, my main "functional" way of programming is making a lot of use of the "final" keyword. I try to have as many classes as possible completely immutable, and for the rest to have a clear distinction between what's transient and what's immutable.

Guillaume
A: 

Make everything asynchronic. Use immutable objects, messages, etc. Communicate via queues.

Yoni Roit
I can see how MQ help concurrency, but is that really functional ?
Guillaume
You're right, it's not functional, but it helps concurrency which is what I understand is the main goal here. Maybe I'm wrong and it isn't.
Yoni Roit
A: 

Here's a talk on rubyconf 2008 about the subject, it's mostly ruby centered, but several concepts remain valid.

http://rubyconf2008.confreaks.com/better-ruby-through-functional-programming-2.html

krusty.ar