Friday, October 21, 2011

Clojure, a great way to learn OOP

Edit: There are two parts to this post. You can read the second part here.



Clojure is not sold as an OOP language, but it is a great way to learn OOP concepts. I had the (mis)fortune of learning OOP by using C++. I actually tried to learn OOP with Java and Python, but the problem was that the examples were so completely pointless that I was able to learn how to do OOP in those languages, but I never learned what OOP actually is. I had to learn that while using C++ to do things that made sense.

I will admit that I'm not a big fan of OOP. I always read that it reduces complexity, that it makes it easier to model your problem, but in most cases I didn't see that the benefits outweighed the overhead.

I've learned something while using Clojure. Nobody really cares about OOP. We care about what OOP does for us, and you can get the benefits of OOP without the BS of C++/Java-style OOP. Objects and polymorphism are convenient. Clojure has made me realize that there are alternative (and IMO far better) ways to get the benefits of OOP.

Here's a quote from Practical Clojure by Luke VanderHart and Stuart Sierra (which I highly recommend if you have no knowledge of Clojure): "The most important example is that maps can do 90 percent of what objects do in an object-oriented program. What real difference is there between named properties of an object and a key/value pair in a map? As languages like Javascript (where objects are implemented as maps) demonstrate, very little." It was nice to see in print something that I've been realizing for myself.

Maps and multimethods are not only very easy to understand, it's tough to learn them without also knowing why you'd want to use them. There's so much formal BS with C++/Java OOP that someone new to the language can't really capture everything. With Clojure you do a lot less and get the same benefits. It's also a natural part of programming in Clojure, even if your program is only 40 lines long, which means it's perfect for introductory programming classes. OOP is sold in the C++/Java world as a way to handle large-scale programs. There's no reason for that to be the case.

It seems strange to say it, but using Clojure has pushed me in the direction of OOP.

{I am not claiming to be an expert in OOP. Take my comments as coming from someone who is a little bit more than a beginner when it comes to OOP. I'd love to hear your thoughts if you disagree. I might learn something.}

3 comments:

Philip Schwarz said...

FYI, just after reading your post, I read the following in the Preface of Dean Wampler's "Functional Programming for Java Developers": Clojure minimises the use of OOP in favour of functional programming."

Philip

lmf said...

My post more or less defines OOP as a way to get polymorphism and data grouping. I don't think many OOP enthusiasts would agree with that definition.

Encapsulation is also a key part of Java/C++ OOP. Dean Wampler may have been referring to that.

ZackZone said...

CM303.com bandar taruhan agen judi bola online terpercaya dan terbesar di indonesia


http://www.scribd.com/doc/257156885/CM303-com-bandar-taruhan-agen-judi-bola-online-terpercaya-dan-terbesar-di-indonesia-docx
http://pack.li/Ejx
http://bit.ly/1M94Pwh