2

What is the best practice in javascript when a function is supposed to return true or false? Can I return falsy values directly instead of true or false? I found this code:

function supportsAPL(handlerInput) {
  const supportedInterfaces = handlerInput.requestEnvelope.context.System.device.supportedInterfaces;
  const aplInterface = supportedInterfaces['Alexa.Presentation.APL'];
  return aplInterface != null && aplInterface !== undefined;
}

and simplified it to this code:

function supportsAPL(handlerInput) {
   const {supportedInterfaces} = handlerInput.requestEnvelope.context.System.device;
   return supportedInterfaces['Alexa.Presentation.APL'];
}

which works but I'm not sure this is proper/nice javascript. I'm looking for what an experienced javascript developer would write after finding the 1st code snippet (also looking to save lines of code).

2
  • 3
    You could coerce the value into a real Boolean with return !!supportedInterfaces['Alexa.Presentation.APL'] Commented Aug 5, 2019 at 23:47
  • 1
    It totally depends upon what the caller of the function expects or what the doc says the function will return. If the function documents that it returns a Boolean, then it should return true or false. If the function documents that it returns a any truthy or falsey value, then it can do that and the caller should act accordingly. If I were designing an API, whose main job was to deliver a Boolean result, I'd make sure it returned an actual Boolean (true or false) - just less chance for anyone to ever get confused at minimal cost to make that happen. Commented Aug 6, 2019 at 3:34

1 Answer 1

4

I think the 'best practice' is to always return what the caller's are going to use it for. So, in this case, the function is named supportsAPL which seems like it should return a yes / no (true / false) to let the caller know that whatever input you gave the function supports APL or not.

You mentioned that you simplified this:

return aplInterface != null && aplInterface !== undefined;

to be this:

return supportedInterfaces['Alexa.Presentation.APL'];

In this case, we went from returning a specific true / false to returning whatever the value of supportedInterfaces['Alexa.Presentation.APL']; is. If APL is supported, you're going to get the value of supportedInterfaces['Alexa.Presentation.APL']; whereas if it's not supported, you'll likely get a falsy value of undefined

More than likely, the caller is going to be doing something like this:

if (supportsAPL(input)) {
    ...
}

or

const aplSupported = supportsAPL(input);
if (aplSupported) {
    ....
}

But, if you are only returning truthy falsy, you're going to break anybody who was expecting a boolean return. So these won't work:

if (supportsAPL(input) === true) {
    ...
}

or

const aplSupported = supportsAPL(input);
if (aplSupported === true) {
    ....
}

In my opinion, always return a boolean in these scenarios since that's the main point of the function (to determine if whatever the input is supports APL).

As @Phil mentioned,

return aplInterface != null && aplInterface !== undefined;

can be simplified to this:

return !!supportedInterfaces['Alexa.Presentation.APL']
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.