r/AskProgramming • • Jun 03 '20

When is trying to do functional programming going too far? How do I push back?

So I am working on a large JavaScript project where the now lead developer has started heavily using functional programming throughout the code base, but to a level that makes it hard to work in.

I'll give you an example - essentially he will try to never write an if statement (e.g. will create a function like "throwIf"), never write a loop, never declare variables, chain everything into massive pipe and compose functions.

All of this is, in my opinion difficult to read, maintain and debug. Especially creating hundreds of small functions you are importing everywhere which could easily be done with standard language features.

Every PR he reviews will have dozens of comments where he is trying to convert people's code to a more functional style (but not fix anything, not catching errors).

How do I deal with this? How do I push back against this as a member of the team?

Side note: have you seen people do this with JavaScript before? I've used Ramda a lot and its no big deal, but taking it to this level seems extreme?

Edit: typos, added more spaces.

70 Upvotes

35 comments sorted by

67

u/[deleted] Jun 03 '20

an if statement (e.g. will create a function like "throwIf"), never write a loop, never declare variables,

None of this has anything to do with functional programming. Just shitty programming.

Recursion is looping in almost all examples of it. The reason functional programmers use map/reduce/etc is because it better captures the nature of the loop. A mapping function generates a new data structure like the current one. A reduce function is a loop that builds a completely new data structure out of an existing one, usually starting from some initial seed.

How do I deal with this? How do I push back against this as a member of the team?

Ask him to justify how these changes are functional in nature. Ask for articles recommending this style. Try to sound like you're interested in learning about this. To me it sounds like your lead developer isn't qualified for the position, so it's going to be difficult to make him understand that without being accusatory. Honestly, it may be easier to talk to your manager and possibly just asking to be put on a different team as you don't believe in your team lead.

5

u/TGR44 Jun 04 '20 edited Jun 04 '20

100% this.

Functional programming in JS often avoids if statements because they sometimes force you to unnecessarily declare variables as mutable (a problem Rust, Scala, Erlang, etc solve by having if as an expression) but they aren’t inherently counter to the style and a good programmer knows when to compromise principles for readability.

One reason higher order functions are useful in JS is if you’re writing Typescript, as it tends to make the type inference a lot cleaner but, again, pragmatism should be the order of the day.

2

u/Felicia_Svilling Jun 04 '20

Js does have the x ? y : z expression though, that you can use instead of if to keep the code functional.

-1

u/TalksInMaths Jun 04 '20

The other advantage of functions like map and reduce (as compared to, say, a for loop) is that they're lazy. They're not evaluated until the result is actually needed. Or, more precisely, the objects these functions return aren't evaluated until needed.

Note: This is how these functions work in python, which is the only language I've really used them in. I assume they behave similarly in js and other languages.

7

u/lifeeraser Jun 04 '20

Nope. Array.prototype.map is eager.

5

u/[deleted] Jun 04 '20

While not true of map/reduce in general, this is another point. Named functions can have conventions about their evaluation. A for loop is used for any number of things, which one is this usage for? Well you have to read it carefully to find out. See a map/reduce? You still have to read it but you can start with your knowledge of conventions and what the specific control structure does.

Functional programming is rarely more efficient than imperative, but the advantage is the ability to create new control structures to express constraints and conventions (with minimal semantic overhead once you understand higher order functions.) There are other ways of doing this, for instance macros and objects, but functions are simple and fairly desirable no matter what other language features you have.

1

u/visicalc_is_best Jun 04 '20

This is completely wrong. These functions are NOT lazy evaluated in JS.

18

u/Loves_Poetry Jun 03 '20

On the other end, I encountered someone that would create 2 classes that inherited from the same base class, add a static factory method somewhere and use polymorphism to execute either branch of an if-statement. If you want to overcomplicate things, OOP gives you just as many tools as functional programming

Some people are purists and they will take programming paradigms way too far. If they're just your average medior or senior dev, it's fine, since they can be ignored and overruled. However, if they become team leads, it can be a bit problematic

You need to have someone on your side first. Are there other devs that are having trouble with this person as well? If so, you can together address this with a manager and let them sort it out

0

u/[deleted] Jun 04 '20

[deleted]

2

u/merryMellody Jun 04 '20

