ChatGPTAPIFree icon indicating copy to clipboard operation
ChatGPTAPIFree copied to clipboard

Updated if condition for boolean check on stream

Open parangeomkar opened this issue 1 year ago • 4 comments

Hi Ayaka! Suggesting a simple readability improvement, instead of null and true/false check on stream variable on line 44, used typeof check. Did not understand the if condition until I read the response sent from there.

Regards, O.P.

parangeomkar avatar Mar 16 '23 06:03 parangeomkar

Correct me if I am wrong, but if stream is neither true nor false, how can it be boolean?

HarshNarayanJha avatar Mar 17 '23 02:03 HarshNarayanJha

HarshNarayanJha, thanks for correcting me, I have update the condition to != "boolean".

parangeomkar avatar Mar 17 '23 03:03 parangeomkar

These two conditions are not equivalent.

The first condition checks if the variable stream is not equal to null, true, or false. This means that the condition will evaluate to true if the stream variable is undefined, an object, a string, a number, or any other value that is not strictly equal to true or false.

The second condition checks if the typeof the stream variable is not equal to "boolean". This means that the condition will evaluate to true if the stream variable is not a boolean value, but it will evaluate to false if the stream variable is undefined or null.

Here's an example to illustrate the difference:

let stream;
console.log(stream != null && stream !== true && stream !== false); // false
console.log(typeof(stream) != "boolean"); // true

In this example, the stream variable is undefined. The first condition evaluates to false because stream is equal to null, and the second and third parts of the condition are not evaluated. The second condition evaluates to true because typeof(stream) is "undefined", which is not equal to "boolean".

ayaka14732 avatar Mar 17 '23 04:03 ayaka14732

Hi Ayaka, Thanks for clarification. As per the response sent from inside the if statement, 'The stream parameter must be a boolean value' suggests that stream is either true or false. Also, the code suggests that it is expecting to receive stream from client in req.body, however, even if it is not received, the sever will accept undefined as valid argument. I see no problem in adding a null check here, but in my experience it is a bad practice to give the client this kind of freedom. So, let me know if you wish to continue with accepting undefined stream as a valid argument, I will add the null check or may be you can close this pull request as this change was only suggested for readability improvement.

parangeomkar avatar Mar 17 '23 06:03 parangeomkar