How to use Dependency Injection in Functional Programming

Dependency Injection is a technique to make the classes in Object Oriented Programming easier to test and configure. Instead of a class instantiating its own concrete implementations, it instead has them injected into it. In Functional Programming, that’s a fancy way of saying “calling a function with parameters”. However, it’s not that these parameters are data, but rather the same thing type of dependencies you’d use in OOP: some type of module or function that does a side effect, and you want to make your function easier to test.

In this article we’ll show you how OOP uses DI to make the classes easier to test, then we’ll show the same technique in FP using JavaScript for both implementations. Code is on Github. After reading this you’ll understand how to make your FP code easier to test and configure, just like you do in OOP style coding.

Continue reading “How to use Dependency Injection in Functional Programming”

Why Functional Programmers Avoid Exceptions

If you’re in a hurry, here is the 60 second version:

My previous article caused a variety of consternation, imperative patriotism, and lots of nuanced follow up. It reminded me of when Richard Feynman was asked to define how magnets work and he refused. The perturbed interviewer postulated it was a reasonable question in hopes to understand why Mr. Feynman wouldn’t answer it. Richard Feynman covered a variety of reasons, 2 of which were:

  1. you have to know the deeper reasons first before I can explain it
  2. I can’t cheat by using analogies that they themselves require deeper meanings to explain how _they_ work.

In the case of avoiding async/await keywords in JavaScript, this makes a huge assumption you know about Functional Programming, Imperative, exception handling, how various languages approach it or don’t, the challenges between dynamic and strongly typed languages, and on and on.

Continue reading “Why Functional Programmers Avoid Exceptions”

Why I Don’t Use Async Await

A lot of JavaScript developers speak in exceptions. However, JavaScript does not have any defined practices on “good exception handling”. What does good mean? All using try/catch, .catch for Promises, and window.onerror in the browser or process.on for Node.js? Just http/file reading/writing calls? 3rd party/vendor systems? Code with known technical debt? None “because fast, dynamic language”?

In my view, good exception handling is no exceptions. This means both writing code to not throw Exceptions, nor cause them, and ensuring all exceptions are handled.

Continue reading “Why I Don’t Use Async Await”