My jaw fucking dropped oh my god. Way to button that one up, that would have driven me crazy to ignore.

35

u/feelings_arent_facts Jun 03 '20

when you hate oop so much that you can never have an object or inheritance

also, functional js is an oxymoron. it sounds like this dude is larp'ing as someone who programs in a true functional language like ocaml or haskell.

js is written to be flexible. to ignore how it was written is to go against the nature of the system.

15

u/gcross Jun 03 '20

Functional programming is an excellent servant but a terrible master.

10

u/knoam Jun 03 '20

Your team shares the codebase so you need to get everyone on the same page. Changing the style of code in a code review is wasteful. There needs to be a style guide that everyone agrees on and tries to conform to the first time they write their code. Changing the style of a substantial existing codebase is problematic because it makes for inconsistency. Get agreement on a policy/plan for how to get to a consistent style.

JavaScript isn't Haskell. You're going to run into trouble if you treat it like it is. Unidiomatic code is a form of technical debt because you can't take a programmer off the street and get them productive quickly. This has to be factored in.

Code Review isn't the best place to be teaching FP principles. Schedule some time for lessons instead.

You might be better off using a language like Reason rather than forcing JS to be something it's not.

FP advocates talk about structuring their code with a core of pure FP code wrapped in impure code to handle the messiness of the world. Maybe you need to formalize an architecture like that.

Debugging functional code is difficult because it's a procedural idea to step through code and watch it mutate. I don't know how to explain the alternative. Once you get into a more FP mindset debugging just isn't something you think to do.

6

u/[deleted] Jun 03 '20

create a function like "throwIf"

Throwing is not functional programming, though I suppose maybe you should be thankful that he's not changing the entire code base to propagate failures via Result return values (or maybe you should hope he does... It's quite nice.)

I think it would be reasonable to point out where he's making it hard to read the code, observe its operation, and modify it, with specific examples. The fact that he's the team lead probably makes it difficult, though — he'd probably conclude that you're a troublemaker.

4

u/aelytra Jun 03 '20

They've gone too far.

Time for a mutiny.

3

u/Icanteven______ Jun 03 '20

Ugh that situation fuckin sucks.

There's a balance between dogmatic ideology and practical programming and this lead of yours is unbalanced. Trying to convert an established team and an established codebase to an entirely different style is a terrible idea. it might be easier for him, but it's gonna be WAY harder for everyone else probably forever in that codebase. AKA it just isn't worth the investment and he is just dieing on a hill.

It's time to mutiny.

3

u/nutrecht Jun 04 '20

This is a "medior developer" thing: they find a new toy and really really want to use that toy everywhere. So suddenly they have this new hammer and literally everything is a nail.

All this shows is that your lead developer is just really inexperienced. I've seen this happen countless times.

2

u/Tacos314 Jun 04 '20

Honestly there is some good advice but rarely solve anything, a person like this believes they are right and will not change that mind set. Most likely they are the lead developer because they are the most vocal and probably spends a ton of time trying to get there ideas to work.

You can push back and/or ignore them, comply, speak with management or look for a new team. At the end of the day its up to you how much time and energy you want to spend combating this or go home at the end of the day and enjoy life while the lead spends the time trying to get the fictional programing to work.

1

u/MunsterPlop Jun 04 '20

Not sure if that was intentional, but "Fictional programming" summarizes things pretty well imo.

2

u/balefrost Jun 04 '20

To be fair, a throwIf function isn't entirely crazy. Kotlin has functions check and require that are both essentially throwIfFalse. You use them to specify preconditions, e.g.

require(size > 0) { "Empty lists are not permitted" }

I find that to be marginally easier to read than the equivalent:

if (size <= 0) {
    throw IllegalArgumentException("Empty lists are not permitted")
}

never write a loop

I mean with Array.prototype.forEach, you can avoid writing most explicit for loops. Perhaps for..of would be better since you can use break and continue and return within the loop body.

If you're complaining that the developer is using map and reduce and filter, then it really depends on context. I also prefer using those methods over using explicit loops because they better advertise the purpose of the loop. Certainly, they can be overused. And certainly, using an explicit loop to append items to an array can be more efficient than generating the intermediate collections. But there's nothing inherently wrong with these functions.


