question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Simplify LocalDate(Time) constructor parameter names

See original GitHub issue

The parameter names are very weird for day-to-day use:

LocalDateTime(year = 2021, monthNumber = 1, dayOfMonth = 1, hour = 12)

I don’t care that month is a number. That’s what we have a type system for. OfMonth for day is also redundant as that’s obvious from the context.

https://github.com/Kotlin/kotlinx-datetime/blob/3af71d2e874592fc70282de441a09d024dcefb54/core/common/src/LocalDateTime.kt#L50

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ilya-gcommented, Jul 26, 2022

I believe that numbering months from zero is an abomination and should not be considered a normal practice that we take into account when naming the corresponding parameter/property.

1reaction
dkhalanskyjbcommented, Jul 26, 2022

You make a good point about dayOfMonth. I agree, nobody will be confused about the meaning of LocalDateTime.day.

Regarding monthNumber, Month is a nice, well-typed, unambiguous thing. monthNumber: Int, on the other hand, is not. Unfortunately, many systems use zero-based numbering for months, even while using one-based numbering for days. JavaScript comes to mind:

new Date(2020, 1, 1) Date Sat Feb 01 2020 00:00:00 GMT+0100 (Central European Standard Time)

So, it makes sense for LocalDate(Time)? to highlight an area that would give potentially misleading results by using a longer name. “monthNumber? What kind of a number? The usual kind, 1…12, or zero-based?” If we manage to provoke a question of this kind, we have won.

In your example (which, I assume, is simplified, but nonetheless), Month.DECEMBER is, I think, a better choice, which should be more obvious with a less prominent month number, like 5 or 10.

WDYT?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refactoring Long Parameter List in Constructors (Java)
Option 1: Use builder pattern​​ Here is the example from IntelliJ IDEA: Select the constructor with long parameter list -> “Refactor” -> “Replace ......
Read more >
Why does LocalDateTime not have a String constructor?
You can create an instance of these classes only by calling one of the static factory methods (with ~. parse() being one of...
Read more >
Migrating from 1.x to 2.0 - Noda Time
Parameter names. Some parameters have been renamed for consistency. ... The LocalDateTime constructors accepting tick values have been removed.
Read more >
LocalDateTime (Java Platform SE 8 ) - Oracle Help Center
LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second. Other date and time fields, ...
Read more >
Java – Spring: create LocalDate or LocalDateTime ... - iTecNote
In a spring project, I'd like to create a LocalDate from an @Autowired constructor parameter whose value is in a .properties file.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found