Tuesday, September 06, 2011

Functional Programming? or Here We Go Again....


I am very interested in functional programming languages and their “no iteration, no mutable state, immutable objects only” way of doing things. Recursion, in Functional Programming (FP), is divine, and we should all aspire to divinity. Iteration is direct from the devil, as it makes for convoluted, difficult to maintain, crappy code. If you don't believe that, read some Java.

So what is the barrier to entry? Well, most of (if not all) the functional languages are considered “esoteric”. The most popular pure functional language seems to be Scheme, which is itself a stripped down, simplified descendant of Common Lisp. Every programmer I have talked to hates Lisp. Why? Simple: parentheses. Also, to a strongly typed mind (someone who is trained first on C#, Java, C++, C, Visual Basic, or whatnot) the fact that there are only two types in Lisp, the atom and the list, makes little to no sense.

Ruby comes close to integrating Functional Programming with Object Oriented Programming... but not really. It allows some functional programming concepts to slip in, but it doesn't really support “Functional Thought”.

Python? Not really. Plus both of these languages are really scripting languages, Not compiled, not optimized, not ready for the kind of speed one needs on the Enterprise level. (Not that Java is a speed demon, mind you.) No, if you want Enterprise speed (Warp 1, engage!) you have to settle for Java, or (shudder) suck at the teat of Microsoft. Or do you?

Enter Scala.

Scala is specifically designed to integrate functional programming and object oriented programming, while providing strongly typed variables, first class functions, and type inference. So what, you ask? Here's the really cool part. It runs on the Java JVM. That's right. It compiles to Java bytecode. Optimized. Ready for the enterprise. Java-like syntax. True integration of iterative and functional thought.

Using Scala, you can even leverage Java libraries. They completely inter-operate with each other. So all that code you Java developers have laying around... keep it. Import it. Leverage it.

As if that were not enough... All you C# junkies... you now have another functional programming option besides F#... Scala is available for the CLR as well. So you can compile Scala down to MSIL and let it fly on your Microsoft Web stack, or with your WPF application, or as, I don't know, a class library.

But I sense that you guys really wanna know “Why do I care about Functional Programming to begin with? What's wrong with good ole imperative OOP?” Well...

Nothing, really. FP just makes it easier to write thread safe applications. It makes code more maintainable by not allowing mutable objects. What I mean is, when you hand a function a set of parameters, you get the same response every time those parameters are given to the function. Functions are not allowed to alter the innards of Objects. They can take that object, and create a new object based on it with different values, but the original object is not altered. At best, it still exists. At worse, it is deleted and replaced by the altered clone.

Think about what that does for your unit testing. You get predictable results every time a function runs. If you have ever tried to write a test harness for complex imperative code, you know what a nightmare that is. Small changes in one part of an application can effect the state of objects on the other side of the app. And that can be really difficult to track down if it causes a test to crash.

Do some reading on it. Seriously. FP, once you understand it, will make you a better coder. Even if you choose not to use it, it's one of those things that will change the way you understand software.

3 comments:

Concerned said...

Can YOU write a language that is straight forward and simple?

Ronin said...

I could. But the programs that it would right would be constrained to be straight forward and simple. Using a simple language would take away high level tools from a developer.

Unknown said...

I just tried to leave you a detailed comment, only to have it get lost in cyberland. So let me try again, in a more succinct approach:

I know how brilliant you are, but I didn't know how great a writer you are until just now. I think you should seriously fish around for some free lance work.

Why not? Your writing is more crystalline, more personable and accessible than any of the crap yahoo pays people to write..
One of the things I said in the lost post was that you are able to convey the gist of your message to a reader who is an outsider to the world of programming. That is the hallmark of a really good writer.

Don't blow it off as "ehhh, it's just my Mom saying that". True enough, but I do think, as one who's been told stuff like I"m telling you here more than once, that I MIGHT be qualified to make such a statement with a bit of cred.

Think about it. It might even be fun. There's no reason (other than time availability) that I can see that you shouldn't pursue a sideline as an author. I'd love to see your name in print---even if it is only the 'Net.

I am going to try to post this one more time. If it loses it again, I'll just shoot the content to you in an e-mail.