views:

3494

answers:

9

What are the differences between these programming paradigms, and are they better suited to particular problems or do any use-cases favour one over the others?

Architecture examples appreciated!

+3  A: 

This isn't a full answer, but I write a bit about how "functional" affects/contrasts "OO" style (in the context of F#) here:

http://lorgonblog.spaces.live.com/blog/cns!701679AD17B6D310!511.entry

Brian
+2  A: 

You might consider reading this! There are many examples of when to use which ant what is the main differences, pros/cons etc

Cshift3iLike
+9  A: 

I think the available libraries, tools, examples, and communities completely trumps the paradigm these days. For example, ML (or whatever) might be the ultimate all-purpose programming language but if you can't get any good libraries for what you are doing you're screwed.

For example, if you're making a video game, there are more good code examples and SDKs in C++, so you're probably better off with that. For a small web application, there are some great Python, PHP, and Ruby frameworks that'll get you off and running very quickly. Java is a great choice for larger projects because of the compile-time checking and enterprise libraries and platforms.

It used to be the case that the standard libraries for different languages were pretty small and easily replicated - C, C++, Assembler, ML, LISP, etc.. came with the basics, but tended to chicken out when it came to standardizing on things like network communications, encryption, graphics, data file formats (including XML), even basic data structures like balanced trees and hashtables were left out!

Modern languages like Python, PHP, Ruby, and Java now come with a far more decent standard library and have many good third party libraries you can easily use, thanks in great part to their adoption of namespaces to keep libraries from colliding with one another, and garbage collection to standardize the memory management schemes of the libraries.

Python, ruby,... do not have "standard" libraries like C or LISP, since they are single implementation languages. Python is what Guido says, there is no standard. Any particular C or LISP (or whatever) implementation nowadays comes with a large set of libraries beyond the standard ones.
Dan Andreatta
+16  A: 

They don't really have pros and cons at all, IMO. They're simply different approaches to the same problems.

In a purely procedural style, data tends to be highly decoupled from the functions that operate on it.

In an object oriented style, data tends to carry with it a collection of functions.

In a functional style, data and functions tend toward having more in common with each other (as in Lisp and Scheme) while offering more flexibility in terms of how functions are actually used. Algorithms tend also to be defined in terms of recursion and composition rather than loops and iteration.

Of course, the language itself only influences which style is preferred. Even in a pure-functional language like Haskell, you can write in a procedural style (though that is highly discouraged), and even in a procedural language like C, you can program in an object-oriented style (such as in the GTK+ and EFL APIs).

greyfade
+1  A: 

In order to answer your question, we need two elements:

  1. Understanding of the characteristics of different architecture styles/patterns.
  2. Understanding of the characteristics of different programming paradigms.

A list of software architecture styles/pattern is shown on the software architecture article on Wikipeida. And you can research on them easily on the web.

In short and general, Procedural is good for a model that follows a procedure, OOP is good for design, and Functional is good for high level programming.

I think you should try reading the history on each paradigm and see why people create it and you can understand them easily.

After understanding them both, you can link the items of architecture styles/patterns to programming paradigms.

Timothy Chung
+4  A: 

For GUI I'd say that the Object-Oriented Paradigma is very well suited. The Window is an Object, the Textboxes are Objects, and the Okay-Button is one too. On the other Hand stuff like String Processing can be done with much less overhead and therefore more straightforward with simple procedural paradigma.

I don't think it is a question of the language neither. You can write funtional, procedural or object-oriented in almost any popular language, although it might be some additional effort in some.

