1
var input = document.getElementById("input").value;

function sendRequest {
  if (Validate.isNumber(input)) {
    input.focus();
    return;
  }

  // or

  if (Validate.isNumber(input)) {
    input.focus();
    return null;
  }

  // or

  if (Validate.isNumber(input)) {
    return input.focus();
  }

  // start Ajax call
  var xhttp = new XMLHttpRequest();
  ...
}

Above 3 function have same result (if validation fail ends the function execution)... So what is the difference? Which one is better?

Buggy
  • 3,539
  • 1
  • 21
  • 38
RaxPat
  • 311
  • 1
  • 11
  • What happens if `input` is not a number? – Ori Drori Sep 30 '18 at 18:19
  • @OriDrori start the Ajax call – RaxPat Sep 30 '18 at 18:20
  • And what does it return? – Ori Drori Sep 30 '18 at 18:20
  • @OriDrori No it just send the value to server and save it in the database. – RaxPat Sep 30 '18 at 18:20
  • I would return a promise from the ajax call, and Promise.reject() (with some to show why it was rejected) if the input is a number. In this way you can react to success and failures (server or check). – Ori Drori Sep 30 '18 at 18:22
  • `input` appears to be a string in your code (the value of the input element). It does not have a `.focus()` method? – Bergi Sep 30 '18 at 18:26
  • Why would you want to `return null;`? The usual `return;` results in `undefined`, which should be fine. Also, what does the function usually return if the validation doesn't fail? – Bergi Sep 30 '18 at 18:27
  • @OriDrori Noo, I am not asking about returning a promise or not, it's just a DOM input field focus(), wondering what is the difference – RaxPat Sep 30 '18 at 18:27
  • I know. If the return value means something - return a promise to cover all cases. If not, just return to terminate the function. – Ori Drori Sep 30 '18 at 18:30
  • @Bergi if the input passed the validation, then just a simple ajax call and send it over to server (No response). – RaxPat Sep 30 '18 at 18:30
  • @OriDrori So what is the difference in above 3 ways to terminate a function execution? – RaxPat Sep 30 '18 at 18:32
  • 1
    You're not doing anything with the result of function, so it doesn't matter. Just return. – Ori Drori Sep 30 '18 at 18:34
  • @RaxPat If you're not returning anything in the case of doing the ajax call, then you shouldn't return anything in the case of an error either. – Bergi Sep 30 '18 at 18:37

1 Answers1

0

EsLint have a good rule: consistent-return

Require return statements to either always or never specify values. If any code paths in a function return a value explicitly but some code path do not return a value explicitly, it might be a typing mistake, especially in a large function

Airbnb JavaScript Style Guide

// No implicit return with side effects
function foo(callback) {
  const val = callback();
  if (val === true) {
    // Do something if callback returns true
  }
}

So what should we return for nothing?

In JavaScript, undefined means a variable has been declared but has not yet been assigned a value.

null is an assignment value. It can be assigned to a variable as a representation of no value.

null vs undefined

TLDR: return null.

PS also take a look at Null object pattern. If success path return Promise then negative path can return Promise.resolve(null) - if consumer of the function already check value inside Promise and doesn't care why it empty.

Buggy
  • 3,539
  • 1
  • 21
  • 38