never declare variables

That's not a hallmark of functional programming. Virtually every programming language has some way to give a name to a value, including FP languages.

This has to be hyperbole. There have to be cases where a single value needs to be shared in two places. There are ways to achieve that without local variables, but I can't imagine that this developer is doing that. I'm sure he's creating local variables.


Especially creating hundreds of small functions you are importing everywhere which could easily be done with standard language features.

What do you mean? Is the developer re-implementing functions that exist in the standard library? Or are they writing new functions that are slightly different to those in the standard library?

Lots of small functions isn't inherently bad. In my limited experience with Clojure (more firmly in the FP camp than JavaScript), I had better luck with small, focused functions than with long, deeply-nested functions.

Every PR he reviews will have dozens of comments where he is trying to convert people's code to a more functional style

Yeah, that's tough. As a more experienced developer, it is sometimes your responsibility to nudge and prod the more junior developers to write better code. They have to balance that with getting work done, and that might mean working in a style that is more preferred by the whole team.

You have to talk about this. If the rest of the devs on the team are against a very functional approach, then the lead is probably out of line... or at the very least, isn't being an effective leader. But if you're the oddball and everybody else is perfectly happy with the functional approach, then maybe you're the one that needs to change.

There's just no solution other than to hash it out with conversation. As long as you have sufficient trust and respect for each other, and as long as you listen more than you tell, and if you agree on the overall goal (presumably to get software out the door), then you should be able to reach a resolution.


So when I'm trying to learn something completely new, I often do have to go "deep" with it. I have to overuse it in order to understand the situations where it doesn't work. And often, that kind of learning has to occur in a "live fire" situation. It may be that this developer has drunk the kool aid... or it may be that he's trying to learn by taking FP to the extreme.

Again, the only way to understand is to talk.

1

u/knoam Jun 06 '20

I agree with pretty much all of this but I especially want to give props to the idea that you have to overdo it to fully learn it. The cost of learning is something you just have to account for. You screw up the first couple of times but from then on, many times more you benefit from those lessons.

1

u/julianeone Jun 03 '20 edited Jun 03 '20

You could bring up something to your co-worker that surprises a lot of people: JavaScript's map is actually less efficient than forEach, in some tests. Because the usual justification is 'it's more efficient', and when you demonstrate it's not, that takes the wind out of a lot of sails.

https://dev.to/henryjw/array-map-much-slower-than-for-loop-57if

1

u/[deleted] Jun 04 '20

If they are the lead developer, and "owns" this project, then they should really be proving guidance up front, so that you aren't wasting time rewriting code to "fit" whatever ivory tower he's trying to climb.

The guys I work with just write code that gets the job done. Personally, I take the brutalist approach, i.e. try and keep the flow as straightforward and self-explanatory as possible. It's not like there's that much on the front end that needs to be optimized up the wazoo these days; unless you're doing heavy lifting on a back end, that is.

It sounds like he's possibly trying to do something fashionable, to the detriment of everything else. But if he's the lead, then he's supposed to set the tone, I suppose.

1

u/[deleted] Jun 04 '20

job security 101

-2

u/[deleted] Jun 03 '20

The answer is always to find out why.

I was listening to a talk from Elon Musk last night and he said something that a lot of engineers don't understand...

"If your implementation hits a roadblock you need to reevaluate your design."

You shouldn't be pushing back on Functional design. You should be asking why your team member feels functional design is better. Personally, functional design is better, only if it's strongly typed. If you have a compiler that can prove correctness, then that's amazing and will save developer time in the future. I'm against functional design in JavaScript because it adds another layer of complexity, for none of the provably correct gains you'd get in haskell. You team member, buy using Ramda, clearly feels their process is faster using functional, which is probably right.

You need to address the disease, and stop worrying about the symptom. Your design is currently garbage and you need a rewrite. If your team member needs to implement a new language to solve a problem, you should take it as a signal that the current language semantics do not solve the problem your team members are having. Your job as a senior engineer is to design a language to solve your problem, and let your engineers use the language to solve that problem. If you don't do this, you will lose engineers.

5

u/gcross Jun 03 '20

Personally, functional design is better, only if it's strongly typed.