panschk
Tempted to downvote for perpetuating the misconception that "Object = GUI widget", but I'll refrain. OOP works just as well for representing abstract concepts, such as "UserAccount" or "PendingSale", as for visible interface elements like "Window" and "Button".
Dave Sherohman
I wrote that a window can be an object. How do you draw the conclusion that every Object is a window from there?It was just an example. Of course OOP can be used just as well to model abstract entities and their relationship.Anyway, thanks for not downvoting. I dont have many points anyway:D
panschk
-1. OOP doesn't have anything to do with GUI's. Ideally, the best method for designing guis is using an external text file (e.g. HTML). While things like string-processing are actually much better done with objects. (think about strings in C)!!
hasen j
I don't know, maybe I'm just used to programming with objects to realise it. But how would you do some interactive stuff like changing the value in TextBox X when the value of TextBox Y has been altered without using objects? Okay, you could simply use global vars for everything...
panschk
Strings can be handled pretty easily in Perl and PHP without using OOP, too. But okay, I may not have choosen the beste example. What I wanted to say was that different paradigms are useful for different tasks.
panschk
String processing is awesomely done in perl (100x better than in Java, C++ or C#) yet the string functionality of the language is absolutely NOT object oriented. C's string handling was terrible, but then C is not the only procedural language (nor the best).
Joe Pineda
+2  A: 

I think that they are often not "versus", but you can combine them. I also think that oftentimes, the words you mention are just buzzwords. There are few people who actually know what "object-oriented" means, even if they are the fiercest evangelists of it.

Svante
+3  A: 

These paradigms don't have to be mutually exclusive. If you look at python, it supports functions and classes, but at the same time, everything is an object, including functions. You can mix and match functional/oop/procedural style all in one piece of code.

What I mean is, in functional languages (at least in Haskell, the only one I studied) there are no statements! functions are only allowed one expression inside them!! BUT, functions are first-class citizens, you can pass them around as parameters, along with a bunch of other abilities. They can do powerful things with few lines of code.

While in a procedural language like C, the only way you can pass functions around is by using function pointers, and that alone doesn't enable many powerful tasks.

In python, a function is a first-class citizen, but it can contain arbitrary number of statements. So you can have a function that contains procedural code, but you can pass it around just like functional languages.

Same goes for OOP. A language like Java doesn't allow you to write procedures/functions outside of a class. The only way to pass a function around is to wrap it in an object that implements that function, and then pass that object around.

In Python, you don't have this restriction.

hasen j
I believe you meant "these paradigms don't have to be mutually exclusive". The 3 of them *are* orthogonal in that ideally you could use one, 2 or the 3 of them in a single program (if your language allows it).
Joe Pineda
Yea I guess mutually exclusive is a better term for that! thanks
hasen j
+1  A: 

One of my friends is writing a graphics app using NVIDIA CUDA. Application fits in very nicely with OOP paradigm and the problem can be decomposed into modules neatly. However, to use CUDA you need to use C, which doesn't support inheritance. Therefore, you need to be clever.

a) You devise a clever system which will emulate inheritance to a certain extent. It can be done!

i) You can use a hook system, which expects every child C of parent P to have a certain override for function F. You can make children register their overrides, which will be stored and called when required.

ii) You can use struct memory alignment feature to cast children into parents.

This can be neat but it's not easy to come up with future-proof, reliable solution. You will spend lots of time designing the system and there is no guarantee that you won't run into problems half-way through the project. Implementing multiple inheritance is even harder, if not almost impossible.

b) You can use consistent naming policy and use divide and conquer approach to create a program. It won't have any inheritance but because your functions are small, easy-to-understand and consistently formatted you don't need it. The amount of code you need to write goes up, it's very hard to stay focused and not succumb to easy solutions (hacks). However, this ninja way of coding is the C way of coding. Staying in balance between low-level freedom and writing good code. Good way to achieve this is to write prototypes using a functional language. For example, Haskell is extremely good for prototyping algorithms.

I tend towards approach b. I wrote a possible solution using approach a, and I will be honest, it felt very unnatural using that code.

sneg
The first c++ copmpilers were nothing more than pre-processors that generated C code.As such, ALL features of c++ - including multiple inheritance, can be implemented using C.(emulating c++ exception handling in C would require some kind of platform support for exceptions, but c++ implementations require that too, so I dont think it makes the basic idea invalid).
Chris Becke