Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

C++, Functional Programming, and Immutability



  • Maybe I just have not read enough about this topic, but immutability is really bothering me. The code I tend to write using this approach tends to copy things many times with multiple loops. With imperative programming the same loops can be executed once in one loop if I can mutate the data. (I don't have a concrete example on hand, but I can dig one up.) I know Qt has types that can share data, but as far as I know as soon as I mutate the benefit goes away.

    I am trying to improve my different approaches to programming and my assumptions about how smart compilers are often falls short of the things they can actually do.

    Can the compiler reason about this if the objects are immutable and produce optimized machine code? Is the compiler smart enough to see these loops and immutable objects and combine them into a single loop and set of operations?

    Note: I am in the middle of some FP books for C++ and perhaps I just need to read more. However, this really bothers me and I was hoping to get the weigh in from some of experts here in this forum. There are other benefits to FP approaches that I really like. Immutability is important for those to work. I just cannot help but wonder if FP is good for "some" types of programming and not others.


  • Lifetime Qt Champion

    Hi,

    Are you reading this book ?

    If not I highly recommend it.

    And to sum up: functional programming is a good tool to have in your tool belt but it's not always the right tool to do what you want.



  • I think I have that book. Plus like 2 or 3 others. I need to finish reading it... It would probably answer a lot of my questions.


  • Lifetime Qt Champion

    Yes, I recommend to finish the one from Ivan. It's really well written and talks about immutability.


  • Moderators

    For a briefer discussion than a book, see https://softwareengineering.stackexchange.com/a/149576


Log in to reply