2

I love functional programming and I love Ruby as well. If I can code an algorithm in a functional style rather than in a imperative style, I do it. I tend to do not update or reuse variables as much as possible, avoid using "bang!" methods and use "map", "reduce", and similar functions instead of "each" or danger loops, etc. Basically I try to follow the rules of this article.

The problem is that usually the functional solution is much slower that the imperative one. In this article there are clear and scary examples about that, being until 15-20 times slower in some cases. After reading it and doing some benchmarks I am afraid of keep using the functional style, at least in Ruby.

By the other hand I feel more comfortable writing code in functional style because it is smart and clean, it tends to less bugs, and I think is more "correct", specially nowadays that we can use concurrency and parallelism for better performance.

So I am very confused about which style to use in Ruby. Any wise recommendation will be appreciated.

Rafa Paez
  • 4,820
  • 18
  • 35
  • 1
    I have the same issue. Something to consider is that Ruby may not be as optimized for functional programming as other languages. The Ruby community seems to highly value code elegance over premature optimization, however, so my general m.o. is to use a functional style unless efficiency or space constraints make it necessary to use an imperative approach. Also: while interesting, this question isn't really appropriate for SO since it's more of a discussion question. I would rephrase to ask a specific question. – jmromer Mar 10 '14 at 01:25
  • This is an excellent question and it should be great to know the opinion of wise advance developers. Sometimes we accept questions that can be easily found in a tutorial and sometimes we put on hold interesting questions like this one that no tutorial or manual will tell you. I disagree despite I understand the rules. – Rafa Paez Mar 10 '14 at 09:30
  • Use the Benchmark class or Fruity to prove your theories. That's how I show whether one path is faster than another and removes the guessing. – the Tin Man Mar 10 '14 at 14:35
  • Also, Ruby 2+ is much faster and has been optimized in ways that 1.9+ wasn't. Don't trust articles on 1.9 to be totally applicable and always be skeptical until you've proven it for yourself. There is an amazing amount of stale information out there. – the Tin Man Mar 10 '14 at 14:40
  • 1
    Why not posted here - https://www.ruby-forum.com/forum/ruby.. This kind of question is welcome there. Please post there,, and give the posted link to me also. I wanted to see the discussions.. – Arup Rakshit Mar 10 '14 at 22:55
  • @ArupRakshit: Thanks! I am not register in that forum but I will take take a look. – Rafa Paez Mar 10 '14 at 23:01
  • @Rafa Very active forum that is.. Like SO. – Arup Rakshit Mar 10 '14 at 23:07

0 Answers0