最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Best practice for returning a variable that exists in global scope? - Stack Overflow

programmeradmin2浏览0评论

Is the return statement in here unnecessary?

var fahrenheit;
var celsius;
function cToFConvert () {
       celsius = temperatureInput.value;
       fahrenheit = celsius * (9/5) +32;
       console.log(fahrenheit);
       return fahrenheit;
}

I can get the fahrenheit value even if I do not use the return statement. Does that mean using a return is redundant if the variable was declared in global scope?

Is the return statement in here unnecessary?

var fahrenheit;
var celsius;
function cToFConvert () {
       celsius = temperatureInput.value;
       fahrenheit = celsius * (9/5) +32;
       console.log(fahrenheit);
       return fahrenheit;
}

I can get the fahrenheit value even if I do not use the return statement. Does that mean using a return is redundant if the variable was declared in global scope?

Share Improve this question edited Jun 16, 2016 at 7:38 Bar Akiva asked Jun 16, 2016 at 7:31 Bar AkivaBar Akiva 1,2293 gold badges16 silver badges27 bronze badges 4
  • 3 Yes it is redundant. However, it is much better to create methods that are atomic and only use their own scope instead. – str Commented Jun 16, 2016 at 7:32
  • 3 var = farenheit; should be var farenheit; – Nina Scholz Commented Jun 16, 2016 at 7:34
  • yeah sorry, did not copy the code correctly. Edited. @str what does it mean "atomic" in programming? I have yet to encounter the term. – Bar Akiva Commented Jun 16, 2016 at 7:39
  • See the answer of Suren Srapyan. – str Commented Jun 16, 2016 at 7:51
Add a ment  | 

6 Answers 6

Reset to default 6

Function and pure function.

The concept to keep a function pure is the idea to use the function without side effects. This means, the function should rely on own parameters and return something which is related to the parameters only.

That means a function should have some input and and some output.

In this case for converting a temperature, you have a value Celsius and want to get the value in Farenheit. This is a good example how to write a function which is resusable for any purpose and could be inserted into a library without changes.

How does it work?

You may think of an input and an output based on the input.

function convertCelsiusToFarenheit(celsius) {
    return celsius * 9 / 5 + 32;
}

Now you can use the function with a wanted input and store the output to a variable

var myFarenheit = convertCelsiusToFarenheit(temperatureInput.value);

Or if you like to convert a bunch of values, you could use the function as callback

var myFarenheitData = [-10, 0, 10, 20, 30, 40].map(convertCelsiusToFarenheit);

With this in mind, its is easier to write a multipurpose function.

DON'T USE GLOBAL VARIABLES.

var farenheit;
var celsius;
function cToFConvert () {
       celsius = temperatureInput.value;
       farenheit = celsius * (9/5) +32;
       console.log(farenheit);
       return farenheit;
}

You can remove the return statement. No it is not necessary. Why you use global variable, you will have access to it anywhere and can change it's value anywhere.

For alternate you can do

function cToFConvert(celsiusValue){
  return celsiusValue* (9/5) +32;
}

And call like

var fahrenheit = cToFConvert(temperatureInput.value);

Avoid working with variables outside of the method, that would be my suggestion. Pass the variables to the method as parameters and return the result. If you still want to work with the variables outside the method, then the return is redundant because you're already modifying the variable inside the method.

function cToFConvert(celsius) {
  return celsius * (9 / 5) + 32;
}
var celsius = 123; //temperatureInput.value;
var farenheit = cToFConvert(celsius);

console.log(farenheit);

Here I think is reductant, but I think this would not be a valid implementation either. Instead you can do:

var cToFConvert = function() {
var celsius = temperatureInput.value;
var farenheit = celsius * (9/5) +32;
console.log(farenheit);
return farenheit;
}

and then

var convertedValue = cToFConvert();

Hope this helps!

Though it is redundant here, both of the approaches differ.

Case 1: You do not use return.

Here, the calling method doesn't expect a value.

Eg. var y=cToFConvert();

`y` will be `undefined`.

Case 2: When you use return.

The calling method expects a value to be returned.

Eg. var y=cToFConvert();

y will have the value farenheit.

Over the two, I personally prefer Case 1, because by looking at the function call, you can clearly say what the function returns. Also as mentioned in other answer, do not use Global variables unless there is no way out.

var farenheit=0, celsius=13; //definded in global space
function cToFConvert () {
       farenheit = celsius * (9/5) +32; //using global variables, no need to return
}
function checkFarenheit(){
  alert(farenheit); //checking global variable.
}
cToFConvert();
checkFarenheit();
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Note: this is not at all good practice as it will be more error prone, use promises or callbacks methods.

发布评论

评论列表(0)

  1. 暂无评论