Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a program to simulate the activity of ants in a grid (PDF). The ant can move around, pick up things and drop things.

The problem is while the action of the ants and the positions of each ant can be tracked by class attributes easily (and we can easily create many instances of such ants) my client said that since he has a background in functional programming he would like the simulation to be made using functional programming.

To be clear, the original words from the client is "no class" only, but not "functional programming". So I assume he doesn't mean functional programming and I can do it imperatively. Additionally, I have no prior experience in functional programming.

However, I think it's beneficial to focus on this question being particularly about a functional programming requirement than just simply "do it imperatively."

How would you handle this situation? Would you try to persuade your client that using object-oriented programming is much cleaner, try to follow what he required and give him poor-quality code, or do something else?

share|improve this question

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

9  
One thing that might change his mind, is if the cost of you doing so is higher (if you are more proficient in OO languages than a functional programming language). – Holger Nov 29 '11 at 9:57
    
Might be interesting to compare Rich Hickey's ant simulation code (gist.github.com/1093917) - in Clojure so basically functional although the simulation was mainly designed to demonstrate the use of STM. – mikera Nov 29 '11 at 14:12
13  
Commenters: Do not leave an answer here in the comments. Write your own answer. Comments are not a venue for discussing various possible answers to the question: either put forth your suggestion as an answer or take it to chat to flesh it out first. – user8 Nov 29 '11 at 17:32
6  
Just wanting to make sure you saw N3dst4's point that "functional programming" is a particular programming discipline. Programming that is not object oriented is usually described as "procedural programming". – DJClayworth Nov 29 '11 at 17:58
1  
Why do you think that object-oriented implementation would be "much cleaner"? Most likely it'll be much less readable than a proper functional solution. – SK-logic Nov 30 '11 at 16:21

18 Answers 18

up vote 201 down vote accepted

Object-oriented code is not by definition cleaner, and conversely non-OO code is not by definition crappy. While there does seem to be a rather obvious object-oriented mapping to this particular problem, I would suggest that you try the functional programming approach anyway. Give it your best shot, try to solve the problem in the best functional programming style that you can muster, and you might just learn something you didn't expect.

share|improve this answer
83  
+1 for "you might just learn something you didn't expect"! – Kenneth Nov 29 '11 at 8:21
2  
Also, Data-Oriented programming does allow excellent performances because is cache friendly and is better implemented in sets of functions processing blocks of data. It seems perfect for the problem you're working on at. I don't know how it applies to functional programming but I'm guessing it helps more than it hurts. – Klaim Nov 29 '11 at 11:15
7  
+1 for "you might just learn something you didn't expect"!, BUT: If the OP doesn't have much experience in functional programming and the client expects a good and cheap solution, it would be rather risky to dive into a new way of solving problems. – mort Nov 29 '11 at 11:18
3  
@mort - The client in this case wants something specfic, sounds like they know enough to actually to know what they want, so if the person they hired cannot do it thats their problem. I guess what I am saying is the fact the client wants something "good and cheap" and say they hired the wrong person who cannot provide that, its the client's fault, not the author's fault he doesn't know how to provide a cheap and good functional programming solution to this problem. One of those its worth the author trying to provide what the client wants, since there no technical reason not to do it. – Ramhound Nov 29 '11 at 11:55
2  
Nowhere in the question did the OP say the words "good and cheap". The desire could be "Fast and good" (out of the three: fast, good, cheap). All of that is irrelevant, without OP guidance, since the "Specifications" say "Use Functional Programming". – WernerCD Nov 29 '11 at 13:42

You mention that the client used to program in a functional language, maybe he has a reason that he requires you to write the code in a functional style. You should ask him why.

Maybe he intends to keep the code and maintain it himself later.

Moreover, I don't think the two choices are either do it OO-style or write crappy code like you mentioned. Sure writing functional code in an example like this could be harder, especially if you only have experience in object oriented languages, but if the client is willing to wait a bit longer for you to get up to speed with the functional style then it wouldn't hurt to ask him that.

I would ask him why he wants the code in functional style and if time isn't so much of an issue I would ask for some extra days to get up to speed with functional programming. (hurray for getting paid to learn!)

If all else fails explain that it would take you much less time to do it in a OO style.

share|improve this answer
    
