323 votes

Am I too 'clever' to be readable by Jr. devs? Too much functional programming in my JS?

In your code, you have made multiple changes: destructuring assignment to access fields in the pages is a good change. extracting the parseFoo() functions etc. is a possibly good change. introducing ...
user avatar
  • 123k
226 votes

Am I too 'clever' to be readable by Jr. devs? Too much functional programming in my JS?

If you are in doubt, it probably is too clever! The second example introduces accidental complexity with expressions like foo ? parseFoo(foo) : x => x, and overall the code is more complex which ...
user avatar
  • 54.6k
175 votes

Why do Trampolines work?

Kevin succinctly points out how this particular code snippet works (along with why it's quite incomprehensible), but I wanted to add some information about how trampolines in general work. Without ...
user avatar
  • 4,389
149 votes

How functional programming achieves "No runtime exceptions"

How does a Function Programming, such as Elm, achieve "No runtime exceptions"? That's easy. You simply don't write functions that fail. That might sound simplistic, but that's the gist of ...
user avatar
147 votes
Accepted

What is the "Free Monad + Interpreter" pattern?

The actual pattern is actually significantly more general than just data access. It's a lightweight way of creating a domain-specific language that gives you an AST, and then having one or more ...
user avatar
105 votes

Should functions that take functions as parameters, also take parameters to those functions as parameters?

There is absolutely no reason to pass a function, and its parameters, only to then call it with those parameters. In fact, in your case you have no reason to pass a function at all. The caller might ...
user avatar
  • 3,914
103 votes
Accepted

Is functional programming faster in multithreading because I write things differently or because things are compiled differently?

The reason people say functional languages are better for parallel processing is due to the fact that they usually avoid mutable state. Mutable state is the "root of all evil" in the context of ...
user avatar
102 votes
Accepted

Return considered harmful? Can code be functional without it?

If a function doesn't have any side effects and it doesn't return anything, then the function is useless. It is as simple as that. But I guess you can use some cheats if you want to follow the letter ...
user avatar
  • 54.6k
92 votes
Accepted

Why do Trampolines work?

The reason your brain is rebelling against the function loopy() is that it is of an inconsistent type: function loopy(x){ if (x<10000000){ return function(){ // On this line it ...
user avatar
  • 2,598
86 votes
Accepted

What do you call a function where the same input will always return the same output, but also has side effects?

I'm not sure about universal definitions of purity, but from the point of view of Haskell (a language where programmers tend to care about things such as purity and referential transparency), only the ...
user avatar
  • 5,119
77 votes
Accepted

What is the name of a function that takes no argument and returns nothing?

Java's choice to do it that way with a separate name for every arity was not exactly worth emulating. However, if you must for the sake of consistency, or if you're writing very generic library code, ...
user avatar
72 votes
Accepted

What is it about functional programming that makes it inherently adapted to parallel execution?

The main reason is that referential transparency (and even more so laziness) abstracts over the execution order. This makes it trivial to parallelize evaluation. For example, if both a, b, and || are ...
user avatar
63 votes

"Easy to reason about" - what does that mean?

To my mind, the phrase "easy to reason about", refers to code that is easy to "execute in your head". When looking at a piece of code, if it is short, clearly written, with good names and minimal ...
user avatar
  • 38.1k
62 votes
Accepted

What is referential transparency?

Referential transparency, referred to a function, indicates that you can determine the result of applying that function only by looking at the values of its arguments. You can write referentially ...
user avatar
  • 19.1k
60 votes

Do you need to think about encapsulation if you can ensure immutability?

I hate how encapsulation is always framed as preventing unauthorized access. If this were the best way to think of it, immutability would indeed eliminate most of the need for encapsulation. In fact, ...
user avatar
59 votes
Accepted

Why do some functional programming languages use a space for function application?

which seems to be the more mathematical way functional languages are inspired by lambda calculus. In this field, parentheses are not used for function application. I also think that the latter ...
user avatar
  • 7,822
57 votes
Accepted

Why are lists the data structure of choice in functional languages?

Because lists are simpler than trees. (You can see this trivially by the fact that a list is a degenerate tree, where every node has only a single child.) The cons list is the simplest possible ...
user avatar
52 votes

Why would a program use a closure?

By way of explanation, I'm going to borrow some code from this excellent blog post about closures. It's JavaScript, but that's the language most blog posts that talk about closures use, because ...
user avatar
52 votes
Accepted

Do you need to think about encapsulation if you can ensure immutability?

The question Casting your question to real life: Is it okay for your doctor to post your private medical records publicly to Facebook, provided no one (other than you) is able to change it? Is it ...
user avatar
  • 37.2k
50 votes

"Easy to reason about" - what does that mean?

A mechanism or piece of code is easy to reason about when you need to take few things into account to predict what it will do, and the things you do need to take into account are easily available. ...
user avatar
48 votes
Accepted

Why are discriminate unions associated with functional programming?

Discriminated unions really shines in conjunction with pattern-matching, where you select different behavior depending on the cases. But this pattern is fundamentally antithetical to pure OO ...
user avatar
  • 54.6k
47 votes

A language based on limiting amount of arguments passed to functions

There are lots of languages which already work this way, e.g. Haskell. In Haskell, every function takes exactly one argument and returns exactly one value. It is always possible to replace a function ...
user avatar
45 votes

What are the functional equivalents of imperative break statements and other loop checks?

The closest equivalent to looping over an array in most functional languages is a fold function, i.e. a function that calls a user-specified function for each value of the array, passing an ...
user avatar
  • 17.3k
45 votes

Are immutable objects important only in multi-threaded applications and if so, how are shared immutable objects useful?

No, immutable objects are quite useful in general. The first and most basic reason is that concurrency in a system doesn't require a multi-threaded application. Making say... a row in a database ...
user avatar
  • 107k
43 votes
Accepted

Is Functional Programming a viable alternative to dependency injection patterns?

Dependency management is a big problem in OOP for the following two reasons: The tight coupling of data and code. Ubiquitous use of side effects. Most OO programmers consider the tight coupling of ...
user avatar
43 votes

What is the name of a function that takes no argument and returns nothing?

In the java world, it is called Runnable. In the C# world, it is called Action. But, there is a better name which nicely fits within a larger view of things. The larger view of things comes ...
user avatar
  • 31.4k
42 votes
Accepted

How would this be programmed in non-OO?

In FP style, Product would be an immutable class, product.setPrice would not mutate a Product object but return a new object instead, and the increasePrice function would be a "standalone" function. ...
user avatar
  • 184k
40 votes

What would be good factual arguments to convince high level management to consider functional programming?

You are approaching this from the wrong side. In most companies, management is not responsible for "choosing the programming paradigm", they are (or at least should be) responsible for making the team ...
user avatar
  • 184k
40 votes
Accepted

A language based on limiting amount of arguments passed to functions

Robert C. Martin in his book "Clean Code" recommends heavily the use of functions with 0, 1 or 2 parameters at maximum, so at least there is one experienced book author who thinks code becomes cleaner ...
user avatar
  • 184k

Only top scored, non community-wiki answers of a minimum length are eligible