#poll
What do you prefer to read:
A:
function foo() { return 1; }
var foo = foo();
or
B:
var foo = foo();
function foo() { return 1; }
Please comment.
#poll
What do you prefer to read:
A:
function foo() { return 1; }
var foo = foo();
or
B:
var foo = foo();
function foo() { return 1; }
Please comment.
B, but with a different var name. I agree with Qantas regarding that. I like to see the most summarized code (function call) and then investigate the function elsewhere if I need to. To me, "A" would be like reading through the jQuery library before calling .hide()
. If I want to know what .hide()
does, I'll look for it and read it. In that light, maybe that function could be in it's own library that is required in and then it doesn't have to be above or below :)
@Qantas94Heavy you are losing scope of the question i'm afraid.
A; the order of declarations let's me know that function foo(){...}
is no longer accessible and var foo
has overwritten it
Neither, it's a bad idea to use the same variable name for both a function and its result. If you're only using it once, use an IIFE instead.