Lambda by xTom Devian Art

Preface

Volkan Nazmi Metin — By means of titles, people call me software architect, software development manager, but my personal best is the software developer. In summary, I am generally reasonable, mostly reachable and probably making sense according to people around me. Dealing/adventuring with all aspects of programming for the last 15 years and currently giving technical consulting and free code review services.

Any input is appreciated, respond here or reach me on twitter. I like reading a code, talking about software architecture and engineering the code. I will respond every mention and comment with an SLA of 99.9999%.

This post is about my personal examination of the changes in the software development hype.

Original post on nodejs with Volkan

My career showed me a hype back and forth in the last 15 years. The hype is started with Functional Programming and as a BS, Computer Engineer and Junior developer I had been thought Functional Programming. For the first 5 to 6 years, I had written code in C (a little bit C++) on Sun Solaris, Linux, and HP/AIX environment. It was all good, and I was happy about what I was doing.

After 6 years in my career, people started to talk about Object Oriented Programming and SOLID principles. Sooner after this talks, I was pulled by JAVA first and then C#. It has continued like this and I found myself writing business/enterprise applications for about 5 years with OOP concepts.

Nowadays, for the last 3 years, people again started talking about Functional Programming and the good old concepts like Pure Functions, Closure, Lexical Scope, and Composition. Those were the topics that we were discussing and learning 15 years ago when I started writing C.

So for me, it is the Rise of Functional Programming or FP strikes back. We did not have arrow functions in C like in JavaScript but, we can mimic all the way down with pure ANSI C. OR a great map function but we do have for loops anyway.

You may say ok, so what. Well, I am coming to my struggle nowadays.

I am not a computer scientist but a software engineer or developer, so my general purpose is not investigating what is the best implementation of Pure functions or Closures etc. There are a bunch of articles around about this. What I need is to make use of them in practical applications and provide reasonable quality, for a reasonable price in a reasonable time period.

For me, it is not the war between FP and OOP, it is not my concern which one is best. The best one depends on the requirement of the application and only be driven by it. I saw badly implemented classes and also incredibly bad implemented functions. So it is not the programming paradigm that makes the application bad, but it is us, the programmers.

Now the struggle is; as a relatively experienced Software Engineer, I have issues to tell these functional programming concepts to developers, who have born into Object-Oriented programming. Since expectation from me is to mentor the less experienced ones.

Let’s consider a practical example of closure and lexical scope in JavaScript.

Greet — EcmaScript 5 Version

For a JAVA or C# developer who has no idea about functional programming, this implementation totally does not makes sense since returning a function or for them, a method is not a general practice. It is not straight forward in those languages anyway.

Let’s take a look at the same -nearly same implementation of above code with C#.

Still easy for a C# developer to read but I haven’t seen anyone implement this functionality as above. Generally, if I ask a functionality like this, what I would probably get;

Cons of this Greet or Greeter is that they are not reusable enough, but generally, it doesn’t matter for most of the C# people since the name is a parameter and this is an OK enough to provide the generic structure of Greet.

Let’s take a look at to the ES6 version of the same implementations, which totally does not make any sense at all to a C# dev. Here by means of the functionality of functions, ES6 version exactly does the same thing as the first ES5 version. Although, there is a big difference on how they handle the *this*. But, this is for another post though.

Greet — EcmaScript 6 Version

So for a C#/JAVA developer, this is not readable at all. If you have not done any functional programming in the past, it is not. But for those who had, it is even much more readable than the ES5 version.

Another important part of this is, in C#, since it is compiled and scope is validated while you are building the code, you do not need to consider how the name parameter is available in your Greet method. (Consider first C# version of Greet) Even worse, most inexperienced OOP guys using JAVA or C# doesn’t even know about scope let alone this is lexical scope. This makes life hard when you start working with pure OOP developers.

So let’s look at our code and try to understand the lexical scope.

Lexical Scope

In our JavaScript code (ES5 version), we have a function that returns a function. More specifically Greet function returns a Punctuate function. The problem here is that the returned function uses the name parameter of the Greet function. If you only consider the block scope, the name is undefined for our returned function. So, how this is happening… Basically, JavaScript is handling the problem for us and even the scope of Greet and Punctuate finalized, JavaScript still enables the name variable for the returned Punctuate function.

There is a great article right in MDN which deals with only the lexical scope mechanism of JavaScript, I suggest you take a look at this also.

Summary

Functional and Object Oriented Programming is a totally different way of thinking and the understanding of scope differences is only one of the milestones. I will try to clarify more concepts for pure object oriented mindsets since nowadays Functional Programming strikes back.

Note for OOP Fans: As I mentioned earlier in this post that is not a comparison of FP and OOP. I mean no offense here.

Lastly, if you like what you have read, please check my blog also and click clap for this to reach out even more people.