@downvoter would you give some feedback? – Thanos Papathanasiou Nov 29 '11 at 8:35
3  
I understand that the tl;dr notation worth a downvote, to some – Independent Nov 29 '11 at 17:04
1  
Is there anything in the FAQ or help pages anywhere admonishing the use of "tl;dr"? Or is it just some rogue users who don't like it? Seems to me that adding a succinct summary of an answer can only be a good thing, so I can't imagine why this would be considered worth a downvote. – Ben Lee Nov 29 '11 at 18:23
1  
@Bratch I thought the tl;dr notation was for the user trying to read my answer. Meaning that even if you skip all the rest, if you just read this then you get the gist of the answer. What do you mean with what you are saying? – Thanos Papathanasiou Nov 29 '11 at 19:06
1  
SOme of us old people have no idea what tl;dr means (I did look it up but why would anyone use such cryptic nonsense?) – HLGEM Nov 29 '11 at 20:46

Are you aware that functional programming doesn't just mean "programming without classes"?

See the Wikipedia article about it for the full schtick, but in short...

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state.

Functional programming is a programming paradigm, just like OO is a programming paradigm.

If your background is in OO, then I can see how you'd want all your ants to be objects. On the other hand, if you're simulating an ant farm with millions of ants, OO and message-passing may become inefficient.

Luckily for you, Python has excellent tools for functional programming (the most important one being that functions are first-class objects.)

Python functional programming HOWTO

share|improve this answer
    
+1 for poinging this out. It really should be clarified in the question. – Bratch Nov 30 '11 at 0:53
    
Did you know that you can have languages that are both OO and functional? Those are two organizing principles that are actually largely orthogonal to each other. Admittedly a lot of OO languages are also structured imperative languages, but there's no theoretical basis for coupling those strongly. – Donal Fellows Nov 30 '11 at 20:57
    
@DonalFellows Absolutely, it's a good point that the two are not at all mutually exclusive. Python (because the question was originally tagged Python) is imperative, object oriented, and functional, depending where you stand when you look at it. And elsewhere on this page someone mentions OCaml, which is OO and functional. – N3dst4 Nov 30 '11 at 23:19

Explain to your client that if he wants functional programming, he should hire someone who specializes in that. Functional programming is very different from OOP, and you will be mistaken if you think you can easily pick it up and deliver a complex solution of high quality.

share|improve this answer
    
Agree. It is just common sense applied. – Mister Smith Nov 29 '11 at 12:49
1  
The problem is, from a business perspective, it's not always easy to admit your lack of knowledge to the customer ("You should hire someone familiar with functional programming instead"). It's easier to claim OOP is better, simply because you're familiar with it. Less honest, but easier. – Andres F. Nov 29 '11 at 13:52
    
@Andres F: Dealing with a new language (and paradigm) is not easy at all. Soon or later the client has to reconsider the problem. Better soon than later. – Mister Smith Nov 29 '11 at 17:29
4  
@Mister Smith: I fully agree with you. I'm just saying this kind of honesty from the provider (i.e. the programmer) is not always forthcoming. Essentially you are telling the customer to hire someone else, which makes all the sense in the world, but is painful nonetheless. – Andres F. Nov 29 '11 at 18:00

There is a common misconception that "OO" is completely contrary to "functional". These things can go hand-in-hand very well. In your example, I guess an "Ant" can be modeled well as an abstract data type, which can be straight forward implemented using classes and objects. Transitions between "Ant states" can be naturally modeled using functions, which will lead you to a functional approach as far as your "Ant" class is an immutable type.

And be aware that "objects" can be interchanged by the functional concept of a closure, since objects are the poor man's closures are the poor man's objects are the ... ;-):

http://stackoverflow.com/questions/2497801/closures-are-poor-mans-objects-and-vice-versa-what-does-this-mean

http://stackoverflow.com/questions/501023/closures-and-objects

share|improve this answer
    
+1 Functional programming and OOP are orthogonal concepts. Look at OCaml, Scala, Clojure, python for languages that can handle both. – rds Nov 29 '11 at 18:07
    
Those two links are worth an upvote alone... – Wayne Werner Nov 29 '11 at 19:20

After talking it over with the client, if he still wants it his way, you either do a professional job, or if you can't then you either don't take the contract or find some way out of it.

