The main problem with writing in verse is, if your fourth line doesn’t come out right, you’ve got to throw four lines away and figure out a whole new way to attack the problem. So the mortality rate is terrific.
x = x + 1
When I started programming in my early graduate years, I found above statement quite puzzling. In normal algebraic context, above line didn’t make sense. But I was told to forget algebra and get used to imperative programming. It’s a statement, understand it and get used to it. During all these years, we got exposed to various scenarios and problems, and we learnt to solve all of them using the tools and techniques of imperative programming. I worked on number of programming languages but all of them were philosophically similar with differences in syntax. I prided myself in being able to pick any language and deliver on short notice.
Enter Scala! I expected to be able to pick the syntax and move on. But to my surprise, soon I realized that it’s new game. Scala comes from a different programming paradigm – functional programming. In the world of functional programming above construct was no more good. It’s whole new way of thinking (Though Scala allows above statement to work because it’s a multi paradigm language, we will keep our focus on functional programming aspect only where it is not a valid construct). Mutation is evil!
Functional programming has its roots in lambda calculus. It has strong mathematical background and has been more popular in academia than industry. Going functional way will need significant re-tooling of our minds, it has different way of solving problems than imperative. We have to learn new ways of designing and composing programs. It’s going to be decent effort by any standard. Even early adapters of Scala have expressed that there is steep learning curve required for teams to be able to effective.
Is functional programming better than imperative programming?
This is also one of those epic battles where no winning side can be declared – imperative programming vs. functional programming (or at least I can’t be that judge!). Both of the sides have equally passionate foot soldiers rooting for it.
I will prefer talking from my personal experience.
A whole new perspective
We tackle variety of scenarios in our day to day programming assignments. They never come with neat labels on them which say it has to be solved with a specific paradigm. All it needs is an elegant solution that is effective and in time. One size doesn’t fit all and it will be good to have all problem solving tools in our toolkit. We will be better by not being held hostage to a particular way of thinking rather open to all. After all our allegiance is with business, not with any specific programming paradigm. Learning functional programming will broaden our perspective.
That is the precise reason why Scala is a multi paradigm language and has support for imperative, functional and object oriented programming. Similar path is being taken by other leading programming languages Java 8, C#, Python. They all are having support for functional programming constructs in varying degrees.
Brevity is the soul of wit
But we are not trying to be funny here! After all programming is a serious business. And we all have heard countless tales of amazing sense of humour of our fellow programmers!
But you have to trust me it is amazing to read a code free of tons of boilerplate code, un-necessary repetitions and obvious type inferences. You can skim the code faster and get hang of purpose of the code way more smoothly and effortlessly in functional than in imperative. It’s amazing to express out computation succinctly and be able to glide though them quickly when needed. By the way it is less error prone as well.
Need for speed
In past few years we have seen a clear trend in hardware that multi core / CPU is the way forward. We are talking about horizontal scaling – distributed programming. Data is exploding and only way to cope it though distributed programming. I find immutability and no side effect philosophy of functional programming specifically effective here. Erlang has been a shining example in this area. Few of recent examples include Spark, Kafka and Flink.
Test all the way
Functional code with its focus on brevity, immutability and referential transparency makes unit and integration testing very easy. It has huge impact on reliability of code and productivity improvement.
It is too early to predict future in the battle of supremacy of paradigms and languages. But one thing is clear to me no matter which camp you belong to (Java/C#/Python etc.), you are going to deal with tenets of functional programming!
So wake up, smile and smell the coffee. Why so serious!