this post was submitted on 06 Nov 2023
1 points (100.0% liked)

Programmer Humor

35244 readers
36 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 0 points 1 year ago* (last edited 1 year ago)

The point is that this scenario exists in Js in the first place. It's a completely unnecessary rake left around for people to step on. Also, the function isn't side effecty since it doesn't make implicit references outside its scope. The fact that the date is mutable is an internal concern there. You could just as easily do

function getMonthName(monthNumber) {
  const date = new Date();
  date.setDate(1);  
  date.setMonth(monthNumber - 1);

  return date.toLocaleString([], { month: 'long' });
}

The problem here isn't with side effects, but with having to know that you want to set your date to first day to get the next month reliably.