Producing "crappy code" just because you disagree would be highly unprofessional.

share|improve this answer
  1. Why are we all assuming the client knows the difference between functional and imperative programming? A lot of people don't know the names or specifics of non OO programming paradigms and will readily interchange terms like "procedural" "imperative" and "functional".

  2. Do not walk how others tell you to walk unless you believe you should walk that way. Therefore if you don't believe functional programming is suitable then don't set yourself up to fail or take on a project half-heartedly.

  3. If the client writes the spec then implement the spec, otherwise you write the spec and implement your spec.

  4. The best strategy to influence clients' decisions is to make the undesirable option significantly more expensive. It works everytime.

  5. If you are the expert (relative to the client), then you should be able to persuade them.

  6. In order to really know whether the client has a valid point, you need to gain more experience with functional programming, either so you can shoot it down with confidence or realise that your bias towards OO is due to your inexperience.

  7. Why not do it both ways then let the client see both implementations and decide which is easier to maintain. Just factor all this into your project estimates so you can enjoy the learning curve while you get paid.

share|improve this answer
    
+1 for "Why are we all assuming the client knows the difference between functional and imperative programming?". The client could mean something like "I don't want it to be repetetive, so break everything down into functions", which is common-sense to developers. A client might not think it's common-sense, so he's telling you. – AlexWebr Nov 29 '11 at 17:44
1  
+1, in reality client has no idea what Functional programming is an is either driven by latest buzz words or because they did it twenty years ago and still think of themselves as technical. – Anonymous Type Nov 30 '11 at 3:00

Before bothering any further, I'd make sure that you both are talking about the same thing. You could ask him for when software is 'object-oriented' to him. Sine he said that it's not his main expertise himself, it may be that he has some skewed idea.

For instance, many people could consider

class C {
public:
  C();
  void f( int );
  void g();
};

to be a classical object-oriented approach, but

struct C;
void construct( C ** );
void f( C *obj, int arg);
void g( C *obj );

not (even though both are equally object-oriented as far as the classical "data together with the functions which operate on it" definition goes).

share|improve this answer
    
+1 for occams razor – mizipzor Nov 29 '11 at 16:56
2  
Why argue about the precise meaning of OOP? It would be better to ask why the customer thinks his simulation is better suited to functional programming. The customer may be right... I seriously doubt by "functional" he meant your second C example, or that he was confusing "functional" with "imperative". – Andres F. Nov 29 '11 at 18:03
    
@Andres F.: I didn't claim that by 'functional' he meant my second C example. I was just pointing out that some people consider it to be OOP while others wouldn't. So before starting an argument, it would be good to avoid any misunderstandings. Maybe there is no disagreement in the first place. Maybe the boss, since he said that he's not familiar iwth OOP himself, assumes that OOP has certain properties (just like the OP apparently assumed functional programming had certain properties). – Frerich Raabe Nov 30 '11 at 4:07
    
Strictly, I wouldn't consider the latter to be OOP as the method call/message dispatch isn't routed via the object. That's a key feature of OOP. – Donal Fellows Nov 30 '11 at 21:00

Would you try to persuade your client that using object-oriented programming is much cleaner?

I think you need to educate yourself more on programming paradigms. Object-Oriented programmed code is not necessarily cleaner, and in fact, it is not universally applicable. Also, a good object-oriented coder knows how to do good work using a procedural/modular (With Functional and Declarative paradigms, it is a bit harder, but it should not be overly difficult for a good programmer to arrive - via reading and deduction - to an acceptable FP/Declarative solution.)

You almost never cannot, I repeat, you almost cannot have a good understanding of when and how to use Object Orientation without having a good understanding of procedural and modular programming. OO is a lot more than just declaring classes and inheritance hierarchies.

Or would you try to follow what he required and give him crappy code?

If you cannot write good code procedurally, I doubt you can write good code in an object-oriented manner. Period. I'm not trying to judge here, but this has to be asserted.

Object Orientation is an extension of procedural and modular programming. Object-Orientation simply gives you tools that, when used appropriately, give you better mechanisms with which to deal encapsulation, coupling, cohesion and code-reuse/extensibility issues.

