In functional programming languages, a developer is always cautious about writing pure functions. Some functional languages simply do not allow impure functions, and some other functional languages force to to state explicitly when something impure is happening. But in imperative languages, there is usually no such distinction between pure and impure codes/functions. And impure fictions are quite the norm in imperative languages. But someone can still write pure functions in imperative languages and that is good for code health in my opinion.

So, how can we convince or encourage others to write pure functions within an imperative languages?

  • How would you write object.updateName("Bob") or updateName(object,"Bob") in a pure way in an imperative language that doesn't go through excessive contortions?
    – user40980
    Jan 7 '15 at 23:59
  • I am not for making everything pure. But many things can be written in a pure manner. And if that improves the code health, it should be done. That's all!
    – Gulshan
    Jan 8 '15 at 0:08
  • 2
    @MichaelT: return a copy of object with all fields identical except name set to "Bob". That's what the auto-generated copy method for case classes does in Scala, for example. Lenses are objects which encapsulate the notion of updating other objects in a purely functional manner (basically, lenses are purely functional getters and setters). The cool thing about lenses as opposed to properties or getter/setter pairs is that they are first-class objects. In a sense, lenses are reified lvalues, but being first-class you can e.g. pass them to functions or return them. Jan 8 '15 at 1:07
  • 1
    There's also a whole zoo of compositional operators on lenses that let you combine lenses in powerful ways. There are several lense packages for Scala (an imperative, impure language), for example. Those packages even automatically create lenses for your own datatypes (using macros). Jan 8 '15 at 1:09
  • 1
    @JörgWMittag akin to how BigInteger in Java works. However, for many aspects of how imperative languages work and the thoughts that go in behind them, this can become cumbersome. It's not so much that one can't write pure functions and methods, but that the environment, frameworks and other libraries that don't take this mindset make it a much more challenging venture than just accepting the mutation and impure paradigm of the imperative language in most places.
    – user40980
    Jan 8 '15 at 1:13

The only good way to encourage others to work a certain way, is to demonstrate the benefits of doing so. You could also force people to work a certain way, via code reviews, but if the people don't understand the benefits, you will simply make everyone unhappy.

Pure functions have plenty of benefits even in imperative languages:

  • Predictability: with the same input, you always have the same output.
  • Isolation: invoking a pure function cannot modify program behavior, since the function has no side effects. Explaining the benefits of isolation is probably a big task in itself...
  • Easier to debug, in part due to the above attributes.
  • Less places to debug: most bugs occur when the program state is modified.

This is off the top of my head, so I hope others will chime in.

Not the answer you're looking for? Browse other questions tagged or ask your own question.