33
loading...
This website collects cookies to deliver better user experience
Code is read more often than it is written
“Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. ...[Therefore,] making it easy to read makes it easier to write.”
Oh.. I don't know. I did not write that part. I was just asked to add a new functionality, or a new validation, or a new error handler, and that I did.
"We are constantly reading old code as part of the effort to write new code.
const validateDate = (dateString) => {
// some logic to parse and validate the string and extract the date parts.
const day = // value extracted from dateString
const month = // value extracted from dateString
const year = // value extracted from dateString
const date = new Date(year, month, day)
// lots of checks and conditions
// final fall through validation of the created date
return (
date.getFullYear() === year &&
date.getMonth() === month &&
date.getDate() === day
)
const date = new Date(year, month, day)
return date.getFullYear() === year &&
date.getMonth() === month &&
date.getDate() === day
Yes, we are making sure that the date we created with day, month and year has the correct day, month and year.
Yeah.. I get that. I can read code.
new Date(2001, null, 5) // --> Jan 05 2001
new Date(undefined, 2, 12) // --> Invalid Date {}
new Date(2008, 1, false) // --> Jan 31 2008
new Date(2008, 1, "3") // --> Feb 03 2008
new Date(2008, 1, "nope") // --> Invalid Date {}
45.16.2009
. The regex would have properly retrieved the 3 date parts and passed them to the Date constructor.new Date(2009,16,45)
?new Date(2009,16,45) // --> Mon Jun 14 2010