Labrinth — Alexas_Fotos The challenge is to create a piece of software (something real, tangible and more than a hello world) without any variables. The idea came from a tweet of ’s article. Which I responded (in jest) with try to “code without variables”. Samer Buna I am planning on creating a Promise library. I was inspired by an article I stumbled across by , . Trey Huffine Learn JavaScript Promises by Building a Promise from Scratch I believe Promises are simple enough to understand the code and also complex enough to be a challenge. Warning! If I were to compare writing clean code to riding the Tour de France, . This code would be better compared to the X-Games BMX Freestyle Big Air. You about to see a couple of double backflips and 360s, but when get on bike it’s probably best to keep all wheels on the road. Kids, don’t try this at home . this code is not that you your or work With that being said ( ) there is a lot to learn from this code and I encourage you to create your own playground and see how extreme and freaky you can get. It’s at the edges is where you’ll discover the most interesting things. if you allow yourself The Rules Keywords not allowed: , , , , . Bonus points for not using or , or keywords. var let const import class if switch function Libraries are allowed as long as all rules are followed. New libraries can be created, but must follow all rules. Libraries must be generic enough to use in any project and cannot be a substitution for the business logic of the code created. Tests are not necessary. But if you choose to write tests, they are not subject the rules. Let’s Start! TDD A Promise library is farily complex and as I make changes to the code, I want to be sure those changes don’t break anything that previously was working. So I am going to start by writing out all my tests first. This is made easy because node already includes a Promise library, so I will write my tests against that first. One difference, is I do not plan to create any classes as . So instead of the typical code you would use to create a Promise: , You can just use excluding the keyword. I find classes unnecessary in JavaScript new Promise((resolve, reject)) XPromise((resolve, reject)), new XPromise.tests.js Start with the interface Right away I was presented with a challenging task. Similar to the A+ Promise implementation, I wanted to be able to create a Promise using , and . So needs to be a function, but also have 2 properties ( and ), which are also functions. XPromise((resolve, reject) => …) Promise.resolve(…) Promise.reject(…) XPromise resolve reject Normally this would not require much thought, but because of the rules I am unable to do something like this: Time to get creative by using to attach and to the main function. Object.assign resolve reject So far I am pretty happy with this. That is, until I realize and are helper functions that will eventually need to be pointed to the main function, which now there is no reference to 😦 resolve reject XPromise Creating a reference without a variable also needs to return an object that contains 2 functions, and . Those functions must call the original function, which (again) there is no longer a reference to. XPromise then catch XPromise So… I need figure out how to create an asynchronous, recursive, anonymous function or this whole thing is gonna be a bust. Crap. It’s time to bust out the Combinators When talking about anonymous recursive functions, the famous Y combinator immediately comes to mind. That is the Y Combinator’s purpose. Though, the Y Combinator isn’t the only combinator we can use. For this task, I have decided to use the much less known, but more simple U Combinator. I dig the U Combinator because it’s easy to remember. f => f(f) That’s it! The U Combinator takes a function as an argument and then passes that function to itself. Now the first argument of your function will be your function. If that sounds confusing, . Don’t worry about that, it’ll be easier to see in code. that’s because it is confusing Take notice of the part and how it is the same for both and . sayHello => () => 'hello' sayHello UsayHello Now let’s try this with recursion. Perfect! This exactly what we need! Now it’s time to jam it into the project. Okay, this is the basic skeleton of a Promise. We have our main function , the helper functions and . takes a function, which contains and . This function also returns an object that contains the functions and . XPromise resolve reject XPromise resolve reject then catch You can see that I am also using an Immediately-Invoked Function Expression to make the U Combinator available to use as the argument. U If I haven’t lost you and are still following… rest assured, for the remainder of this article we’ll be coasting down hill! 😃 Stay with me now, the worst is over! Storing state This application, like others has to store some kind of state. This will either be the values from , and/or the functions from and . Typically, this would be done with good ‘old variables. Though, we can also accomplish the same thing just using default parameters. This will also give the added benefit of being able to call the function and also seed it with a new state! Which, spoiler alert, we are going to do just that! resolve reject then catch BTW, This is a great case for Redux! Converting blocks to expressions I coding with over . This is just a preference of mine. statements and contain blocks, so I gotta make an alternative. I also like to use the to combine multiple expressions, which you will see below. prefer expressions blocks if try/catch comma operator Ya, that is much better! 😀 Now I wanna clean up that . try/catch Fast forward >> This article is about the challenge of writing software without using variables. So to save time, let’s skip the boring shit and just fill in the blanks. This article is not about how to create a Promise library. Well, there it is, in all it’s glory; A Promise library without a single , , or . const let var And check this out… all my tests are passing! 😁 Source Code @ https://github.com/joelnet/XPromise Postgame wrap-up This challenge ended up being a lot harder (time consuming) than I thought. Not necessarily because of the limitations, but because . A promise may or may not be called synchronously/asynchronous, may or may not have a resolve value, a reject value a then resolver and/or a catch resolver. That is 64 possible states! Clearly I don’t have enough tests. creating a Promise library was much more complex than I expected it to be I finally had a legitimate use case for the U Combinator, which was totally awesome. Medium sucks at code blogging and it’s a pain in my ass to create a gist for every tiny snippet 😦 I kind of like the way this library turned out. The entire thing ended up becoming a single expression. My message to you I know this became complex fast, don’t feel like you have to understand 100% of it. I just hope that you found it entertaining. I hope you there was something in this article you haven’t seen before. I hope I made you curious to code-explore on your own! How would you have completed the challenge? Would you have made the same choices? What did you like? What would you have done differently? I would ❤️ to hear your thoughts! Follow me here, Twitter , , it makes me happy… and when I am happy I write more! @joelnet Linked-in Continue reading here: _Read the latest stories written by Joel Thoms on Medium. Computer Scientist and Technology Evangelist with 20+ years of…_medium.com Latest stories written by Joel Thoms - Medium
Share Your Thoughts