I have been learning some Ruby recently and I had the following experience. I had written a small tool of about 200 lines of code and, as an inexperienced Ruby programmer, I had used several loops to build arrays from other arrays. E.g.
gs = ... # Some array of input data.
l = gs.size
i = 0
r = Array.new
while (i < l) do
if (cond(gs[i]))
r.push(gs[i])
end
i = i + 1
end
# r contains the result now.
Then I discovered that I can do it in a functional style:
gs = ... # Some array
r = gs.select { |g| cond(g) }
# r contains the result now.
So I immediately replaced most of my loops in the code with constructs like each, select, take_while, drop_while, and so on. I have decided to use a functional style in Ruby whenever possible and try to avoid an imperative style.
As another example, in C++ I tend to use classes and methods everywhere and use functions only for small utility tasks that are private to the implementation of some class. So I tend to think object-oriented when working in C++ and avoid a procedural style almost completely.
More in general, when working on a given project in a given language, I tend to pick one paradigm and stick to it throughout the project: I find this makes my code more uniform, easier to read and to debug, which, IMO, is very important, especially for production code.
Yet, I often think that maybe I should explore more and try out different possibilities offered by a language, e.g. use different paradigms and try out different solutions. For me this involves some overhead both while coding (should I make it procedural or object-oriented? imperative or functional?) and when reviewing or debugging code (what did I want to do here?) because of the increased complexity.
So my question is: do you also tend to select a preferred programming paradigm when coding and stick to it, or do you tend to explore more and mix different paradigms?
If you mix different paradigms, do you have the feeling that there is some overhead in terms of how fast you can write and debug code (added complexity, switching between different mindsets) or is it just a matter of learning how to do it and then you can even be more productive?
Or do you mix different paradigms in a more structured way, e.g. you apply each paradigm to a specific class of problems (e.g. functional for queries, object-oriented for architectural aspects, and so on)?