Other advantages of functional design are that some things can be expressed more clearly as higher-order functions and that pure functions operating on immutable data are easier to reason about regardless of whether they are strictly typed because you don't have to worry about their side-effects (the classic example being launching missiles).

Your design is currently garbage and you need a rewrite.

I think that you have just drawn an incredibly strong conclusion from very little information.

2

u/[deleted] Jun 04 '20

pure functions operating on immutable data

I doubt they're actually working with persistent data structures, in this case.

3

u/brandondyer64 Jun 04 '20

That's not really what the word "immutable" means in FP though.

0

u/[deleted] Jun 04 '20

No, but they're kind of essential to efficient functional algorithms handling large data structures.

2

u/gcross Jun 04 '20

I agree, my point was just that the benefit of functional programming doesn't just revolve around strict types as the parent was claiming.

1

u/[deleted] Jun 04 '20

I think you should watch this. https://youtu.be/cIQ36Kt7UVg?t=308

I'm not saying functional javascript is the answer here. I'm saying an engineer taking their time to rewrite a system is a symptom that the system needs a rewrite.

As for "clearer" code, I totally agree with what you said, but what you said is the problem. High order functions are clearer, but the problem is Javascript is unityped. If you've actually written in Ramda, you're guaranteed to have javascript, especially JSON errors in your code. I have yet to see Javascript, even Typescript, properly handle effects and datastructures well, so to me, functional style in Javascript offers nothing more than aesthetic improvements. You spend more time piecing together "good looking code", just to have a run-time "Property not found" error because your production database assumptions were wrong. I frankly have given up writing "Functional" code in Javascript. It is a lot of work to solve a problem that isn't there in the first place. JS code doesn't break because it "looks bad". Software engineering teams don't run into large scale technical problems because their code "looks bad". Not to mention dealing with errors in Ramda monads are a nightmare because the stack trace swallows information about the specific errors, and spits out, sometimes impossible to debug problems. If JS was strongly typed, you'd be forced to write failure cases for all possible scenarios or the compiler will complain. In that environment, Functional makes far more sense. You put in the work and you actually get more than aesthetic improvement, but real technical improvements.

1

u/[deleted] Jun 04 '20

I think Musk was probably high during that interview.

-2

u/DecisiveVictory Jun 04 '20

he will try to never write an if statement (will create a function like "throwIf")

That's not FP. Throwing exceptions is not FP. Him doing this is a huge red flag.

The part where he is partially right is that in JS, if-else is a statement, not an expression.

Suggest he uses conditional (ternary) operator instead, it's FP.

never write a loop

That's fine, you don't need loops (except for performance). Loops are bad.

never declare variables

Only immutable values? That's great. You don't need variables.

chain everything into massive pipe and compose functions.

That's nice except it's JS. These constructs end up being clumsy in JS, as far as my experience goes.

Why not switch to Scala.js or PureScript instead then?

How do I deal with this? How do I push back against this as a member of the team?

You do two things (either, or both):

a. Learn Haskell, Functional Scala, etc. to a sufficient level that you can point out where he is misapplying FP,

b. Find a new job.

0

u/[deleted] Jun 04 '20

[deleted]

5

u/caboosetp Jun 04 '20

Functional programming is when you would take a group of statements that you would need to either repeat or call elsewhere and create a function from that

Bruh, that's writing functions, not functional programming.

2

u/Ran4 Jun 04 '20 edited Jun 04 '20

Functional programming is when you would take a group of statements that you would need to either repeat or call elsewhere and create a function from that.

That's using functions. Functional programming means a lot more than that.

There isn't really one solid definition, but functional programming concepts typically include:

  • Immutability of data

  • Composition over inheritence

  • Functions that use "dead" data as opposed to objects with methods

  • Static typing (though not all functional programming languages are statically typed! Lisp for example, is functional but typically dynamically typed)

  • A different set of higher-level abstractions (unlike OOP, which has "OOP patterns"). Things like monads and sum types (using Result/Maybe/Option instead of null or throwing exceptions for example).

  • Declarative programming

  • Managing side effects carefully (such as raising exceptions or in extreme cases like Haskell, any form of IO).

1

u/[deleted] Jun 04 '20

Like I said, Layman’s Terms.