Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Elsewhere: an argument has arisen over the terminology of a named function in CoffeeScript. In particular somebody refereed to something like this

 foo = ->
    console.log("bar")

as a named function. But its been objected that everything in CoffeeScript is anonymous functions and there are no named functions. This is certainly true, CoffeeScript only has function expressions which can then be stored in a variable. But I don't think that means it is wrong to call this a named function.

As I see it, it is a named function because its a function that has been given a name. True, its not a named function in the same way that some other languages have named functions, but I think its close enough that its not inappropriate to call it a named function. To insist otherwise just seems to be nitpicking.

Am I out to lunch in thinking that insisting that this isn't a named function is just nitpicking?

share|improve this question
2  
Isn't this whole question just, well, nitpicking? :-) –  Mat Mar 19 '13 at 19:09
    
@Mat, yes it seems I can't avoid nitpicking about nitpicking –  Winston Ewert Mar 19 '13 at 19:21
    
For the small pool of programmers that I talk to (outside of programmers.SE that is), they mostly say to use JavaScript's named functions for use as "classes" (constructors), while anonymous functions stored in variables for plain old functions. –  skizeey Mar 19 '13 at 19:28
    
"Just nitpicking" implies the answer doesn't matter, and that understanding the subtleties of a language isn't a worthy goal. –  meagar Mar 19 '13 at 19:31
    
I could look at it analogously to CoffeeScript: foo = -> is just a plain old function, whilst class Foo is a constructor. I see no reason why foo = -> should strictly be called anonymous. –  skizeey Mar 19 '13 at 19:31
show 2 more comments

6 Answers 6

CoffeeScript is inexorably tied to JavaScript, and JavaScript differentiates between the following expressions:

function foo() { ... }
var foo = function () { ... }

In fact, you can even write:

var foo = function bar () { ... }

Since this difference matters in JavaScript, it makes sense to use the same terms when talking about CoffeeScript. However, CoffeeScript does not support anything like the function foo () syntax, so we can say it does not have "named" functions.

In a sense, the name is part of the function definition in the function foo() { ... }, where in the other case you just create a function and assign it to a variable. This is reflected, for example, in the (nonstandard) name property of functions: in the first case, foo.name will give you "foo" and in the second it will give you "".

Additionally, in JavaScript, these also differ in terms of how they get introduced to the scope: the first version is "hoisted" and available throughout its scope where the second definition is only available after it's assigned.

Basically, just think of it as JavaScript-specific jargon, which is transferred to CoffeeScript because CoffeeScript is so closely related to JS.

share|improve this answer
    
It's true that there isn't such a thing as a function foo () {}. However, you can still initialize a named function via the class construct. Just that the compiled CoffeeScript (the resulting JavaScript) is much more verbose than how most would write a named function. –  skizeey Mar 19 '13 at 19:23
    
And also, there's a technical caveat: your foo function's body will not be hoisted. –  skizeey Mar 19 '13 at 19:25
    
@skizeey: Good points, thanks. –  Tikhon Jelvis Mar 19 '13 at 19:27
    
jelivs: no problem. One correction from the last comment I wrote on your answer: your class foo's function body will not be hoisted to the top of the file. –  skizeey Mar 19 '13 at 19:33
    
Since CoffeeScript doesn't make the distinction between named and anonymous functions, can we really say that the terminology is transfered to CoffeeScript? Javascript's distinction simply doesn't mean anything in that language. –  Winston Ewert Mar 19 '13 at 19:39
show 2 more comments

Am I out to lunch in thinking that insisting that this isn't a named function is just nitpicking?

No. After all, in terms of semantics, your function reference is stored in a variable, which you can refer to via a variable name.

share|improve this answer
add comment

It's worth noting that the user explicitly stated they were turning an "anonymous function into a named function", both terms having a strong, existing meanings and notably different functionalities in in the JavaScript world. Given the existing meaning, the were doing no such thing, and I pointed this out.

CoffeeScript is not so far removed from JavaScript that you should be redefining terms they both share to mean something else in one language. CoffeeScript doesn't exist in some bubble, removed from its JavaScript implementation the way you might argue that C++ is separated from Assembly. Knowing the difference between an anonymous function and a named function matters, because if you expect your "named" CoffeeScript function to behave like an actual named function, you're going to be disappointed:

doStuff() # I cause an error

# ... later

doStuff = (x,y) ->
  alert("Were I actually a named function, this would work!")

The equivalent JavaScript would work fine, with a real named function:

doStuff(); // I work just fine!

// later....

function doStuff() {
  alert("I'm a real named function!")
}

You might be right that I'm just "nitpicking", but so what? The subtle points in computer programming matter, and being "technically" correct is important. It's the difference between writing code that happens to work, and actually understanding why your code is correct.

share|improve this answer
1  
If I tried your example in Python, for example, it still wouldn't work. So I'm not sure that has anything to do with named vs anonymous functions. –  Winston Ewert Mar 19 '13 at 19:24
1  
@WinstonEwert See my update. Python really doesn't have anything to do with it... –  meagar Mar 19 '13 at 19:26
    
@meager, my point is that named functions don't necessarily act that way, although they do in Javascript. Therefore, hoisting itself doesn't disqualify CoffeeScript's functions from being considered named. –  Winston Ewert Mar 19 '13 at 19:35
    
Yes, you have to understand that all functions in CoffeeScript are anonymous (actually, I'd prefer to say that they are all function expressions). But that doesn't mean that we can't be a bit loose with terminology sometimes. That's why I think its nitpicking to insist that you can't ever call them named functions. –  Winston Ewert Mar 19 '13 at 19:38
add comment

Definitely not a nitpick, imo. I use it extensively for readability:

readfile()
dothis()
dothat()
thistoo()
writefile()

function readfile() {
    ...
}
...

Thus:

Real named function in "coffeescript"

hello()

`function hello() {`
console.log 'hello'
dothings()
`}`

You escape pure JS via the backtick `

Note that you can't indent on your function body.

Cheers

share|improve this answer
add comment

Don't waste time arguing with pedants. It's never fruitful. Yes, everyone knows what you mean by a "named function" in CoffeeScript, even if it's technically incorrect. No, using technically incorrect but widely understood terminology has no bearing on the correctness or incorrectness of the proposed solution. Is it possible to be more precise without gaining some awkwardness of phrasing? Probably not. However, that doesn't mean people will let it slide. Just imagine this guy on the other end of the conversation.

The reason it's technically incorrect is because you haven't named the function, you've named a reference to the function. Consider:

foo = bar = ->
  console.log "What's my name?"

What's the function's name? foo and bar are both referencing the same function, but have different names. Also, either foo or bar could be reassigned at any time to reference a different function, or even a different type altogether, without changing the function itself.

share|improve this answer
add comment

So, the way I read this is like this:

When you declare a function in a JavaScript console using

var foo = function() { ... }

and then invoke foo without parentheses, it returns

function() { ... }

However, if you define it using

function foo() { ... }

and then invoke foo again without parentheses, it returns

function foo() { ... }

In the first case, I'd say you're declaring a variable named "foo" storing an anonymous function in it. In the second case, I'd say you're actually declaring a named function called "foo".

Because CoffeeScript uses the first pattern, I'd agree that it's technically correct that CoffeeScript's functions are all anonymous functions being stored in named variables.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.