r/programming • • Feb 23 '12

Don't Distract New Programmers with OOP

http://prog21.dadgum.com/93.html
207 Upvotes

288 comments sorted by

View all comments

Show parent comments

7

u/Koreija Feb 23 '12

The real power of oop is the use of design patterns.

The funny thing is, that most "patterns" are only necessary in OOP, because the paradigm sucks and most OO languages get the defaults wrong. Everything OOP promised was proven wrong or is available in many other paradigms. Learn and understand multiple paradigms. If you don't like them it still makes you a better OO programmer.

10

u/munificent Feb 24 '12

Patterns are just paradigms applied to a language that doesn't support it natively. Closures and iterators are patterns in C++. Objects and namespaces are patterns in Scheme and C. Every language has patterns and many of them are direct language features in other languages.

-4

u/[deleted] Feb 24 '12

Well yes but many times a pattern can be implemented more easily in an oop language. Try using a strategy pattern in c , which would require function pointers - which are a pain to deal with, versus c++, where the compiler can do it for you with the v-table and interfaces.

But actually i hate iterators. I find them extemely unintuitive. I prefer C# 's foreach statement, iteration done right.

3

u/munificent Feb 24 '12

C#'s foreach is the iterator pattern baked into the language.

1

u/dnew Feb 24 '12

And "event" is the observer pattern, yes.

1

u/[deleted] Feb 24 '12

That was my point actually :) c++ iterators are the ugliest crap ever. Foreach is clean.

1

u/tragomaskhalos Feb 24 '12

C++0x improves this by providing a new "for" variant that hides the begin()/end() ugliness should you so desire.

1

u/pfultz2 Feb 24 '12 edited Feb 24 '12

Foreach is a little cleaner in C++ than C# because of C++s tuple library. So iterating a map of strings in C++03 is as simple as:

string key, value;
foreach(tie(key, value), string_map) { ... }

Whereas in C# it looks like this:

foreach (KeyValuePair<string, string> pair in string_map) { ... }

1

u/[deleted] Feb 24 '12

Can you point out some links? I'm not saying oop is the ultimate solution either - i just find design patterns intuitive. I grew up with strutctural and procedural programming. I actually dont use tons of objects myself. But id like more info on what other paradigms you are referring to. Thanks!

5

u/[deleted] Feb 24 '12

When you have first class functions, most of the patterns go away. Mostly because you have functions that can take a function as a parameter and return a function as a result. You can write functions at run time and you can make traversal algorithms that work on large classes of data structures.

You can do this in C++, but the code gets so obfuscated that patterns are easier. Doing such things in Java is an exercise in masochism. Doing it in Lisp, Haskell, F#, Agda and that family of languages is simple and elegant.

2

u/[deleted] Feb 24 '12

A coworker of mine said that haskell is a good starter language if i wanted to get into functional programming. Opinions?

3

u/[deleted] Feb 24 '12

Very good. It is well designed and there are no backdoors to sneak your old habits through. In Lisp, for instance, you can write Java-like code if you really want... not in Haskell; you are forced to do things the functional way. This is frustrating as all hell and you will feel retarded until you learn the functional way, but assuming you can deal with the frustration it's a good learning experience.

Don't expect to pick it up like you would Ruby. It's a completely different beast and you will feel like you are learning how to program all over again. Almost none of your knowledge will transfer. You can't even write x=x+1 in Haskell. You can't write for or while loops. There are no classes. And the type system is stronger than anything you are have likely ever worked with, but you don't have to declare any types. Very weird coming from Java or C++.

Check out "Learn you a Haskell" which is completely online I believe, and also a published book now.

1

u/sacundim Feb 24 '12

Of the more common functional languages, Haskell has the steepest learning curve. OTOH, it's also the more popular one these days.

When it comes to difficulty, I'd rank the more common functional languages in three tiers from simplest to most complex:

  1. Scheme, Common Lisp

  2. F#, O'Caml, SML

  3. Haskell

Learning an earlier language in the list makes it easier to learn a later one. The ML dialects in (2) are basically statically typed counterparts to the Lisp languages from (1); Haskell uses a similar type system to the languages in (2), but removes implicit side effecting and has a number of novel concepts that follow from that.

So there's two strategies:

  1. Tackle Scheme first and Haskell later, maybe with F# in between;

  2. Go off the deep end and just do Haskell.

0

u/redalastor Feb 24 '12

Opinions?

You'll bang your head in frustration a lot until you realize all of a sudden it makes sense. If you get discouraged, just come back another time until it 'clicks'.

1

u/Koreija Feb 24 '12

Can you point out some links?

Design Patterns in Haskell gives examples of the dual counterpart of some design patterns in FP.