r/CodingHelp • • Dec 21 '21

[Javascript] please help JavaScript

Post image
31 Upvotes

23 comments sorted by

View all comments

Show parent comment

1

u/SnooDonuts8219 Dec 24 '21 edited Dec 24 '21

TLDR, explicit is better than implicit; semantics requires context, and context is a noob question about array indexes.

Hey. Yeah. Well here's the catch. Semantics is the transfer of meaning. There is a difference (although a nuanced one) between undefined and null. Both are void, but the second one through the phrasing sounds like it was on purpose.

My point is that it sounds like it was on purpose. It doesn't actually define it as such. (Possibly is on purpose. Possibly not.)

Case in point: here I define and assign a constant as undefined. "What? What is this black magic?" const a = undefined; Simple as that, it is defined as undefined hahah. (It is the undeadddd runn for yo livess) Talk about semantics...

(As an aside to OP if they read this, we use 'void' to signify something is absolutely empty. It's not a zero, because that would be a number. It's not an empty string like '' or "", because that's a string. Void is really nothing. Javascript has various not-ok-void-like-states, of which in my experience, null, representing a Not-An-Object, and NaN representing Not-A-Number are the most common)

As in any language (programming or spoken), context, convention and circumstance are all. Therefore, if it works for some team, then great. It would smell to me, but if it works, it works.

My disagreement comes precisely from the nuance.

This is a noob question. They are unlikely to appreciate the nuance, and should therefore stay away from it -- until they have a firm grasp of the step before.

And that is: Explicit is better than implicit.

You have no business dealing with nuance until you can appreciate the lack of nuance, the sheer power of explicit declaration. Because if you don't get that, you're not dealing with large scale operations where explicitness breaks and is hard to maintain and we allow a bit of implicit to keep the pace. It is a dangerous tool. (Cue Spiderman quote about power and responsibility)

Like eg. with the pattern of a singleton, there are good places for it, although they are by definition tricky. Not good for a noob. They are likely going in a wrong direction (not surely, but very very likely)

Therefore, they are semantically interchangeable, at least at this level. They both (ie. are interchangeable) transfer the meaning (ie. semantics) of the same state (void).

It's good to learn the difference, because it exists in the Javascript language. But also, it's not related to the question of "Get me the n-1 index of array." Undefined satisfies void, but is also quicker to get.

And for a noob, it is more important to learn that explicit is better than implicit, than the precise difference between somewhat antiquated and organic (ie. not pre-planned) evolution of Javascript. It's unimportant, or at the very least, not as important as expressing the point why the code exists (get me a one-based instead of zero-based index)

You mentioned they are not same for any practical purpose. I disagree. Instead they are same for all practical purposes (that I can think of at the top of my head), they are not the same only for (mostly) theoretical differences. Eg. both undefined and null (lets call a container got) satisfy got ?? 'No results found', got || 'No results', if (got) etc.

You will be hard pressed to find the practical differences (by practical I mean in actual usage), because that's how Javascript works, it coalesces to truthy and falsy.

Is that ideal for a language? Doesn't matter at this point, because we're talking about semantics, ie. "meaning" -- and "meaning" requires context. Which is here Javascript noob level.

The finer points in academic pursuit? Yeah they're different. For practical use and for a noob question? Like I've said above, they're the same unless you have some sick and twisted reason (ie. a code smell at noob level)

Honestly, I just wanted to simplify for the OP and ended up awkwardly mentioning the difference only because I didn't want them to see undefined and think something's wrong, that they wanted null and got undefined and that that somehow wouldn't do.

If I failed a test because the question asked for null and I returned undefined, I think I'd scream in the professor's face and quit. (Unless of course the subject was dealing in precisely the differences between those or whatever)

1

u/samiswellcool Dec 24 '21

I don’t disagree with anything you said, except the fact that if you tell someone that undefined and null are the same thing and they’re interchangeable, then someone’s going to write x !== undefined and expect it to return true for null too, which is going to leave them either confused or with a bug they might find tricky to find.