r/node • • Nov 10 '20

Conditionally call a function with optional chaining.

Post image
345 Upvotes

126 comments sorted by

View all comments

Show parent comments

4

u/[deleted] Nov 10 '20

Hard disagree, it's only more readable if your definition of readability is pinned to that of a beginner. As we become more knowledgeable our idea of readability should move with us.

Take this for example, beginners often don't know about ternaries:

const x = a ? b : c;

The more "readable" approach as you put it would be like this:

let x;
if (a) {
    x = b;
else {
    x = c;
}

Not only is this noisier and harder to parse for anyone more experienced but it's also opened up the need to track the mutability of the variable reference that didn't exist with the ternary approach.

1

u/HappinessFactory Nov 10 '20

I was goin with that if statements read more like english.

Shifting everything into one line does shorten things. And those of us that know what a turnarys does can read it faster.

But it's not more "readable" if that makes sense.

Kinda like object parsing:

const getDataFromObj = ({data}) => data;

Is quick and easy and those of us who know how it works understand how useless of a getter it is

Buuuuuut, I would argue this is more readable

var getDataFromObj = function(obj){ return obj.data; }

I'm writing this on my phone so I apologize for the formatting.

Anyways, the point is while shortcuts are nice and convenient it does come at a cost of readability and we do have to be aware of that.

Youre not wrong, it is noisier, but it still closer to english which is the goal of readability.

To be clear even as a junior I understand the benefits of using these newish techniques and I implement them myself.

But they're not more readable. That's just false. It's like saying Moby Dick is more readable than see spot run. Yes it's a better book but, it's substantially harder to read.

1

u/[deleted] Nov 10 '20

It's more readable in code which is a distinct type of language. Your second code snippet there isn't more readable, it merely has more words in it. Is that all that matters? Would you prefer add(1, 2) over 1 + 2 for this reason?

If you're keen for traditional language-esque readability then you ought to try Haskell, which ironically demonstrates extremely well that programming is closer to mathematics than anything else. That we currently can best express it linguistically is an irrelevance.

1

u/HappinessFactory Nov 10 '20

We clearly have different definitions of readability. I suppose this means we will just have to agree to disagree.

You do make strong points though. I agree with everything aside from code being its own distinct language. To me it is still very much english.

Like how I prefer using .forEach instead of you're old school for loop. It's closer to english.

I haven't tried Haskell as a front end dev I am very much in js land for better or for worse. ( I like js and TS a lot tbh)

2

u/[deleted] Nov 10 '20

I respect your perspective. One of the nicer things about Haskell is that sometimes the code comes together like this: andFinallyThis . andSomethingElse . doAThing Hopefully we'll see something similar if TC39 approves the pipeline operator proposal.

The trouble is that if you optimise for linguistic familiarity above all else you sacrifice other really important properties, properties that inherently exist because programming is, or at least can and in my view should be, a logical venture.

Taking forEach as an example, if you were to use this for mapping an array, you'd have a program that's harder to quickly parse (again, for anyone other than a beginner), and crucially the logic of the program would be harder to follow. See:

const nums = [1, 2, 3];
const strs = [];

nums.forEach((num) => {
    strs.push(String(num));
});

We have to keep track of the mutability of strs (in practice, in a functional codebase, you consider const to mean truly immutable). We have to track the iteration over nums and ensure we're not doing anything different in some loops.

On the other hand, Array.prototype.map:

const nums = [1, 2, 3];
const strs = nums.map(String);

Functor mapping implies a pure function, so when you read this code you know beyond all reasonable doubt that it will map an array given the provided function and that's it. No changes to nums, and a predictable value at strs. This is an enormous benefit to readability that can only be realised if you're willing to raise your definition of readability vis a vis the lowest common denominator.