Code Refactor - Better way to write
key points
⦿ Chances of sending wrong value to the function is more in sendEmail
⦿ Refactor as refactoredSendEmail. Force the calling function to pass the parameters(to,body,subject). And destructure in the actual method (refactoredSendEmail)
⦿ Refactor as refactoredSendEmail. Force the calling function to pass the parameters(to,body,subject). And destructure in the actual method (refactoredSendEmail)
const sendEmail = (to, subject, body) => {
if (to) {
//send email
} else {
//some error message
}
};
//chances of sending wrong value is more as below .
//we are sending email address in subject....
sendEmail("teaching js", "anurag.nayak@hotmail.com", "still novice");
bject: "still novice",
});
//Refactored Code
const refactoredSendEmail = ({ to, subject, body }) => {
if (!to) {
//error message
}
//sendEmail
};
refactoredSendEmail({
body: "improvement",
to: "anurag.nayak@hotmail.com",
subject: "still novice",
});