Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I started programming when imperative programming languages such as C were virtually the only game in town for paid gigs. I'm not a computer scientist by training so I was only exposed to Assembler and Pascal in school, and not Lisp or Prolog.

Over the 1990s, Object-Oriented Programming (OOP) became more popular because one of the marketing memes for OOP was that complex programs could be composed of loosely coupled but well-defined, well-tested, cohesive, and reusable classes and objects. And in many cases that is quite true.

Once I learned object-oriented programming my C programs became better because I structured them more like classes and objects.

In the last few years (2008-2014) I have programmed in Ruby, an OOP language. However, Ruby has many functional programming (FP) features such as lambdas and procs, which enable a different style of programming using recursion, currying, lazy evaluation and the like. (Through ignorance I am at a loss to explain why these techniques are so great). Very recently, I have written code to use methods from the Ruby Enumerable library, such as map(), reduce(), and select(). Apparently this is a functional style of programming. I have found that using these methods significantly reduce code volume, and make my code easier to debug.

Upon reading more about FP, one of the marketing claims made by advocates is that FP enables developers to compose programs out of small well-defined, well-tested, and reusable functions, which leads to less buggy code, and low code volume.

QUESTIONS:

  1. Is the composition of complex program by using FP techniques contradictory to or complementary to composition of a complex program by using OOP techniques?
  2. In which situations is OOP more effective, and when is FP more effective?
  3. Is it possible to use both techniques in the same complex program?
  4. Do the techniques overlap or contradict each other?
share|improve this question

closed as too broad by Bart van Ingen Schenau, gnat, GlenH7, Doval, Telastyn Aug 25 '14 at 0:34

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs. If this question can be reworded to fit the rules in the help center, please edit the question.

    
I am going to vote to keep this closed. If you want an answer, I would leave out everything before "QUESTIONS" and split the rest into multiple questions: but search to see if each one has already been asked, and if so, either don't ask it again or change it to address any perceived deficiencies in the answers. –  Snowman Aug 26 '14 at 3:45

1 Answer 1

A1. I don’t understand what you’re asking. As a software designer, you’re free to do whatever you want: you can use FP, OOP, their combination, or anything else you think will do the job.

A2. OOP is usually good when your software has large and sophisticated state. For example, think about the modern interactive WYSIWYG word processor. Any single document is many collections of different objects: pages, paragraphs, words, letters, styles, images, fonts and their glyphs, and so on. The state of your algorithm is the document itself + a few more things, like cursor position, undo stack, and user preferences. It would be a nightmare in FP. The same applies to any sufficiently complex GUI application.

FP is usually good when complex algorithms only operate simple data, while the global state is read-only. If this is the case, FP can lead to better results than OOP due to the reasons you’ve described.

A3. I do it all the time in C#. When I have large and complex state (document object model, or network connection to a remote server with complex protocol, or just a rich GUI view model) I usually create a statefull class, which encapsulates what needs to be encapsulated and exposes what needs to be exposed. When I create logic that does something complex with that state, I mostly create static stateless classes, and heavily use FP.

A4. Again, I don’t think I've understood your question. If you want to, you can surely use both approaches in the single app. Neither one is a silver bullet, however.

share|improve this answer
1  
You can model just about everything that isn't the outside world (i.e. interactions with the file system, network, input/output streams) mutably or immutably so this seems like a red herring. Regardless of where you stand on the whole mutability/immutability argument, saying that FP only works with "simple data" is definitely wrong. Mainstream OOP languages tend to have poor support for complicated data structures, since they lack support for algebraic data types and pattern matching. In C-derived languages you're forced to represent everything as a struct. –  Doval Aug 24 '14 at 22:15
1  
“Mainstream OOP languages tend to have poor support for complicated data structures” — the support is outstanding. Look at HTML DOM (w3.org/TR/DOM-Level-2-HTML/html.html) for the example of complex data structure I’m talking about. “In C-derived languages you're forced to represent everything as a struct” — C++, C# and Java are all C-derived. Java doesn’t even have a concept of struct in the language. In C# structs are mostly used for interop with C and COM. And in C++, there’re plenty of libraries with template collections and smart pointers (STL, ATL). –  Soonts Aug 25 '14 at 0:12
    
A class in C#/Java is a struct whose members can be hidden. All user-defined types boil down to structs in those languages. In, say, OCaml, you can mix and match tagged unions, tuples, and structs/records. –  Doval Aug 25 '14 at 2:37
1  
“All user-defined types boil down to structs in those languages” — just like in OCaml. Moreover, in all languages, all data boils down to a set of bytes. However, such generalizations do not bring any value. “In, say, OCaml, you can mix and match tagged unions” — if you need to relax type checking, in C# you can use e.g. typeless “object” variables and/or dynamics. “tuples” — msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx –  Soonts Aug 25 '14 at 12:37
    
Moreover, in all languages, all data boils down to a set of bytes. Semantically, they're not bytes; they're records with visibility modifiers. if you need to relax type checking, in C# you can use e.g. typeless “object” variables and/or dynamics. Nobody said anything about relaxing type checking, so dynamic is right out. Anonymous objects can't be passed to/from from functions. Function argument lists aren't tuples, and no one lines using accessors called ItemN, so C#'s "tuple" (which is still a class/struct/record) is awkward to use. There's still no support for tagged unions. –  Doval Aug 25 '14 at 13:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.