// this is a user object or undefined, should probably use `some` instead of `find`constisUserPostCreated=users[0]&&posts.find(post=>post.userId===users[0].id)if(isUserPostCreated){showUserPost()}
I'd usually do the following:
const[user]=usersif(!user)returnconst{id}=user// Does the user have any posts?if(!posts.some(({userId})=>userId===id))returnshowUserPost()
I guess I could name that boolean instead of writing the comment (or doing nothing at all) but I'm just being honest, and this best represents what I usually write at this time in my life.
And indeed, naming is hard (I didn't find your name descriptive enough).
Not sure which is better. Comments go stale easier than names, but this pollutes the scope.
Maybe I should wrap the const+if into a block for lexical scoping.
That would be wild. Ha. Ha-ha. Ha.
I see no issues with reading your code, plus what I provided in the article are only meant to serve as suggestions. My own examples could definitely be improved as you showed.
I do prefer adding a variable over a comment though, because as you said: comments become stale.
Variables become stale too :v
Don't tell me you have never seen something like
isValid.forEach(name(true))
They are easier to update though, since once someone finally realizes what that damn thing represents, they can apply a "rename identifier everywhere" refactoring tool.
I'd usually do the following:
I guess I could name that boolean instead of writing the comment (or doing nothing at all) but I'm just being honest, and this best represents what I usually write at this time in my life.
And indeed, naming is hard (I didn't find your name descriptive enough).
Not sure which is better. Comments go stale easier than names, but this pollutes the scope.
Maybe I should wrap the
const
+if
into a block for lexical scoping.That would be wild. Ha. Ha-ha. Ha.
I see no issues with reading your code, plus what I provided in the article are only meant to serve as suggestions. My own examples could definitely be improved as you showed.
I do prefer adding a variable over a comment though, because as you said: comments become stale.
But yeah, this stuff is hard sometimes 😅
Variables become stale too :v
Don't tell me you have never seen something like
They are easier to update though, since once someone finally realizes what that damn thing represents, they can apply a "rename identifier everywhere" refactoring tool.
Haha, yeah good point. I agree that they are easier to update.
I like to use comments as a last resort to explain why a piece of code is written in a certain way or exists in the first place.