If we’re talking about readability, I prefer

~~~~~~~~~~~~~~~~~~~~
car.event &&
car.foo &&
~~~~~~~~~~~~~~~~~~~~
(
console.log('foo'),
console.log('bar'),
function() {
for (let i = 0; i < 10; i++) {
console.log(i);
}
}()
);

But that’s more to do about the if...then syntax. I suppose it’s better to be explicit than implicit, as the Zen of Python says, but I’m not so sure [obj].[prop] is really less explicit except but to people not familiar with the language. But once someone learns the language, they’ll probably look at [obj].has([prop]) as a kind of counter-intuitive thing since they’d likely have a “is-a” model of thinking to which the explicit has (contrast with “has-a”) would rub them the wrong way. Even the example here car.has('event'), really? It might be more explicit, but do cars really have events? “Explicit” to whom, what are we assuming about the reader? Granted, we cannot make the document and everything in it “explicit” to any and all kinds of readers, to every kind of audience. Now in the domain model, of course, the car may have an event because that’s what the domain expert says or what the use-case implies. But without that understanding, cars do not really have events, they have doors, wheels and engines, speed or a current speed, price, creation dates, and so on. So it’s interesting in how the language’s “readability” can be contextualized by these factors, if we’re assuming “the outside world”, even considering of annoyingly excessive literalists like me, as “it’s more readable to novices to the language” is an “outside world” assumption no less than “is-a” thinking is an outside world assumption (old school OOP/class-ical programmers) and “domain experts reading my code” is an outside world assumption.

The general point I’d make is that if I read such an expression as car.has('event') as counter-intuitive irrespective to the programming language, how much of the language do I need to learn for it to become “more readable”? And if I’ve mastered enough of the language to read it as a programming language expression, wielding that duality, do I really need such a convenience in the first place? It adds more characters to the code and involves a method whose name (has) directly conflicts with a mental model I use when reading or reasoning about the code (i.e. “is-a” vs “has-a” relationships), whether I’m an non-English speaker, an OOP class-ist, or a domain expert. To say the least, it really seems to me that anyone who would use Object.entries and Map in this way does not need help with readability of JavaScript and “being explicit” here has the outcome of undermining readability of genuine “is-a” relationships; e.g. user.verified (“is-a” relation) vs user.has('verified') (“is-a” relation made, superficially, to look like a “has-a” relation, even though I know enough of the language to understand the relation as such is not reflected by the method in my context switching effort as I read). It’s certainly longer and maybe I feel more at home with it as an English speaker, but I don’t know if it’s more explicit.

Yes, these are OO concepts, more generally, and the example of car.event is probably a bad one. But I’d say the has method here forces one into having to make such contrasts of mental models, whereas the language is simple enough on its own such that it affords the opportunity to leave it up to the reader. Of course, you could say car.event implies that the car “is-a(n)” event, but I would reply that, if you know enough of the language to know that you don’t need help with readability in the first place to warrant adding more code, more complicated declarations (perhaps performance isn’t really worth noting here, or is it?), and potentially implicating “has-a” relationships for every and all objects or inciting the OO discussion. You might also say, “the OO programmer would never think an ‘is-a’ relation holds except but under circumstances of a class inheritance”, and that might be true, but again I’d reply that if they understand programming languages in general well enough, I don’t think they’d need help with readability in this way. In any event, I’m just not sold that even “readability” is what I’m being afforded by this strategy or that “readability” is a defense of it. It seems more like anglicization of a programming language, more than anything else, for better or for worse.

nobody leaves the cave before the end of a new dawn https://gist.github.com/dualyticalchemy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store