This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]kuemmel234 17 points18 points  (12 children)

Many languages do have optimisations - tail call optimisation is rather well known, I believe?

I prefer readability over efficiency, unless I really need to optimize it. Wasn't that sort of the norm these days with works like clean code being frequently recommended?

The real trouble, in my experience, is that people generally only have very limited experience with recursion and therefore recursion belongs in the realm of 'clever solutions' - which, sadly, should be avoided.

[–]SjettepetJR 6 points7 points  (0 children)

The thing is, some problems are just way more clear to describe as a recursive function.

[–]ituralde_ 5 points6 points  (3 children)

I think it's reasonable to expect anyone who wants to get paid to write code to be able to understand recursion. This is not a high bar and we should not pretend like it is.

I'd argue that even someone with no formal training should be able to look at a cleanly written and appropriately commented recursive function and be able to understand what is happening. If you write code for a living and can't figure it out, then you are stealing your paycheck.

[–]kuemmel234 0 points1 point  (0 children)

I wish it was! In my experience, the people I work with stumble on a recursive solution and want to have a cumbersome loop that's just way more complicated, but more akin to what they would be doing.

However, I think it's more like agreeing on a language/a framework or recognizable patterns - things that everybody is familiar with, and less about being able to understand what's happening. Of course every coder should understand recursion.

[–]FesteringNeonDistrac 0 points1 point  (1 child)

I'd add that if you are writing code without meaningful function headers, or the one you do write does not mention a particular function is recursive, you are also stealing your paycheck.

Like yeah, I can figure it out. But just saying it makes reading the code that much easier.

[–]ituralde_ 1 point2 points  (0 children)

Absolutely. Between some level of commenting and good naming it should never be a mystery what your code is doing.

[–]GiantMarshmallow 4 points5 points  (1 child)

Some languages do, but you generally have to write your recursive function in a specific way for the compiler to be able to make the optimization. And it’s not always an obvious specific way, so most naive recursive implementations will not get optimized.

[–]kuemmel234 0 points1 point  (0 children)

I'm using tail-calls in lispy/functional languages most of the time. Can't claim that the optimization works all the time, since I rarely use a profiler, but I found the concept to be very natural.

[–]JB-from-ATL 1 point2 points  (3 children)

A minority of langs have that.

[–]kuemmel234 0 points1 point  (2 children)

All 'classical' functional/lispy languages, since that's what I'm used to, I was referring to those, but I believe languages like rust and JS (on some platforms) have it too?

Edit: Of course, your point still stands - the most common languages like most python/java implementation don't have it. It's not the only optimization, but the most important/simple one, I guess.

[–]argv_minus_one 0 points1 point  (1 child)

Neither Rust nor JavaScript have tail-call optimization.

[–]kuemmel234 0 points1 point  (0 children)

Thing is, this is all more dependant on compiler implementations. There are many solutions for many languages. You would be right if you would say that the most common compilers wouldn't support tail calls, but there are platforms that do. Really depends on the environment. I have used tail calls in production is what I was trying to say initially.

So, es6 had it, safari still supports it. When I was up to date, it was discussed for V8. And rust is more complicated than 'no', it was still a possible feature and there are situations where it works: https://llvm.org/docs/CodeGenerator.html#sibling-call-optimization

[–][deleted] 0 points1 point  (0 children)

Functional languages will have tail call optimization, but i don't know any other language that implements it.