But all those issues are not inherent and unique to OO. They exist in procedural/modular code (and in other paradigms for that matter.) This is the type of complexity issues that, at its very core, is paradigm-independent. If you cannot handle them without OO glue, then it's unlikely you can handle them with it.

=========

Going back to your original question, of whether to try to persuade your client. It depends. As poster Sean McMillan said, if the client is simply trying to micro-manage the development effort for some agenda (read, office politics), walk away. People who do that sabotage projects to blame someone else, or push a particular agenda. You do not want to be involved in that.

OTH, there might be other reasons for such a requirement. Many embedded shops, for right or wrong, choose to put a lot of constrains on what you can do with C++ (no virtual methods, no exceptions, for instance.) Some times it is done in a knee-jerking fashion. Some other times, there are valid technical reasons for doing so.

So you need to understand why the client wants to avoid OO code. And if you can surmise that there is no political agenda (no red flags), then you should do the professional thing to do, which is simply do the code procedurally/modular, and do a good job at it.

There is really no excuse for delivering crappy code, independently of the programming paradigm. If you can't produce acceptable code with one paradigm, you most certainly cannot produce acceptable code in general.

share|improve this answer

You are mixing up data-structures and object-oriented-programming (a common confusion in this OO infested world)

If all you need to do is "track data attributes" in a data structure and modify them then almost any language made after the 70s will have good suppot for it, OO or not.

The things that are easier to do in OO are roughfly

  • Class based inheritance (its easy to create a new class that pretends to be an old one)
  • Class based polymorphism (its easy to add new kinds of ant to the simulation afterwards)

If you don't have a pressing need of one of these then basically any programming paradigm should solve this without too many problems.

share|improve this answer
    
I would add that any functional programming language with support for polymorphism should make it fairly easy to write an object oriented or pseudo-object oriented style that allows you to easily add new types of ant. – Marcin Nov 29 '11 at 16:13
    
@Marcin: its true that modern FP languages are quite powerful. I just really wanted to point out the distingction between data-structurs/ADTs and OO – hugomg Nov 29 '11 at 16:18
    
But OO is really just ADTs plus object-controlled method dispatch. Everything else builds on top of that. (Well, often the only control by the object over dispatch is by the object's type, but that's a refinement.) – Donal Fellows Nov 30 '11 at 21:03

my client said that since he has a background in functional programming he would like the simulation to be made using functional programming.

(This is yet another example of a social issue being mistaken for a technical/design issue.)

There are two possibilities here:

  1. Your client is expecting to be able to take the code you have written and adabpt or maintain it himself after you are done writing it. If so, you should know a lot more about the "house style" -- functional vs OO is only the tip of the iceberg. You will either need to limit yourself to a style of programming that your client understands, or you will need to educate your client in the styles you prefer. (Once, I was asked to build a web app with CGI, without using templating or libraries because the client might want to make changes.)

  2. Your client is trying to control the development because of some agenda. This is a bag full of crazy that you want nothing to do with. If you are truly providing a "turnkey" piece of software, the client shouldn't care if it's made of hamsters running on wheels, as long as it does the job reliably. Allowing yourself to be micromanaged this way is just asking for pain.

It's up to you to decide which situation you're in, and act accordingly.

share|improve this answer

Umm... am I the only one here thinking "this is obviously a test job/assignment"?.

Firstly - the assignment itself is kind of "academic" in nature (simulate an aspect of behavior of ants).

Secondly - a request to implement requirements using (or avoiding) a very specific programming paradigm smells of the "client" who can read code and make such assertions.

If this is the case, you better do what is required of you - it will be rather nice learning experience and if you can do it, you'll learn a lot in the process...

If this is not the case, you should indeed ask yourself and/or the customer for the reasoning about the assignment. If the reasoning is solid, then do it - you'll learn and you'll be better as a programmer for the experience. Who knows - you might even learn to like functional style over OO.

If the explanation is lacking, then all bets are off.. I can not recommend you what to do.

You might want to try regardless to implement the requirements in functional language/style or you might politely turn down the offer if you sense something fishy.

The main thing is - once you understand the motivation behind the requirements, the proper course of action becomes evident.

EDIT: After taking a diagonal look at the referenced PDF, the algorithms described there surely seem like a good fit for functional style rather than OO

share|improve this answer

