Published at:
26 November 2020
Commenting is basically a way for someone to judge something, whether it's in terms of good or bad in the process, we will know whether we will improve or improve something, according to the comments given.
In our discussion this time, we will see how important it is to comment on a function that we have created ourselves, of course in JavaScript. Imagine we have a function like this
JS Code
03 LOC
1function setLocation(x, y) {2window.location.href = `${x}://${y}`;3}
Basically, how we read a function in JS just directly what function name that has been provided, in our case on above we named it as `setLocation`
we will conclude that function will set a new location, but what kind of location? what the purpose for? in below we will try to understang bit-by-bit to knowing better that function.
JS Code
01 LOC
1window.location.href = `${x}://${y}`;
Oh, turns out that function will be execute a location of URL at address bar browser something like that, with note we should fill in some arguments `x`
and `y`
.
What's The explanation of arguments `x`
and `y`
? whether we will can fill in `object`
or `string`
or maybe we can fill in some special input? another we use Typescript, the type data mechanism will be done by Typescript itself on runtime or maybe on development, but how about we use JavaScript?
Yes, in JavaScript (we will call it JS in the future) the arguments in the `setLocation`
function will be displayed by the intellisense editor as `any`
meaning whatever the data is will be allowed.
The problem is that the function we created will be read by 2 sources, namely; Computer and Human, when the code is read by the computer the code will continue to run no matter the _argument name we have set, but what about human?
The function we create will be difficult for the next developer (Human) to understand, because without knowing what to send to the `setLocation`
function, it's different if we leave a comment
JS Code
05 LOC
1// args `x` with data type string, is a protocol2// args `y` with data type string, is a domain name3function setLocation(x, y) {4window.location.href = `${x}://${y}`;5}
We will understand the code and we will use it according to the comments given, this is better than before, at least we understand what to send into the function we create.
We will try to comment on the function we created, according to the standard of JSDoc, we will change the above comment to something like this,
JS Code
08 LOC
1/**2* A function that can be executed on address bar browser3* @param {string} x a host url4* @param {string} y a domain url5*/6function setLocation(x, y) {7window.location.href = `${x}://${y}`;8}
The above function that we have modified the comments according to the JSDoc directive provided, this will be more aesthetic, and also intellisense in the editor will appear like using TypeScript.
Indeed, intellisense it's just can help use to determine what should be send in that function, when we want to use that, but it's not totally type strict
This is very helpful especially if we have a function where we should send a data object as a parameter of the function, for example we have a function like this
JS Code
12 LOC
1/**2* @param {object} fullName3* @param {string} fullName.firstName your first name4* @param {string} fullName.lastName your last name5*/6function overwriteData(fullName) {7return {8firstName: "Adib",9lastName: "Firman",10...fullName11};12}
When we want to use the `overwriteData`
function it will ask for a parameter with the contents of an object, instead of opening the original source of the function to find out what the object is, we have put a comment on the function, when we want to use it
Alike usual intellisense will give us a hint, and you know that the more insteresting for this approach is;
In the picture above we will enter data according to the instructions from intellisense of course we will get a hint from our editor, interesting isn't it?
The next question is, should we comment on all the functions that we have created? Of course not, an example of a function like this
JS Code
01 LOC
1const sum = (firstNum, secondNum) => firstNum + secondNum;
When we want to use the function, we already know from the given argument name, and from the function name it is `sum`
which only adds up two values.
Commenting on a function, depending on how it will be used are the given parameters very complex? or is it readble from naming function and arguments created?
But if you feel that the function needs to be given a comment, that's better, because it is to make it easier to maintain by the next developer (Human).
Thank you.