Function names


#1


https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/0/2?curriculum_id=506324b3a7dffd00020bf661


I'm not stuck, this is just a general knowledge question.

Why are functions named after the variable they are declared with, and not the parameter(s) they carry? For example, why isn't this function called 'number'?


#2

Functions are best described with names the tell us what they do. In other words, names are purpose based. The same can be said of variable names. They too should describe their purpose or role in the program (or function).

Say we wrote an identity function like so,

var number = function (number) {
    return number;
};

In truth we wouldn't write it exactly this way since there would be conflict with variable names from a reading standpoint, at least. Technically the two variables are in different scopes, so won't actually collide, but our code would get very confusing and difficult to debug; that much is certain.

Often we include a role in the name:

getNewName()
get_something()

get implies new inputs and that our function is going to return a new value which we will use to update some state in our program.

isPrime(number)
is_even(number)

is implies a state which is to be tested and a Boolean value returned.

This is not a hardline approach, but an arbitrary one that we can use to make our code more easily readable and understandable.

var divideByThree = function (number) {
    return number / 3;
};

Notice how the function name describes what the function does, and to what? We expect any number we pass it to be returned 1/3 of its original size.