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?

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).
–
HolgerNov 29 '11 at 9:57

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.
–
user8Nov 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".
–
DJClayworthNov 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-logicNov 30 '11 at 16:21

18 Answers
18

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.

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.
–
KlaimNov 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.
–
mortNov 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.
–
RamhoundNov 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".
–
WernerCDNov 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.

I understand that the tl;dr notation worth a downvote, to some
–
IndependentNov 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 LeeNov 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 PapathanasiouNov 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?)
–
HLGEMNov 29 '11 at 20:46

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.)

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.

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

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 ... ;-):

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".

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.

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

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

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

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.

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.

+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 TypeNov 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.

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

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.

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:

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.)

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.

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

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

You have a client, it's always a good sign

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.

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:

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.

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.

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).

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!!