0

Dose this two function have a name or a term? I'm sure they do but don't know what to search for.

// edit: thanks to Bergi: 
// this is a function with `no free variables` (outer variables)

function add (a, b) { return a + b }

and

// edit: thanks to Bergi: 
// this is a function with `free variables` (b) if you didn't know that

var b = 2
function add (a) { return a + b }

Is it called something special if a function goes outside of its own scope? and what do you call the function that don't use anything outside of its own scope?


What I'm would like to explain to someone is if I where to write this utility file that exported two helper function

// util
function divide (a, b) {
  return a / b
}

function half (a) {
  return divide(a, 2)
}

export {
  divide,
  half
}

And then in another file I would only use the divide function

import { divide } from util

Then WebPack/rollup would be able to tree shake and discard the half function in the final bundle

But if where to import the half function from the utility file

import { half } from util

But don't really need the divide function in my main script I would end up having both function in my bundle anyway because half depends on divide

If I have would have changed the function to not use any variables outside of it's own scope like so:

function half (a) {
  return a / 2
}

then it would be "dependency" free.

Endless
  • 34,080
  • 13
  • 108
  • 131
  • Well, a function that only uses variables inside its own scope is *one* of the qualifiers for a "pure function" – CertainPerformance Sep 02 '18 at 19:43
  • I don't understand, is this a question about a function type naming or a question about the performance and tree shaking? – smnbbrv Sep 02 '18 at 19:54
  • Is it also pure if it's a function references itself (recursive)? eg if `a` is negative `if(a < 0) return half(a*-1)` – Endless Sep 02 '18 at 19:55
  • the question is about naming – Endless Sep 02 '18 at 19:55
  • 1
    IMO. Write code thats easy to understand / maintain, not code that might make tree shaking performant. You might even find the second one may even get inlined.. – Keith Sep 02 '18 at 19:58
  • 1
    @CertainPerformance Sure, a function that uses only local variables (and doesn't modify mutable arguments) is a pure function, but not every pure function uses only local variables. – Bergi Sep 02 '18 at 19:59
  • 1
    I would call `divide` a *dependency* of `half`. I don't think there's a term for a function that uses nothing else - almost every function does that, especially if you count the native operators and the global language builtins. (If it really doesn't, you'd call it a *primitive*, but I don't think that's what you are looking for) – Bergi Sep 02 '18 at 20:00
  • I thought pure meant that you pass in arguments to a function and get a new value back - not that it use any other variables outside of it's own scope... `array.sort()` returns the array but it also modifies the original array - therefore `sort` is not pure? meaning a pure function should be immutable? or I'm wrong? – Endless Sep 02 '18 at 20:02
  • the opposite of a *pure function* is an *impure function* ? :) – Jonas Wilms Sep 02 '18 at 20:06
  • @Bergi so what you are saying `half` is also pure? – Endless Sep 02 '18 at 20:09
  • 2
    @Endless A [pure function](https://en.wikipedia.org/wiki/Pure_function) does not mutate anything and its result does not depend on anything mutable. Yes, your `half` and `divide` function would both qualify as such, and `sort` would not, but as I said this term is not what you are looking for. – Bergi Sep 02 '18 at 20:15
  • purity has nothing to do here. Also, the performance thing is very much arguable. I'm not really sure you'll get any real performance with this – smnbbrv Sep 02 '18 at 20:16
  • This, is just a VERY basic simple example. Every time a function has to look for something else outside of the scope it has to look for that in the upper scope, if not go to the next one and so forth – Endless Sep 02 '18 at 20:19
  • @Keith I get that you should write understandable code and i do that all the time, but sometime you have to think of what the cost of writing large libraries, lodash for example, they have a hole bunch, but you might only use 2 functions from lodash in you hole project – Endless Sep 02 '18 at 20:26
  • Just in case you never knew, `lodash` can also be used modular mode,..eg. If you just wanted `at` you could do `const at = require('lodash/at');` That will help keep your rollup / webpack builds smaller. – Keith Sep 03 '18 at 05:49

1 Answers1

1

Is it called something special if a function goes outside of its own scope?

We call them closures. It has a free variable, which in JS is looked up lexically in the scope.

However, while technically every function is a closure in JavaScript, this is probably not the term you want to use; most people associate it to dynamically created functions which would be weird for a module-level function.

What do you call the function that don't use anything outside of its own scope?

A "function without free variables" would fit.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I think I finally got my answer – Endless Sep 02 '18 at 20:31
  • can you just quickly tell me if this [fiddle](http://jsfiddle.net/nv1xrap0/1/) is "closure" or a "function without free variables" or is it called recursive? – Endless Sep 02 '18 at 20:35
  • @Endless Complicated. You could say that it doesn't have a free variable, but technically it is equivalent to `var get = function() { … get() … }` where you can see better that `get` is not defined *inside* the function (it's not a part of it) but rather a closed-over variable from the outer scope. – Bergi Sep 02 '18 at 20:41
  • Thanks for trying, I will accept your answer, got a nice debate about this. Now i know there is something called "Free variables" so i learned something new today :) – Endless Sep 02 '18 at 20:44