I still remember a quote from a computer magazine I read when I was 12. In an article comparing the programming languages of the time, a C developer described the Pascal programming language as “having a mother hen hovering over you, watching your every move, making sure you don’t screw up.” I didn’t fully understand it at the time, but he was most likely referring to the strong type checking in Pascal, which could lead to compiler and runtime errors. If you mixed and matched types in a C program, the program would probably continue to run and fail only when something went terribly wrong.
I think our C developer was mostly right in retrospect. Generally speaking, there isn’t an overwhelming need for strict type checking. Coding is hard as it is. Developers spend a significant amount of time fighting with compilers just to get their code run. Dynamically typed programming languages like JavaScript, PHP, and Python are popular for a good reason. It’s a lot easier to get your code running in them. Now, please don’t get me wrong – I think strong type checking is important in avoiding quite a few bugs before they occur, but is the price we pay worth the benefits?
I think there is a happy middle ground between plain JavaScript and its strongly typed variant TypeScript, and that is Lodash, the popular Swiss Army knife JavaScript library. At Faradai we make heavy use of Lodash. The number one reason why we use it is because Lodash eliminates a whole range of type checks. If you pass an undefined variable to a Lodash function, the function will return gracefully, and the execution will continue from the next line without throwing any errors.
Lodash has a large number of handy functions, but probably the most important one is the simplest: _.get()
. It allows you to access an arbitrary property of an object no matter how deep it is like so: _.get(table, 'data.0.childNodes.3')
and if the property doesn’t exist, no harm is done. This is especially useful in frontend code – displaying a table cell empty isn’t usually the end of the world when the alternative is throwing the infamous ‘undefined’ is not an object error, breaking the whole table and maybe some other things too.
There are times, of course, especially when you are dealing with mission-critical code, you need to make sure that the data you are processing is in the right format. However, in such cases, you need to have a good automated test suite anyway. All the type checking in the world can’t save poorly tested code. It’s the age old validity vs. reliability dilemma. Your code may be 100% valid, but it can still be incorrect.
Another reason we prefer to use Lodash is that Lodash/fp, in my opinion, is the most convenient way to do functional programming in JavaScript. Yes, the documentation for Lodash/fp is terrible, but all you really need to know are:
- The first argument to a Lodash function is now passed from “outside” like
_.get('property')(myObject)
instead of_.get(myObject, 'property')
. - Use
_.flow()
liberally to pass one function’s output to the next. Lodash handles the necessary “auto-currying” for you in the background.
It is possible to do a great deal of functional programming in a mixed-paradigm language like JavaScript without knowing a thing about function composition, currying, or point-free style. I think there are quite a few developers out there who would appreciate the elegance of the following code:
var _ = require('lodash/fp');
var users = [
{firstName: 'Katniss', lastName: 'Everdeen', status: 'active'},
{firstName: 'Peeta', lastName: 'Mellark', status: 'active'},
{firstName: 'Seneca', lastName: 'Crane', status: 'disabled'}
];
function getFullNamesOfActiveUsers(users) {
return _.flow(
_.filter(user => _.get('status')(user) === 'active'),
_.map(user => _.get('firstName')(user) + ' ' + _.get('lastName')(user))
)(users);
}
console.log(getFullNamesOfActiveUsers(users));
The code is read naturally from left to right. The lodash functions simply transform the passed data without changing it, and finally and perhaps most importantly the code is easy to follow.
I think Lodash/fp has the potential to transform JavaScript (and Node.js) into a dynamically typed functional programming language with the least amount of effort. If you wish to add some strong type checking on top, that’s fine too.
Worth a Read:
Related: