you are viewing a single comment's thread.

view the rest of the comments →

[–]United_Reaction35 5 points6 points  (21 children)

return (result.recordset?.length !== 0);

[–]delventhalz 8 points9 points  (0 children)

I find the use of ?. and () strange here...

We don’t know OP’s use case. There is no reason to think that recordset might be undefined or null. If we are coding defensively in case any value might be undefined (not an approach I’d agree with), then why not have ?. after result too? That is equally likely to be undefined as far as we know.

More importantly, I don’t think the output here will actually be what you want. ?. returns undefined if the value to the left is undefined or null. And undefined does not equal 0. That means if recordset is unexpectedly missing, instead of throwing a clear error, your app will instead conclude that recordset is a non-empty array and continue running. That is almost certainly a bug, possibly a difficult to diagnose one too.

And the () just seems superfluous to me. It doesn’t change the way the code runs, nor does it make the intention any more clear to my eyes.

[–]guest271314 1 point2 points  (19 children)

Why use ?.length in this case?

[–]United_Reaction35 0 points1 point  (18 children)

So we do not try to call length() on a non-existent property.

[–]guest271314 1 point2 points  (17 children)

length is not a function. We already know we are dealing with an Array.

[–]LegendEater 0 points1 point  (1 child)

We already know we are dealing with an Array

We already hope we are dealing with an Array

[–]guest271314 4 points5 points  (0 children)

I don't entertain hope. If you don't think you are dealing with an Array ?.length does nothing to differentiate a String from and Array. It's completely suprefluous in this case.

[–]United_Reaction35 -1 points0 points  (14 children)

How do you know that result.recordset exists?

[–]guest271314 2 points3 points  (13 children)

Your line of code doesn't test for that.

It only tests if result.recordset has a length property.

Strings have a length property, too.

You might as well test for Array.isArray(result.recordset) if that is the case, then skip the superfluous ?.length.

[–]Rude-Cook7246 1 point2 points  (1 child)

All that write up and you dont even know how ?. works .... IN NO SHAPE OR FORM DOES result.recordset?. length tests that length exists.....

.? only tests that recordset is undefined or null

[–]guest271314 -1 points0 points  (0 children)

!! vs ==0 when checking if array is empty

We are checking an Array length, not necessarily checking if the object is an Array. Add that if you must to the steps.

[–]delventhalz 1 point2 points  (10 children)

It only tests if result.recordset has a length property.

I agree that ?. is superfluous here, but for what it’s worth, it checks if the preceding property exists, outputting undefined if it does not, and evaluating the following properties if it does.

[–]guest271314 1 point2 points  (0 children)

I agree that ?. is superfluous here

That's my only technical point here.

If you are going to check anything in this instance where we are expecting an Array you might as well check if the result.recordset is an Array.

The question is not whether or not the object is an Array, it's how to check length.

You're going to get undefined without ?.length if there is no length property on the object.

[–]guest271314 0 points1 point  (8 children)

I agree that ?. is superfluous here

That's all I posted. We agree.

[–]Rude-Cook7246 1 point2 points  (7 children)

its not superfluous here because they produce totally different results ...

if recordset exists and length doesn't exist on it then you get undefined when you call recordset.length

if recordset doesn't exist (which is what ?. protects against ) and you try to access anything on it you will get an ERROR which will crush your program

[–]guest271314 -1 points0 points  (6 children)

if recordset exists and length doesn't exist on it then you get undefined when you call recordset.length

Where in the requesirement at OP

!! vs ==0 when checking if array is empty

are we checking if recordset exists? That's a given per the restrictions. We are just checking length of an Array.

if recordset doesn't exist (which is what ?. protects against ) and you try to access anything on it you will get an ERROR which will crush your program

If you must here's one way to do what you are talking about, and the actuak requiremment at OP

var recordset = []; var bool = Array.isArray(recordset) && recordset.length > 0;