Published on

How to Overthink a Simple Problem as a Programmer

Authors

Introduction

Today, I will share a story of a time when I solved a problem and then realized that I overthought the problem. I wasted time (and internet bandwidth) trying to solve a problem that almost didn't exist.

The Problem

About a month ago, I wanted to add a last modified field lastmod to the sitemap for Studymono—an education website I am building. So I wanted to turn the sitemap from this:

sitemap.xml
<url>
    <loc>https://www.studymono.com/demo-slug</loc>
</url>

to this:

sitemap.xml
<url>
    <loc>https://www.studymono.com/demo-slug</loc>
    <lastmod>2020-11-28</lastmod>
</url>

Looks easy to do right?

The problem, however, was that the source from which I generated the fields in the sitemap had the last modified dates in the W3C Datetime format with hours and minutes. So they looked like this 2021-28-12T12:22:88. Although it was valid to be placed like that in the lastmod field of the sitemap, I preferred to strip off the hour and minutes part from the timestamp.

My Initial Attempt

To do this, I created a getLastMod function that accepts a W3C Datetime and returns the same date with the time stripped off.

After overthinking how to do this and taking some time to read about the properties of the Date object in Javascript, I came up with a solution of using a template literal to join the year, month, and date.

And this is the code I had that solved the problem:

const getLastMod = (date) => {
  const d = new Date(date)
  const pad = (num) => {
    if (num < 10) {
      return `0${num}`
    } else {
      return `${num}`
    }
  }
  return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
}

I created a pad function that accepts a 1 or 2-digit number and returns a 2-character string padded with zeroes if it is a one-digit number and returns the number otherwise. The function returned a template literal string that joins the year, month, and day.

You might have noticed that I added 1 to the value that is returned from d.getMonth. That was intentional. I figured out that d.getMonth returns 0 for January, 1 for February, 2 for March, etc.

Everything worked fine with the function. But right before committing changes to Git, I discovered there was a way for me to do what I wanted without resolving to backticks or invoking the Date object.

The Realization

I figured out that I could get what I needed directly from the string without making use of Date or template literal strings or anything like that. I saw that to strip off the time from the Datetime string, I just needed to get the first ten characters of the original string.

This is the getLastMod I ended up with after the amendments:

const getLastMod = (date) => date.slice(0, 10)

I had a single-line function that did just what the 12-lines of code did. This made me start wondering if I needed to abstract this function or just put it inline where it is needed.

Conclusion

That is the story of me overthinking a simple problem. I realized the need to always go back to review previously written code to see possible ways of smoothening things up. Just as much as I believe that things should be done as quickly as possible, I believe that code revision before committing should not be overlooked, as it has a big role to play in maintaining performance and keeping code clean (and green).