Skip to main content
324 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 ...
amon's user avatar
  • 135k
227 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 ...
JacquesB's user avatar
  • 59.8k
176 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 ...
Jack's user avatar
  • 4,509
152 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 ...
Jörg W Mittag's 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 ...
Quitting Due To Antisemitism's user avatar
105 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 ...
JacquesB's user avatar
  • 59.8k
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 ...
Kevin's user avatar
  • 2,658
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 ...
Andres F.'s user avatar
  • 5,139
64 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 ...
David Arno's user avatar
  • 39.3k
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, ...
Karl Bielefeldt's user avatar
56 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 ...
Jörg W Mittag's user avatar
54 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 ...
Flater's user avatar
  • 52.5k
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. ...
Michael Borgwardt's user avatar
49 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 ...
JacquesB's user avatar
  • 59.8k
48 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 ...
Jörg W Mittag's user avatar
46 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 ...
Jules's user avatar
  • 17.8k
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 ...
Telastyn's user avatar
  • 109k
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. ...
Doc Brown's user avatar
  • 209k
41 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 ...
Doc Brown's user avatar
  • 209k
40 votes
Accepted

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

If you do this long enough, you'll eventually find yourself writing this function over and over: public static Type3 CombineFunc1AndFunc2( Func<Type1, Type2> func1, Func<Type2, Type3&...
Jack's user avatar
  • 4,509
40 votes

Return considered harmful? Can code be functional without it?

Tell, Don't Ask comes with some fundamental assumptions: You're using objects. Your objects have state. The state of your objects affects their behavior. None of these things apply to pure functions....
Robert Harvey's user avatar
38 votes
Accepted

"Remembering" values in functional programming

First of all, congratulations on "seeing the light". You've made the software world a better place by expanding your horizons. Second, there is honestly no way a professor who doesn't understand ...
gardenhead's user avatar
  • 4,747
38 votes

Is there a non-deterministic function without side effects?

Of course this depends on the definitions. Let's drop "pure" which has a definition in your question that clearly makes non-deterministic pure functions impossible as being deterministic is ...
Hans-Martin Mosner's user avatar
37 votes
Accepted

What are the benefits of referential transparency to a programmer?

The benefit is that pure functions make your code easier to reason about. Or, in another words, side effects increase the complexity of your code. Take an example of computeProductPrice method. A ...
Arseni Mourzenko's user avatar
37 votes

Why are discriminate unions associated with functional programming?

Having programmed in Pascal and Ada before learning functional programming, I don't associate discriminated unions with functional programming. Discriminated unions are in some way the dual of ...
AProgrammer's user avatar
  • 10.5k
37 votes
Accepted

How do compilers work in a language that doesn't allow recursion?

Recursion can only be programmed either by having a call to function A within the definition of A itself (direct), or by having function A call function B, and function B call function A (indirect). ...
Kilian Foth's user avatar
35 votes

How do the type systems in functional languages differ from those in OO languages?

tl;dr summary: they are more powerful, more expressive, and most importantly, they don't lie. [Aside: I would like to point out that there is a double false dichotomy in the OP's question. FP and OO ...
Jörg W Mittag's user avatar
34 votes

"Remembering" values in functional programming

This is really just an addendum to gardenhead's answer, but I'd like to point out there's a name for the pattern you're seeing: folding. In functional programming, a fold is a way to combine a series ...
Jack's user avatar
  • 4,509
34 votes

Do compilers like Javac automatically detect pure functions and parallelize them?

are compilers such as Javac smart enough to detect when a method is a pure function. It's not a question of "smart enough". This is called Purity Analysis and is provably impossible in the general ...
Jörg W Mittag's user avatar
33 votes
Accepted

What is the benefit of having "no runtime errors", like Elm claims?

Exceptions have extremely limiting semantics. They must be handled exactly where they are thrown, or in the direct call stack upwards, and there is no indication to the programmer at compile time if ...
Karl Bielefeldt's user avatar

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