Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
Ok so there are like 100 comments that I honestly won't read (I was eager to read the first 10 but got tired at 5).
What whas that? It's honesty. We all should practice it when coding, because we know what we are doing at the time of coding whatever but we should be honest and think that "My future me will love to see comments on this code".
"Yeah but there are functions that are SOLID and don't need comments because blah blah"
I had the exact same conversation with a colleague the other day so let's mimic it:
Is it straightforward, isn't it?
But how do you know what it returns?
"But Joel, it returns a user, A USER!"
Nope, it returns a Promise that hopefully will retrieve a user (as long as it exists in the DB).
And me and you, and the rest of the team will love having this information without the need of Ctrl+Click over the function name to see what is doing behind the scenes.
/**
* Retrieves a user from the DB based on the ID
* @param {number} id
* @returns {Promise<Object>} User
*/constgetUserById=async(id)=>awaitUser.findOne({where:{id:id}});
So when you are about to use this function you get:
Here it is! Inference! Beloved inference.
Moreover if you use TS pragma
// @ts-check
at the top of your JS file it will use TS when it's important, in dev time.
Without adding TS as project dependency plus without the extra bundle time of compiling/transpiling TS into JS.
It will use JSDoc to type-check your functions and variables. VSCode will handle it out of the box.
So if you try to do something like passing a string it will correctly complain like that:
Look how cool it looks like!
// @ts-checkimport{User}from'../db/models/users';/**
* Retrieves a user from the DB based on the ID
* @param {number} id
* @returns {Promise<Object>} User
*/constgetUserById=async(id)=>awaitUser.findOne({where:{id:id}});/**
* Gets a cookie value by name
* @param {string} name
* @returns {string}
*/exportconstgetCookie=(name)=>{constvalue=`; ${document.cookie}`;constparts=value.split(`; ${name}=`);if(parts.length===2)returnparts.pop().split(';').shift();};/**
* Retrieves the user ID from the browser cookies
* @returns {number}
*/constgetUserFromContext=()=>parseInt(getCookie('uid'));/** @type {User} */constcurrentUser=getUserById(getUserFromContext());
It looks even cooler in VSCode as it changes the color for the JSDoc when properly written:
Sooner or later I receive either a "you are right" or a "you were right" about that.
I had been in enough projects to say that this is a must.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Ok so there are like 100 comments that I honestly won't read (I was eager to read the first 10 but got tired at 5).
What whas that? It's honesty. We all should practice it when coding, because we know what we are doing at the time of coding whatever but we should be honest and think that "My future me will love to see comments on this code".
"Yeah but there are functions that are SOLID and don't need comments because blah blah"
I had the exact same conversation with a colleague the other day so let's mimic it:
Is it straightforward, isn't it?
But how do you know what it returns?
"But Joel, it returns a user, A USER!"
Nope, it returns a Promise that hopefully will retrieve a user (as long as it exists in the DB).
And me and you, and the rest of the team will love having this information without the need of Ctrl+Click over the function name to see what is doing behind the scenes.
So when you are about to use this function you get:
Here it is! Inference! Beloved inference.
Moreover if you use TS pragma
at the top of your JS file it will use TS when it's important, in dev time.
Without adding TS as project dependency plus without the extra bundle time of compiling/transpiling TS into JS.
It will use JSDoc to type-check your functions and variables. VSCode will handle it out of the box.
So if you try to do something like passing a string it will correctly complain like that:
Look how cool it looks like!
It looks even cooler in VSCode as it changes the color for the JSDoc when properly written:
Sooner or later I receive either a "you are right" or a "you were right" about that.
I had been in enough projects to say that this is a must.