There are several good aspects in their request to use functional programming:

  1. You have a client, it's always a good sign
  2. The client expects you to be very good at what you're doing. This is why they ask functional programming. So your sales organisation is doing a good job or you're asking very high price for your services.
  3. Client organisation has some people who know functional programming is a good thing and will be big in the future

But there are some alarming signs also:

  1. You don't seem to be prepared to implement it in functional programming. You're already slightly outdated in your skills and cannot keep up with the changes.
  2. Or the client is expecting you to be better programmer than you really are. This means you might need to downgrade their requirements until you can do it properly.
share|improve this answer
    
-1 for the implicit assumption that FP is better than OOP. – Russell Borogove Nov 29 '11 at 18:36
    
@tp1 1) You assume the client is technically smarter than the programmer, which isn't the case since client is just that the client. 2) FP is older than OOP and while it is getting a lot of press lately, there is nothing wrong with OOP and you don't need to forget to use FP 3) The worse part of this is that assume FP is better and using FP makes you a better programmer, is is only true in a case by case and in this case that seems not to be true. – Joe Tyman Nov 29 '11 at 20:10
    
@Joe Tyman: Well, there has to be assumption that people are not stupid, or otherwise clients are gone in an instant. I wasn't trying to say oop is bad or worse, but instead that assuming functional might be unreasonable requirement in this situation - maybe the client doesn't know the skills of the programmers, or worse, trying to make programmers switch their tech. – tp1 Nov 29 '11 at 20:34
    
@Joe Tyman: Also, the worst case scenario I had in mind was that clients really need people who can do some advanced functional programming like category theory, and they're trying to find a team who can do it - this is why the request for them could be unreasonable. – tp1 Nov 29 '11 at 20:49

Seconding the above responses that maybe OO isn't the best solution in which case the client may have a point.

Take a look at the AI Challenge which models some of the behaviour detailed in the question here http://aichallenge.org and then take a look at the variety of starter packages - http://aichallenge.org/starter_packages.php/ most are languages I wouldn't place within the OO paradigm.

share|improve this answer

You've wrote nothing about programming language, which is propably the most important thing there. The difference between OOP and functional programming is not only the way the code is organized but the language itself. When high concurrency is the case, functional language Erlang is in usage, and it has very high advantage over e.g. Java (it is used e.g. by Facebook chat). OOP solution could simply fail because of performance issues.

The client here is university, so language isn't only performance/configuration issue, the code could be also used for didactic work with students or own research. So 'persuading' client to choose other paradigm is in my opinion not applicable here. This is either you can deal with the task or you can not (and so, you should not take that project).

share|improve this answer

Client says "jump", your answer is: __ _?

For me, I would make an attempt to persuade if it made sense (new project), but I also have a client with a 10 yr old VB6 app that I do upgrades for occasionally... not going to convince them to

share|improve this answer
    
technically though the VB6 app is fine, it's almost OO, and if it works fine on the current OS why "upgrade" to .NET. That just doesn't make sense unless you want to take advantage of new functionality. – Anonymous Type Nov 30 '11 at 3:02
    
Yes but have you tried using vb6 lately? it is soooo painful ;) – boomhauer Nov 30 '11 at 15:26
    
Yup. We use plenty of it for maintaining existing apps that haven't yet been given budget for an update to java or .net. It is painful (compared to a modern IDE) but it's also relative. Like any language (including scripts) once your good at it your definition of pain becomes alot more subjective. – Anonymous Type Dec 4 '11 at 22:32

'Cross Examine' your client a bit (in a non-confrontational manner):

Does client actually know the difference between OOP and functional programming? Are client's concerns/requests legitimate?

If 'Yes': If you're qualified, do what they want and take your money. If you're not qualified, tell them so and let them decide what to do.

Else: hi-tail it outta there!

share|improve this answer
double dist(Point p1, Point p2) 
{
  //return distance
}

This function is functional as long as it doesn't read/write anything outside the function. If the function touched a class variable it would no longer be "functional". The advantage of functional style is there are no more bugs from changing or invalid state. A large amount of functions are just mathematical formulas. That's functional programming in a nutshell.

BTW you can combine a functional style within an object based or oriented design. For example the two "Point" variables are objects with state. And the function is still functional! Yay!!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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