My search for supremacy
Tags: oop, functional, javascript, rails
Published: 2017-07-27 13:16:17

_Internally, a struggle between good and evil is never ending. Externally, I smile, nod, and write functional code._

When I decided I wanted to learn how to make websites, I went to a friend for help. He told me I should learn PHP so I bought a big fat PHP book, read the first 20 pages and then decided I knew enough to get started. My very first project was my own personal website. I started by taking an HTML template a friend of mine created and turning it into a database-driven blog. This entire blog consisted of two files. `index.php` which connected to my database, queried it for blog posts, formatted those blogposts and listed them and `post.php`, which connected to my database, queried it for a single blog post, formatted the blog post and allowed a user to read said blog post.

From behind my keyboard I can hear a million developers sigh and groan. Don’t worry, in the eleven years since then I've learned a lot. I now understand MVC and MVVM models. I understand the importance of file structures and how to write proper routing but the biggest thing I've learned, the thing that changed everything was Object Oriented Programming.

##Seriously OO

After a short stint with PHP and a passionate but short relationship with C#, I found my one true love; Ruby (on Rails). This was my first introduction to full on Object Oriented Programming and I fell in love immediately. To me, writing applications in OO languages feels like extending the analogue world into the digital space. The world is Object Oriented and once you subscribe to that way of thinking, writing proper OO code becomes easy and natural. For a year and a half I did nothing but write Ruby code. Every project, every application, every line of code was done with Ruby on Rails.

As a framework, Ruby on Rails has its disadvantages. It’s 2017 so real time applications are becoming more popular and asynchronous functions have become the bedrock on which a lot of applications are now built. Unfortunately, Ruby on Rails isn’t great for either of these concepts. Don’t get me wrong, RoR has improved but when I was asked to make a sports betting website (which needs to be almost entirely real time), I made the decision to use JavaScript (on NodeJS) instead of Ruby (on Rails.)

##Back to JavaScript

_When I code in Ruby, I swear in English. When I code in JavaScript, I swear in Ruby._

Saying I don’t like the JavaScript syntax or code standards is an understatement but it has great advantages too. Completely built for asynchronous function calls and with very easy real-time communication implementations, JavaScript is an incredibly useful language for projects such as the one we were going to be working on. I hired a few developers — asking them all if they preferred Object Oriented or Functional Programming to understand what kind of experience they’d had — and we started developing.

Initially, every line of code I wrote felt inefficient. Leaving Ruby behind meant leaving behind the comfort and ease of coding that drew me to Ruby in the first place. As the project grew and we had more types of data to handle, I started to long for the Object Oriented way of thinking, and more than that, for the relational database that’s so common in Ruby projects and so uncommon in JavaScript projects. Having to do 4 or 5 queries to render a page with fully relational data seemed unnecessarily slow to me and while I knew better, I attempted to turn JavaScript into an Object Oriented language, with relational data being generated at the model level.

At first, my solution felt great. I was able to write JavaScript as if it were Ruby, my data was relational and I didn’t have to write multiple queries for it. However, it didn’t take long for JavaScript to hit back. The functional-to-object-oriented-hack I made was slow and it was affecting our application. I realised I had to accept I wasn’t using Ruby anymore, refactored our application and went back to functional programming.

We finished our project's MVP, refactored a few more times and found a reasonably comfortable way of writing code. I was getting used to the functional programming way of like but much like getting used to wearing glasses, I wanted to see 20/20 again.

##New beginnings

My real-time project has come to an end. I have seen the powers that JavaScript can give a developer and I have accepted that some languages work better as functional programming languages. On the other hand, I still believe in the power of object oriented programming and prefer it over functional programming in most situations. Now I’m offered new projects and am forced to find a language that fits the problem set. I could choose to use JavaScript when I need asynchronous functionality and real-time communication. Then when I’m building larger applications with relational data, Ruby can be my go to. It's never bad to have many tools in your arsenal but I know myself. I will keep searching for the one ring to rule them all. The one language that is easy to write, object oriented, does real-time and async quickly and easily and above all, doesn't have semi colons;

The search for supremacy is never ending. The answer is out there somewhere.